Tip PHP
Nette\Utils\Type este o clasă pentru lucrul cu tipurile de date PHP.
Instalare:
composer require nette/utils
Toate exemplele presupun crearea unui alias:
use Nette\Utils\Type;
fromReflection ($reflection): ?Type
Metoda statică creează un obiect Type pe baza reflecției. Parametrul poate fi un obiect
ReflectionMethod sau ReflectionFunction (returnează tipul valorii returnate) sau
ReflectionParameter ori ReflectionProperty. Traduce self, static și
parent în numele real al clasei. Dacă subiectul nu are niciun tip, returnează null.
class DemoClass
{
public self $foo;
}
$prop = new ReflectionProperty(DemoClass::class, 'foo');
echo Type::fromReflection($prop); // 'DemoClass'
fromString (string $type): Type
Metoda statică creează un obiect Type pe baza notației textuale.
$type = Type::fromString('Foo|Bar');
echo $type; // 'Foo|Bar'
getNames (): (string|array)[]
Returnează un array de subtipuri din care este compus tipul compus, ca șiruri de caractere.
$type = Type::fromString('string|null'); // sau '?string'
$type->getNames(); // ['string', 'null']
$type = Type::fromString('(Foo&Bar)|string');
$type->getNames(); // [['Foo', 'Bar'], 'string']
getTypes(): Type[]
Returnează un array de subtipuri din care este compus tipul compus, ca obiecte ReflectionType:
$type = Type::fromString('string|null'); // sau '?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
Pentru tipurile simple, returnează numele tipului, altfel 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
Returnează dacă este un tip simplu. Tipurile simple nullable sunt, de asemenea, considerate tipuri simple:
$type = Type::fromString('string');
$type->isSimple(); // true
$type->isUnion(); // false
$type = Type::fromString('?Foo'); // sau 'Foo|null'
$type->isSimple(); // true
$type->isUnion(); // true
isUnion(): bool
Returnează dacă este un tip union.
$type = Type::fromString('string|int');
$type->isUnion(); // true
isIntersection(): bool
Returnează dacă este un tip intersection.
$type = Type::fromString('Foo&Bar');
$type->isIntersection(); // true
isBuiltin(): bool
Returnează dacă tipul este simplu și, în același timp, un tip încorporat 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
Returnează dacă tipul este simplu și, în același timp, numele unei 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
Returnează dacă tipul este unul dintre tipurile interne self, parent, static.
$type = Type::fromString('self');
$type->isClassKeyword(); // true
$type = Type::fromString('Foo');
$type->isClassKeyword(); // false
allows (string $type): bool
Metoda allows() verifică compatibilitatea tipurilor. De exemplu, permite să se determine dacă o valoare de un
anumit tip ar putea fi transmisă ca parametru.
$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