Tipo PHP
Nette\Utils\Type es una clase para trabajar con tipos de datos de PHP.
Instalación:
composer require nette/utils
Todos los ejemplos asumen que se ha creado un alias:
use Nette\Utils\Type;
fromReflection ($reflection): ?Type
Método estático que crea un objeto Type
basado en la reflexión. El parámetro puede ser un objeto
ReflectionMethod
o ReflectionFunction
(devuelve el tipo de valor de retorno), o
ReflectionParameter
o ReflectionProperty
. Traduce self
, static
y
parent
al nombre real de la clase. Si el sujeto no tiene tipo, devuelve null
.
class DemoClass
{
public self $foo;
}
$prop = new ReflectionProperty(DemoClass::class, 'foo');
echo Type::fromReflection($prop); // 'DemoClass'
fromString (string $type): Type
Método estático que crea un objeto Type
a partir de su representación textual.
$type = Type::fromString('Foo|Bar');
echo $type; // 'Foo|Bar'
getNames (): (string|array)[]
Devuelve un array con los nombres de los subtipos que componen el tipo compuesto.
$type = Type::fromString('string|null'); // o '?string'
$type->getNames(); // ['string', 'null']
$type = Type::fromString('(Foo&Bar)|string');
$type->getNames(); // [['Foo', 'Bar'], 'string']
getTypes(): Type[]
Devuelve un array de objetos Type
que representan los subtipos que componen el tipo compuesto:
$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
Si el tipo es simple, devuelve su nombre; de lo contrario, devuelve 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
Devuelve si es un tipo simple. Los tipos simples también incluyen tipos nullable simples:
$type = Type::fromString('string');
$type->isSimple(); // true
$type->isUnion(); // false
$type = Type::fromString('?Foo'); // o 'Foo|null'
$type->isSimple(); // true
$type->isUnion(); // true
isUnion(): bool
Devuelve si es un tipo de unión.
$type = Type::fromString('string|int');
$type->isUnion(); // true
isIntersection(): bool
Devuelve si es un tipo de intersección.
$type = Type::fromString('Foo&Bar');
$type->isIntersection(); // true
isBuiltin(): bool
Devuelve si el tipo es simple y también un tipo incorporado de 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
Devuelve si el tipo es simple y también un nombre de clase.
$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
Devuelve si el tipo es uno de los tipos internos self
, parent
, static
.
$type = Type::fromString('self');
$type->isClassKeyword(); // true
$type = Type::fromString('Foo');
$type->isClassKeyword(); // false
allows (string $type): bool
El método allows()
verifica la compatibilidad de tipos. Por ejemplo, permite determinar si un valor de un tipo
determinado podría pasarse como parámetro.
$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