PHP Típus
A Nette\Utils\Type egy osztály a PHP adattípusokkal való munkához.
Telepítés:
composer require nette/utils
Minden példa feltételezi a létrehozott aliast:
use Nette\Utils\Type;
fromReflection ($reflection): ?Type
Statikus metódus, amely Type objektumot hoz létre reflexió alapján. A paraméter lehet ReflectionMethod
vagy
ReflectionFunction
objektum (a visszatérési érték típusát adja vissza) vagy ReflectionParameter
vagy ReflectionProperty
. Lefordítja a self
, static
és parent
szavakat a
tényleges osztálynévre. Ha az alanynak nincs típusa, null
-t ad vissza.
class DemoClass
{
public self $foo;
}
$prop = new ReflectionProperty(DemoClass::class, 'foo');
echo Type::fromReflection($prop); // 'DemoClass'
fromString (string $type): Type
Statikus metódus, amely Type objektumot hoz létre szöveges leírás alapján.
$type = Type::fromString('Foo|Bar');
echo $type; // 'Foo|Bar'
getNames (): (string|array)[]
Visszaadja az összetett típust alkotó altípusok tömbjét stringként.
$type = Type::fromString('string|null'); // vagy '?string'
$type->getNames(); // ['string', 'null']
$type = Type::fromString('(Foo&Bar)|string');
$type->getNames(); // [['Foo', 'Bar'], 'string']
getTypes(): Type[]
Visszaadja az összetett típust alkotó altípusok tömbjét ReflectionType
objektumként:
$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
Egyszerű típusok esetén visszaadja a típus nevét, egyébként null-t.
$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
Visszaadja, hogy egyszerű típusról van-e szó. Az egyszerű nullable típusok is egyszerűnek számítanak:
$type = Type::fromString('string');
$type->isSimple(); // true
$type->isUnion(); // false
$type = Type::fromString('?Foo'); // vagy 'Foo|null'
$type->isSimple(); // true
$type->isUnion(); // true
isUnion(): bool
Visszaadja, hogy union típusról van-e szó.
$type = Type::fromString('string|int');
$type->isUnion(); // true
isIntersection(): bool
Visszaadja, hogy intersection típusról van-e szó.
$type = Type::fromString('Foo&Bar');
$type->isIntersection(); // true
isBuiltin(): bool
Visszaadja, hogy a típus egyszerű és egyben beépített PHP típus-e.
$type = Type::fromString('string');
$type->isBuiltin(); // true
$type = Type::fromString('string|int');
$type->isBuiltin(); // false
$type = Type::fromString('Foo');
$type->isBuiltin(); // false
isClass(): bool
Visszaadja, hogy a típus egyszerű és egyben osztálynév-e.
$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
Visszaadja, hogy a típus a self
, parent
, static
belső típusok egyike-e.
$type = Type::fromString('self');
$type->isClassKeyword(); // true
$type = Type::fromString('Foo');
$type->isClassKeyword(); // false
allows (string $type): bool
Az allows()
metódus ellenőrzi a típuskompatibilitást. Például lehetővé teszi annak megállapítását,
hogy egy adott típusú érték átadható-e paraméterként.
$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