PHP Türü

Nette\Utils\Type bir PHP veri türü sınıfıdır.

Kurulum:

composer require nette/utils

Tüm örnekler aşağıdaki sınıf takma adının tanımlandığını varsayar:

use Nette\Utils\Type;

fromReflection($reflection): ?Type

Statik yöntem, yansımaya dayalı bir Type nesnesi oluşturur. Parametre bir ReflectionMethod veya ReflectionFunction nesnesi (dönüş değerinin türünü döndürür) ya da bir ReflectionParameter veya ReflectionProperty nesnesi olabilir. self , static ve parent öğelerini gerçek sınıf adına çözümler. Öznenin türü yoksa, null döndürür.

class DemoClass
{
	public self $foo;
}

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

fromString(string $type): Type

Statik yöntem, metin gösterimine göre Type nesnesini oluşturur.

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

getNames(): (string|array)[]

Bileşik türü oluşturan alt türlerin dizisini dizeler olarak döndürür.

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

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

getTypes(): Type[]

Bileşik türü oluşturan alt türlerin dizisini Type nesneleri olarak döndürür:

$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

Basit türler için tür adını döndürür, aksi takdirde 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

Basit bir tür olup olmadığını döndürür. Basit nullable tipler de basit tip olarak kabul edilir:

$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

Bir birlik türü olup olmadığını döndürür.

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

isIntersection(): bool

Bir kesişim türü olup olmadığını döndürür.

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

isBuiltin(): bool

Türün hem basit hem de PHP yerleşik türü olup olmadığını döndürür.

$type = Type::fromString('string');
$type->isBuiltin(); // true

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

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

isClass(): bool

Türün hem basit hem de bir sınıf adı olup olmadığını döndürür.

$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

Türün self, parent, static dahili türlerinden biri olup olmadığını belirleyin.

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

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

allows(string $type): bool

allows() yöntemi tür uyumluluğunu doğrular. Örneğin, belirli bir türdeki bir değerin parametre olarak geçirilip geçirilemeyeceğini kontrol etmeyi sağlar.

$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