Tipo PHP

Nette\Utils\Type è una classe di tipi di dati PHP.

Installazione:

composer require nette/utils

Tutti gli esempi presuppongono che sia definito il seguente alias di classe:

use Nette\Utils\Type;

fromReflection($reflection): ?Type

Il metodo statico crea un oggetto Tipo basato sulla riflessione. Il parametro può essere un oggetto ReflectionMethod o ReflectionFunction (restituisce il tipo del valore di ritorno) o un oggetto ReflectionParameter o ReflectionProperty. Risolve self, static e parent al nome effettivo della classe. Se l'oggetto non ha un 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 l'oggetto Tipo secondo la notazione del testo.

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

getNames(): (string|array)[]

Restituisce l'array di sottotipi che compongono il tipo composto come stringhe.

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

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

getTypes(): Type[]

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

Restituisce il nome del tipo per i tipi semplici, 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'); // nebo 'Foo|null'
$type->isSimple();       // true
$type->isUnion();        // true

isUnion(): bool

Restituisce se si tratta di un tipo union.

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

isIntersection(): bool

Restituisce se si tratta di un tipo di intersezione.

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

isBuiltin(): bool

Restituisce se il tipo è sia un tipo semplice che un tipo incorporato in 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 è sia un tipo semplice che 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

Determina 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 verificare se un valore di un certo tipo può 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