Τύπος PHP

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

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

composer require nette/utils

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

use Nette\Utils\Type;

fromReflection ($reflection): ?Type

Η στατική μέθοδος δημιουργεί ένα αντικείμενο Type με βάση τη reflection. Η παράμετρος μπορεί να είναι ένα αντικείμενο 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'); // ή '?string'
$type->getNames();  // ['string', 'null']

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

getTypes(): Type[]

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

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

Επιστρέφει εάν πρόκειται για απλό τύπο. Ως απλοί τύποι θεωρούνται και οι απλοί nullable τύποι:

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

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

isUnion(): bool

Επιστρέφει εάν πρόκειται για τύπο union.

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

isIntersection(): bool

Επιστρέφει εάν πρόκειται για τύπο intersection.

$type = Type::fromString('Foo&Bar');
$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
έκδοση: 4.0