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

Повертає, чи є тип простим. До простих типів відносяться також прості nullable типи:

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

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

isUnion(): bool

Повертає, чи є тип union типом.

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

isIntersection(): bool

Повертає, чи є тип intersection типом.

$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