Typ PHP

Nette\Utils\Type jest klasą służącą do pracy z typami danych PHP.

Instalacja:

composer require nette/utils

Wszystkie przykłady zakładają, że alias został utworzony:

use Nette\Utils\Type;

fromReflection($reflection): ?Type

Metoda statyczna tworzy obiekt Type w oparciu o refleksję. Parametrem może być obiekt ReflectionMethod lub ReflectionFunction (zwraca typ wartości zwracanej) lub ReflectionParameter lub ReflectionProperty. Przekłada 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 zgodnie z notacją tekstową.

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

getNames(): (string|array)[]

Zwraca tablicę podtypów składających się na typ złożony, jako łańcuchy.

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

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

getTypes(): Type[]

Zwraca tablicę podtypów składających się na 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

Zwraca nazwę typu dla typów prostych, 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 nullable:

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

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

isUnion(): bool

Zwraca, czy istnieje typ unii.

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

isIntersection(): bool

Zwraca, czy o jest typem przecięcia.

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

isBuiltin(): bool

Zwraca czy typ jest zarówno prosty jak i wbudowany w 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 jednocześnie prosty i 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 typów wewnętrznych self, parent, static.

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

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

allows(string $type): bool

Metoda allows() weryfikuje zgodność 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