Τύπος PHP

Nette\Utils\Type είναι μια κλάση τύπου δεδομένων PHP.

Εγκατάσταση:

composer require nette/utils

Όλα τα παραδείγματα προϋποθέτουν ότι έχει οριστεί το ακόλουθο ψευδώνυμο κλάσης:

use Nette\Utils\Type;

fromReflection($reflection): ?Type

Η στατική μέθοδος δημιουργεί ένα αντικείμενο Type με βάση την αντανάκλαση. Η παράμετρος μπορεί να είναι ένα αντικείμενο ReflectionMethod ή ReflectionFunction (επιστρέφει τον τύπο της τιμής επιστροφής) ή ένα αντικείμενο ReflectionParameter ή ReflectionProperty. Επιλύει τα self, static και parent στο πραγματικό όνομα της κλάσης. Εάν το αντικείμενο δεν έχει τύπο, επιστρέφει null.

class DemoClass
{
	public self $foo;
}

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

fromString(string $type): Type

Η στατική μέθοδος δημιουργεί το αντικείμενο Type σύμφωνα με τον συμβολισμό κειμένου.

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

getNames(): (string|array)[]

Επιστρέφει τον πίνακα των υποτύπων που συνθέτουν τον σύνθετο τύπο ως συμβολοσειρές.

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

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

getTypes(): Type[]

Επιστρέφει τον πίνακα των υποτύπων που συνθέτουν τον σύνθετο τύπο ως αντικείμενα Type:

$type = Type::fromString('string|null'); // ή '?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

Επιστρέφει το όνομα του τύπου για απλούς τύπους, διαφορετικά 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

Επιστρέφει αν πρόκειται για απλό τύπο. Οι απλοί μηδενιζόμενοι τύποι θεωρούνται επίσης απλοί τύποι:

$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

Επιστρέφει αν πρόκειται για τύπο ένωσης.

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

isIntersection(): bool

Επιστρέφει αν είναι τύπος διασταύρωσης.

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

isBuiltin(): bool

Επιστρέφει αν ο τύπος είναι τόσο ένας απλός όσο και ένας ενσωματωμένος τύπος της 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

Επιστρέφει αν ο τύπος είναι τόσο ένας απλός τύπος όσο και ένα όνομα κλάσης.

$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

Προσδιορίζει αν ο τύπος είναι ένας από τους εσωτερικούς τύπους self, parent, static.

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

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

allows(string $type): bool

Η μέθοδος allows() επαληθεύει τη συμβατότητα των τύπων. Για παράδειγμα, επιτρέπει να ελέγξετε αν μια τιμή ενός συγκεκριμένου τύπου μπορεί να περάσει ως παράμετρος.

$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