# TypeCollector

> Collects and manages model property types and values.

[API reference](/docs/api/index.md) › [Model](/docs/extensions/model/reference/index.md) › TypeCollector

| final class | `UIAwesome\Model\TypeCollector` |
| --- | --- |
| Source | [`src/TypeCollector.php`](https://github.com/ui-awesome/model/blob/main/src/TypeCollector.php#L42) |

Collects and manages model property types and values.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Collects property metadata for the supplied model. | `TypeCollector` |
| [`add()`](#method-add) | Adds a new property to the list of properties types. | `TypeCollector` |
| [`getTypes()`](#method-getTypes) | Returns the list of properties types indexed by property name. | `TypeCollector` |
| [`getValue()`](#method-getValue) | Returns the value of a property. | `TypeCollector` |
| [`has()`](#method-has) | Checks whether a property exists. | `TypeCollector` |
| [`isForModel()`](#method-isForModel) | Checks whether this collector is bound to the exact model instance. | `TypeCollector` |
| [`isType()`](#method-isType) | Checks whether a property supports the specified type. | `TypeCollector` |
| [`phpTypeCast()`](#method-phpTypeCast) | Converts the value of an property to the type specified by type hinting in the PHPDoc. | `TypeCollector` |
| [`setValue()`](#method-setValue) | Sets a value for one property. | `TypeCollector` |
| [`setValues()`](#method-setValues) | Sets values for multiple properties at once. | `TypeCollector` |
| [`toArray()`](#method-toArray) | Returns an array representation of the model properties and their values. | `TypeCollector` |
| [`withModel()`](#method-withModel) | Copies collector state for the same model class without collecting metadata or assigning values again. | `TypeCollector` |

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(UIAwesome\Model\ModelInterface $model)
```

Collects property metadata for the supplied model.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$model` | [`ModelInterface`](/docs/extensions/model/reference/modelinterface/index.md) | Model whose properties are collected and assigned. |

### add() <span id="method-add"></span>

```php
public function add(string $property, array|string $type): void
```

Adds a new property to the list of properties types.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$property` | `string` | Name of the property. |
| `$type` | `array\|string` | Type of the property. |

### getTypes() <span id="method-getTypes"></span>

```php
public function getTypes(): array
```

Returns the list of properties types indexed by property name.

**Returns**

`array`: List of properties types indexed by property name.

### getValue() <span id="method-getValue"></span>

```php
public function getValue(string $property): mixed
```

Returns the value of a property.

Dot notation is supported for nested models, for example `profile.email`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$property` | `string` | Property name. |

**Returns**

`mixed`: Value of the property.

### has() <span id="method-has"></span>

```php
public function has(string $property): bool
```

Checks whether a property exists.

Dot notation is supported for nested models, for example `profile.email`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$property` | `string` | Property name. |

**Returns**

`bool`: `true` if the property exists, `false` otherwise.

### isForModel() <span id="method-isForModel"></span>

```php
public function isForModel(UIAwesome\Model\ModelInterface $model): bool
```

Checks whether this collector is bound to the exact model instance.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$model` | [`ModelInterface`](/docs/extensions/model/reference/modelinterface/index.md) |  |

**Returns**

`bool`

### isType() <span id="method-isType"></span>

```php
public function isType(string $property, string $type): bool
```

Checks whether a property supports the specified type.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$property` | `string` | Property name. |
| `$type` | `string` | Type name. |

**Returns**

`bool`: `true` if the property supports the specified type, `false` otherwise.

### phpTypeCast() <span id="method-phpTypeCast"></span>

```php
public function phpTypeCast(string $property, mixed $value): mixed
```

Converts the value of an property to the type specified by type hinting in the PHPDoc.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$property` | `string` | Property name. |
| `$value` | `mixed` | Value to be converted. |

**Returns**

`mixed`: Value of the property converted to the type specified by PHPDoc.

### setValue() <span id="method-setValue"></span>

```php
public function setValue(string $property, mixed $value): void
```

Sets a value for one property.

Dot notation is supported for nested models, for example `profile.email`.`

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$property` | `string` | The property name. |
| `$value` | `mixed` | The value to assign. |

### setValues() <span id="method-setValues"></span>

```php
public function setValues(array $data, array $except = []): void
```

Sets values for multiple properties at once.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$data` | `array` |  |
| `$except` | `array` |  |

### toArray() <span id="method-toArray"></span>

```php
public function toArray(bool $snakeCase, array $exceptProperties = []): array
```

Returns an array representation of the model properties and their values.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$snakeCase` | `bool` | Whether to convert property names to snake_case. |
| `$exceptProperties` | `array` | CamelCase property names to exclude from the resulting array. |

**Returns**

`array`: An array representation of the model properties and their values.

### withModel() <span id="method-withModel"></span>

```php
public function withModel(UIAwesome\Model\ModelInterface $model): self
```

Copies collector state for the same model class without collecting metadata or assigning values again.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$model` | [`ModelInterface`](/docs/extensions/model/reference/modelinterface/index.md) |  |

**Returns**

`self`

**Throws**

- [`InvalidArgumentException`](https://www.php.net/manual/en/class.invalidargumentexception.php): if the target model has a different concrete class.