Tipo PHP

Nette\Utils\Type è una classe per lavorare con i tipi di dati PHP.

Installazione:

composer require nette/utils

Tutti gli esempi presuppongono la creazione di un alias:

use Nette\Utils\Type;

fromReflection ($reflection): ?Type

Il metodo statico crea un oggetto Type basato sulla reflection. Il parametro può essere un oggetto ReflectionMethod o ReflectionFunction (restituisce il tipo del valore di ritorno) o ReflectionParameter o ReflectionProperty. Traduce self, static e parent nel nome effettivo della classe. Se il soggetto non ha alcun tipo, restituisce null.

class DemoClass
{
	public self $foo;
}

$prop = new ReflectionProperty(DemoClass::class, 'foo');
echo Type::fromReflection($prop); // 'DemoClass'

fromString (string $type): Type

Il metodo statico crea un oggetto Type basato sulla notazione testuale.

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

getNames (): (string|array)[]

Restituisce un array di sottotipi che compongono il tipo composto, come stringhe.

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

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

getTypes(): Type[]

Restituisce un array di sottotipi che compongono il tipo composto, come oggetti 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

Per i tipi semplici, restituisce il nome del tipo, altrimenti 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

Restituisce se si tratta di un tipo semplice. Anche i tipi semplici nullable sono considerati tipi semplici:

$type = Type::fromString('string');
$type->isSimple();       // true
$type->isUnion();        // false

$type = Type::fromString('?Foo'); // o 'Foo|null'
$type->isSimple();       // true
$type->isUnion();        // true

isUnion(): bool

Restituisce se si tratta di un tipo unione.

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

isIntersection(): bool

Restituisce se si tratta di un tipo intersezione.

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

isBuiltin(): bool

Restituisce se il tipo è semplice e allo stesso tempo un tipo built-in di 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

Restituisce se il tipo è semplice e allo stesso tempo un nome di 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

Restituisce se il tipo è uno dei tipi interni self, parent, static.

$type = Type::fromString('self');
$type->isClassKeyword();   // true

$type = Type::fromString('Foo');
$type->isClassKeyword();   // false

allows (string $type): bool

Il metodo allows() verifica la compatibilità dei tipi. Ad esempio, consente di determinare se un valore di un certo tipo potrebbe essere passato come parametro.

$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
versione: 4.0