Tipo de PHP

Nette\Utils\Type es una clase de tipo de datos PHP.

Instalación:

composer require nette/utils

Todos los ejemplos asumen que el siguiente alias de clase está definido:

use Nette\Utils\Type;

fromReflection($reflection): ?Type

El método static crea un objeto Type basado en la reflexión. El parámetro puede ser un objeto ReflectionMethod o ReflectionFunction (devuelve el tipo del valor de retorno) o un objeto ReflectionParameter o ReflectionProperty. Resuelve self, static y parent al nombre real de la clase. Si el asunto 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

El método estático crea el objeto Type según la notación de texto.

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

getNames(): (string|array)[]

Devuelve la matriz de subtipos que forman el tipo compuesto como cadenas.

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

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

getTypes(): Type[]

Devuelve la matriz de subtipos que forman el tipo compuesto como objetos Type:

$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

Devuelve el nombre del tipo para tipos simples, en caso contrario 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 se trata de un tipo simple. Los tipos simples anulables también se consideran tipos simples:

$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

Devuelve si se trata de un tipo de unión.

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

isIntersection(): bool

Devuelve si es un tipo intersección.

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

isBuiltin(): bool

Devuelve si el tipo es tanto un tipo simple como 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 tanto simple como 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

Determina 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 comprobar si un valor de un determinado tipo puede 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
versión: 4.0