Тип 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'); // или '?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