Typ PHP

Nette\Utils\Type to klasa do pracy z typami danych PHP.

Instalacja:

composer require nette/utils

Wszystkie przykłady zakładają utworzenie aliasu:

use Nette\Utils\Type;

fromReflection ($reflection): ?Type

Metoda statyczna tworzy obiekt Type na podstawie refleksji. Parametrem może być obiekt ReflectionMethod lub ReflectionFunction (zwraca typ wartości zwracanej) lub ReflectionParameter albo ReflectionProperty. Tłumaczy self, static i parent na rzeczywistą nazwę klasy. Jeśli podmiot nie ma typu, zwraca null.

class DemoClass
{
	public self $foo;
}

$prop = new ReflectionProperty(DemoClass::class, 'foo');
echo Type::fromReflection($prop); // 'DemoClass'

fromString (string $type): Type

Metoda statyczna tworzy obiekt Type na podstawie zapisu tekstowego.

$type = Type::fromString('Foo|Bar');
echo $type;      // 'Foo|Bar'

getNames (): (string|array)[]

Zwraca tablicę podtypów, z których składa się typ złożony, jako ciągi znaków.

$type = Type::fromString('string|null'); // lub '?string'
$type->getNames();  // ['string', 'null']

$type = Type::fromString('(Foo&Bar)|string');
$type->getNames();  // [['Foo', 'Bar'], 'string']

getTypes(): Type[]

Zwraca tablicę podtypów, z których składa się typ złożony, jako obiekty 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

Dla typów prostych zwraca nazwę typu, w przeciwnym razie 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

Zwraca, czy jest to typ prosty. Za typy proste uważa się również proste typy dopuszczające wartość null:

$type = Type::fromString('string');
$type->isSimple();       // true
$type->isUnion();        // false

$type = Type::fromString('?Foo'); // lub 'Foo|null'
$type->isSimple();       // true
$type->isUnion();        // true

isUnion(): bool

Zwraca, czy jest to typ unii.

$type = Type::fromString('string|int');
$type->isUnion();        // true

isIntersection(): bool

Zwraca, czy jest to typ przecięcia.

$type = Type::fromString('Foo&Bar');
$type->isIntersection(); // true

isBuiltin(): bool

Zwraca, czy typ jest prosty i jednocześnie jest wbudowanym typem 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

Zwraca, czy typ jest prosty i jednocześnie jest nazwą klasy.

$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

Zwraca, czy typ jest jednym z wewnętrznych typów self, parent, static.

$type = Type::fromString('self');
$type->isClassKeyword();   // true

$type = Type::fromString('Foo');
$type->isClassKeyword();   // false

allows (string $type): bool

Metoda allows() weryfikuje kompatybilność typów. Na przykład pozwala sprawdzić, czy wartość określonego typu może być przekazana jako parametr.

$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
wersja: 4.0