Vrsta PHP

Nette\Utils\Type je razred podatkovnih tipov PHP.

Namestitev:

composer require nette/utils

Vsi primeri predpostavljajo, da je definiran naslednji vzdevek razreda:

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 povratne vrednosti) ali objekt ReflectionParameter ali ReflectionProperty. Rešuje self, static in parent na dejansko ime razreda. Če predmet nima 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 predmet Type v skladu z besedilnim zapisom.

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

getNames(): (string|array)[]

Vrne niz podtipov, ki sestavljajo sestavljeni tip, v obliki niza.

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

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

getTypes(): Type[]

Vrne niz podtipov, ki sestavljajo sestavljeni tip, kot predmete 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

Vrne ime tipa za enostavne tipe, sicer je 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 je tip enostaven. Za enostavne tipe se štejejo tudi tipi, ki jih je mogoče izničiti:

$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 je tip unija.

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

isIntersection(): bool

Vrne, ali je tip presečišče.

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

isBuiltin(): bool

Vrne, ali je tip enostaven in 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 hkrati preprosto ime in 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

Določi, 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() preveri združljivost tipov. Z njo lahko na primer preverimo, ali je mogoče vrednost določenega tipa posredovati 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
različica: 4.0