Nette Documentation Preview

syntax
Tipo PHP
********

.[perex]
[api:Nette\Utils\Type] è una classe per lavorare con i tipi di dati PHP.


Installazione:

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

Tutti gli esempi presuppongono la creazione di un alias:

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


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

Il metodo statico crea un oggetto Type basato sulla reflection. Il parametro può essere un oggetto `ReflectionMethod` o `ReflectionFunction` (restituisce il tipo del valore di ritorno) o `ReflectionParameter` o `ReflectionProperty`. Traduce `self`, `static` e `parent` nel nome effettivo della classe. Se il soggetto non ha alcun tipo, restituisce `null`.

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

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


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

Il metodo statico crea un oggetto Type basato sulla notazione testuale.

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


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

Restituisce un array di sottotipi che compongono il tipo composto, come stringhe.

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

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


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

Restituisce un array di sottotipi che compongono il tipo composto, come oggetti `Type`:

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

Per i tipi semplici, restituisce il nome del tipo, altrimenti null.

```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]
--------------------------

Restituisce se si tratta di un tipo semplice. Anche i tipi semplici nullable sono considerati tipi semplici:

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

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


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

Restituisce se si tratta di un tipo unione.

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


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

Restituisce se si tratta di un tipo intersezione.


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


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

Restituisce se il tipo è semplice e allo stesso tempo un tipo built-in di 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]
-------------------------

Restituisce se il tipo è semplice e allo stesso tempo un nome di classe.

```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]
--------------------------------

Restituisce se il tipo è uno dei tipi interni `self`, `parent`, `static`.

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

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


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

Il metodo `allows()` verifica la compatibilità dei tipi. Ad esempio, consente di determinare se un valore di un certo tipo potrebbe essere passato come parametro.

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

Tipo PHP

Nette\Utils\Type è una classe per lavorare con i tipi di dati PHP.

Installazione:

composer require nette/utils

Tutti gli esempi presuppongono la creazione di un alias:

use Nette\Utils\Type;

fromReflection($reflection): ?Type

Il metodo statico crea un oggetto Type basato sulla reflection. Il parametro può essere un oggetto ReflectionMethod o ReflectionFunction (restituisce il tipo del valore di ritorno) o ReflectionParameter o ReflectionProperty. Traduce self, static e parent nel nome effettivo della classe. Se il soggetto non ha alcun 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 un oggetto Type basato sulla notazione testuale.

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

getNames(): (string|array)[]

Restituisce un array di sottotipi che compongono il tipo composto, come stringhe.

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

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

getTypes(): Type[]

Restituisce un array di sottotipi che compongono il tipo composto, come oggetti Type:

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

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

isUnion(): bool

Restituisce se si tratta di un tipo unione.

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

isIntersection(): bool

Restituisce se si tratta di un tipo intersezione.

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

isBuiltin(): bool

Restituisce se il tipo è semplice e allo stesso tempo un tipo built-in di 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 è semplice e allo stesso tempo 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

Restituisce 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 determinare se un valore di un certo tipo potrebbe 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