PHP 型
Nette\Utils\Type は、PHPのデータ型を扱うためのクラスです。
インストール:
composer require nette/utils
すべての例は、エイリアスが作成されていることを前提としています:
use Nette\Utils\Type;
fromReflection ($reflection): ?Type
静的メソッドは、リフレクションに基づいてTypeオブジェクトを作成します。パラメータは
ReflectionMethod
または ReflectionFunction
オブジェクト(戻り値の型を返す)または
ReflectionParameter
または ReflectionProperty
です。self
、static
、parent
を実際のクラス名に変換します。サブジェクトに型がない場合は null
を返します。
class DemoClass
{
public self $foo;
}
$prop = new ReflectionProperty(DemoClass::class, 'foo');
echo Type::fromReflection($prop); // 'DemoClass'
fromString (string $type): Type
静的メソッドは、テキスト表記に基づいてTypeオブジェクトを作成します。
$type = Type::fromString('Foo|Bar');
echo $type; // 'Foo|Bar'
getNames (): (string|array)[]
複合型を構成するサブタイプの配列を文字列として返します。
$type = Type::fromString('string|null'); // または '?string'
$type->getNames(); // ['string', 'null']
$type = Type::fromString('(Foo&Bar)|string');
$type->getNames(); // [['Foo', 'Bar'], 'string']
getTypes(): Type[]
複合型を構成するサブタイプの配列を ReflectionType
オブジェクトとして返します:
$type = Type::fromString('string|null'); // or '?string'
$type->getTypes(); // [Type::fromString('string'), Type::fromString('null')]
$type = Type::fromString('(Foo&Bar)|string');
$type->getTypes(); // [Type::fromString('Foo&Bar'), Type::fromString('string')]
$type = Type::fromString('Foo&Bar');
$type->getTypes(); // [Type::fromString('Foo'), Type::fromString('Bar')]
getSingleName(): ?string
単純な型の場合、型名を返します。それ以外の場合はnullを返します。
$type = Type::fromString('string|null');
echo $type; // '?string'
echo $type->getSingleName(); // 'string'
$type = Type::fromString('?Foo');
echo $type; // '?Foo'
echo $type->getSingleName(); // 'Foo'
$type = Type::fromString('Foo|Bar');
echo $type; // 'Foo|Bar'
echo $type->getSingleName(); // null
isSimple(): bool
単純な型かどうかを返します。単純なnull許容型も単純な型と見なされます:
$type = Type::fromString('string');
$type->isSimple(); // true
$type->isUnion(); // false
$type = Type::fromString('?Foo'); // または 'Foo|null'
$type->isSimple(); // true
$type->isUnion(); // true
isUnion(): bool
共用体型かどうかを返します。
$type = Type::fromString('string|int');
$type->isUnion(); // true
isIntersection(): bool
交差型かどうかを返します。
$type = Type::fromString('Foo&Bar');
$type->isIntersection(); // true
isBuiltin(): bool
型が単純であり、かつPHPの組み込み型であるかどうかを返します。
$type = Type::fromString('string');
$type->isBuiltin(); // true
$type = Type::fromString('string|int');
$type->isBuiltin(); // false
$type = Type::fromString('Foo');
$type->isBuiltin(); // false
isClass(): bool
型が単純であり、かつクラス名であるかどうかを返します。
$type = Type::fromString('string');
$type->isClass(); // false
$type = Type::fromString('Foo|null');
$type->isClass(); // true
$type = Type::fromString('Foo|Bar');
$type->isClass(); // false
isClassKeyword(): bool
型が内部型 self
、parent
、static
のいずれかであるかどうかを返します。
$type = Type::fromString('self');
$type->isClassKeyword(); // true
$type = Type::fromString('Foo');
$type->isClassKeyword(); // false
allows (string $type): bool
allows()
メソッドは型の互換性を検証します。例えば、特定の型の値がパラメータとして渡される可能性があるかどうかを判断できます。
$type = Type::fromString('string|null');
$type->allows('string'); // true
$type->allows('null'); // true
$type->allows('Foo'); // false
$type = Type::fromString('mixed');
$type->allows('null'); // true