Nette Documentation Preview

syntax
Tip PHP
*******

.[perex]
[api:Nette\Utils\Type] este o clasă de tip de date PHP.


Instalare:

```shell
composer require nette/utils
```

Toate exemplele presupun că este definit următorul alias de clasă:

```php
use Nette\Utils\Type;
```


fromReflection($reflection): ?Type .[method]
--------------------------------------------

Metoda statică creează un obiect Type bazat pe reflecție. Parametrul poate fi un obiect `ReflectionMethod` sau `ReflectionFunction` (returnează tipul valorii de returnare) sau un obiect `ReflectionParameter` sau `ReflectionProperty`. Rezolvă `self`, `static` și `parent` cu numele real al clasei. Dacă obiectul nu are un tip, returnează `null`.

```php
class DemoClass
{
	public self $foo;
}

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


fromString(string $type): Type .[method]
----------------------------------------

Metoda statică creează obiectul Type în conformitate cu notația text.

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


getNames(): (string|array)[] .[method]
--------------------------------------

Returnează matricea de subtipuri care alcătuiesc tipul compus ca șiruri de caractere.

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

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


getTypes(): Type[] .[method]
----------------------------

Returnează matricea de subtipuri care alcătuiesc tipul compus ca obiecte `Type`:

```php
$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 .[method]
----------------------------------

Returnează numele tipului pentru tipurile simple, în caz contrar este nul.

```php
$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 .[method]
--------------------------

Returnează dacă este un tip simplu. Tipurile simple care pot fi anulate sunt, de asemenea, considerate ca fiind tipuri simple:

```php
$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 .[method]
-------------------------

Returnează dacă este un tip de uniune.

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


isIntersection(): bool .[method]
--------------------------------

Returnează dacă este un tip de intersecție.


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


isBuiltin(): bool .[method]
---------------------------

Returnează dacă tipul este atât un tip simplu, cât și un tip încorporat în PHP.

```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 .[method]
-------------------------

Returnează dacă tipul este atât un simplu cât și un nume de clasă.

```php
$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 .[method]
--------------------------------

Determină dacă tipul este unul dintre tipurile interne `self`, `parent`, `static`.

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

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


allows(string $type): bool .[method]
------------------------------------

Metoda `allows()` verifică compatibilitatea tipurilor. De exemplu, aceasta permite să se verifice dacă o valoare de un anumit tip poate fi transmisă ca parametru.

```php
$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
```

Tip PHP

Nette\Utils\Type este o clasă de tip de date PHP.

Instalare:

composer require nette/utils

Toate exemplele presupun că este definit următorul alias de clasă:

use Nette\Utils\Type;

fromReflection($reflection): ?Type

Metoda statică creează un obiect Type bazat pe reflecție. Parametrul poate fi un obiect ReflectionMethod sau ReflectionFunction (returnează tipul valorii de returnare) sau un obiect ReflectionParameter sau ReflectionProperty. Rezolvă self, static și parent cu numele real al clasei. Dacă obiectul nu are un tip, returnează null.

class DemoClass
{
	public self $foo;
}

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

fromString(string $type): Type

Metoda statică creează obiectul Type în conformitate cu notația text.

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

getNames(): (string|array)[]

Returnează matricea de subtipuri care alcătuiesc tipul compus ca șiruri de caractere.

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

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

getTypes(): Type[]

Returnează matricea de subtipuri care alcătuiesc tipul compus ca obiecte 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

Returnează numele tipului pentru tipurile simple, în caz contrar este nul.

$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

Returnează dacă este un tip simplu. Tipurile simple care pot fi anulate sunt, de asemenea, considerate ca fiind tipuri simple:

$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

Returnează dacă este un tip de uniune.

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

isIntersection(): bool

Returnează dacă este un tip de intersecție.

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

isBuiltin(): bool

Returnează dacă tipul este atât un tip simplu, cât și un tip încorporat în 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

Returnează dacă tipul este atât un simplu cât și un nume de clasă.

$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

Determină dacă tipul este unul dintre tipurile interne self, parent, static.

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

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

allows(string $type): bool

Metoda allows() verifică compatibilitatea tipurilor. De exemplu, aceasta permite să se verifice dacă o valoare de un anumit tip poate fi transmisă ca parametru.

$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