Tipo PHP

Nette\Utils\Type é uma classe para trabalhar com tipos de dados PHP.

Instalação:

composer require nette/utils

Todos os exemplos assumem que um alias foi criado:

use Nette\Utils\Type;

fromReflection ($reflection): ?Type

O método estático cria um objeto Type com base na reflexão. O parâmetro pode ser um objeto ReflectionMethod ou ReflectionFunction (retorna o tipo de valor de retorno) ou ReflectionParameter ou ReflectionProperty. Traduz self, static e parent para o nome real da classe. Se o sujeito não tiver tipo, retorna null.

class DemoClass
{
	public self $foo;
}

$prop = new ReflectionProperty(DemoClass::class, 'foo');
echo Type::fromReflection($prop); // 'DemoClass'

fromString (string $type): Type

O método estático cria um objeto Type a partir de uma notação de texto.

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

getNames (): (string|array)[]

Retorna um array de subtipos que compõem o tipo composto, como strings.

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

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

getTypes(): Type[]

Retorna um array de subtipos que compõem o tipo composto, como objetos ReflectionType:

$type = Type::fromString('string|null'); // ou '?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

Para tipos simples, retorna o nome do tipo, caso contrário, 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

Retorna se é um tipo simples. Tipos simples anuláveis também são considerados tipos simples:

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

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

isUnion(): bool

Retorna se é um tipo de união.

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

isIntersection(): bool

Retorna se é um tipo de interseção.

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

isBuiltin(): bool

Retorna se o tipo é simples e também um tipo embutido do 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

Retorna se o tipo é simples e também um nome de classe.

$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

Retorna se o tipo é um dos tipos internos self, parent, static.

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

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

allows (string $type): bool

O método allows() verifica a compatibilidade de tipos. Por exemplo, permite verificar se um valor de um determinado tipo pode ser passado 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
versão: 4.0