Tipo PHP

Nette\Utils\Type é uma classe do tipo de dados PHP.

Instalação:

composer require nette/utils

Todos os exemplos assumem que a seguinte classe está definida:

use Nette\Utils\Type;

fromReflection($reflection): ?Type

O método estático cria um objeto do tipo baseado na reflexão. O parâmetro pode ser um objeto ReflectionMethod ou ReflectionFunction (retorna o tipo do valor de retorno) ou um objeto ReflectionParameter ou ReflectionProperty. Resolve self, static e parent para o nome da classe real. Se o objeto não tem tipo, ele 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 o objeto Tipo de acordo com a notação de texto.

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

getNames(): (string|array)[]

Retorna o conjunto de subtipos que compõem o tipo composto como cordas.

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

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

getTypes(): Type[]

Retorna o conjunto de subtipos que compõem o tipo composto 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

Devolve o nome do tipo para tipos simples, caso contrário, nulo.

$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. Os 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'); // nebo 'Foo|null'
$type->isSimple();       // true
$type->isUnion();        // true

isUnion(): bool

Retorna se é um tipo de sindicato.

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

isIntersection(): bool

Retorna se é um tipo de interseção.

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

isBuiltin(): bool

Retorna se o tipo é ao mesmo tempo um tipo simples e um tipo PHP embutido.

$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 é tanto um nome simples como 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

Determinar 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 tipo. Por exemplo, ele 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