PHP Tip
Nette\Utils\Type je razred za delo s podatkovnimi tipi PHP.
Namestitev:
composer require nette/utils
Vsi primeri predpostavljajo ustvarjen alias:
use Nette\Utils\Type;
fromReflection ($reflection): ?Type
Statična metoda ustvari objekt Type na podlagi refleksije. Parameter je lahko objekt ReflectionMethod
ali
ReflectionFunction
(vrne tip vrnjene vrednosti) ali ReflectionParameter
ali
ReflectionProperty
. Prevede self
, static
in parent
v dejansko ime razreda.
Če subjekt nima nobenega tipa, vrne null
.
class DemoClass
{
public self $foo;
}
$prop = new ReflectionProperty(DemoClass::class, 'foo');
echo Type::fromReflection($prop); // 'DemoClass'
fromString (string $type): Type
Statična metoda ustvari objekt Type glede na besedilni zapis.
$type = Type::fromString('Foo|Bar');
echo $type; // 'Foo|Bar'
getNames (): (string|array)[]
Vrne polje podtipov, iz katerih je sestavljen sestavljeni tip, kot nize.
$type = Type::fromString('string|null'); // nebo '?string'
$type->getNames(); // ['string', 'null']
$type = Type::fromString('(Foo&Bar)|string');
$type->getNames(); // [['Foo', 'Bar'], 'string']
getTypes(): Type[]
Vrne polje podtipov, iz katerih je sestavljen sestavljeni tip, kot objekte ReflectionType
:
$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
Pri preprostih tipih vrne ime tipa, sicer 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
Vrne, ali gre za preprost tip. Za preproste tipe se štejejo tudi preprosti nullable tipi:
$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
Vrne, ali gre za union tip.
$type = Type::fromString('string|int');
$type->isUnion(); // true
isIntersection(): bool
Vrne, ali gre za intersection tip.
$type = Type::fromString('Foo&Bar');
$type->isIntersection(); // true
isBuiltin(): bool
Vrne, ali je tip preprost in hkrati vgrajen tip 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
Vrne, ali je tip preprost in hkrati ime razreda.
$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
Vrne, ali je tip eden od notranjih tipov self
, parent
, static
.
$type = Type::fromString('self');
$type->isClassKeyword(); // true
$type = Type::fromString('Foo');
$type->isClassKeyword(); // false
allows (string $type): bool
Metoda allows()
preverja združljivost tipov. Na primer, omogoča ugotoviti, ali bi vrednost določenega tipa
lahko bila posredovana kot parameter.
$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