# ModelInterface

> Interface implemented by classes supporting model data binding.

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

| interface | `UIAwesome\Model\ModelInterface` |
| --- | --- |
| Source | [`src/ModelInterface.php`](https://github.com/ui-awesome/model/blob/main/src/ModelInterface.php#L10) |

Implemented by 2 types:

- [`BaseModel`](/docs/extensions/model/reference/basemodel/index.md)
- [`UIAwesome\FormModel\FormModelInterface`](/docs/extensions/form-model/reference/formmodelinterface/index.md)

Interface implemented by classes supporting model data binding.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`add()`](#method-add) abstract | Adds a new property to the model. | `ModelInterface` |
| [`getData()`](#method-getData) abstract | Returns the raw data for the model. | `ModelInterface` |
| [`getModelName()`](#method-getModelName) abstract | Returns the model name that this model class should use. | `ModelInterface` |
| [`getNames()`](#method-getNames) abstract | Returns the list of property names. | `ModelInterface` |
| [`getTypes()`](#method-getTypes) abstract | Returns the list of property types indexed by property names. | `ModelInterface` |
| [`getValue()`](#method-getValue) abstract | Returns the value (raw data) for the specified property. | `ModelInterface` |
| [`has()`](#method-has) abstract | Checks if the model has the specified property. | `ModelInterface` |
| [`isEmpty()`](#method-isEmpty) abstract | Whether the model has no loaded raw data. | `ModelInterface` |
| [`isType()`](#method-isType) abstract | Checks if the property is of the specified type. | `ModelInterface` |
| [`load()`](#method-load) abstract | Populates the model with input data. | `ModelInterface` |
| [`setValue()`](#method-setValue) abstract | Sets the value for the specified property. | `ModelInterface` |
| [`setValues()`](#method-setValues) abstract | Sets values for multiple properties. | `ModelInterface` |
| [`toArray()`](#method-toArray) abstract | Returns model properties as an array. | `ModelInterface` |

## Method details

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

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

Adds a new property to the model.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$property` | `string` | Property name. |
| `$type` | `array\|string` | Property type. This can be a string or an array of strings representing the property type. If an array is given, the property is considered to be multi-valued and the property value should be an array of values of the given types. |

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

```php
abstract public function getData(): array
```

Returns the raw data for the model.

**Returns**

`array`: Raw data for the model.

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

```php
abstract public function getModelName(): string
```

Returns the model name that this model class should use.

The model name is mainly used by [`BaseModel`](/docs/extensions/model/reference/basemodel/index.md) to decide how to name the input fields for the properties
in a model.
- If the model name is "A" and an property name is "b", then the corresponding input name would be "A[b]".
- If the model name is an empty string, then the input name would be "b".

The purpose of the above naming schema is that for forms which contain multiple different models, the properties
of each model are grouped in sub-arrays of the POST-data, and it's easier to differentiate between them.
- By default, this method returns the model class name (without the namespace part) as the model name.
- You may override it when the model is used in different forms.

**Returns**

`string`: Model name class, without a namespace part or empty string when class is anonymous. [`load()`](/docs/extensions/model/reference/modelinterface/index.md#method-load)

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

```php
abstract public function getNames(): list<string>
```

Returns the list of property names.

**Returns**

`list<string>`: List of property names.

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

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

Returns the list of property types indexed by property names.

**Returns**

`array`: List of property types indexed by property names.

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

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

Returns the value (raw data) for the specified property.

**Parameters**

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

**Returns**

`mixed`: Value (raw data) for the specified property.

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

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

Checks if the model has the specified property.

**Parameters**

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

**Returns**

`bool`: `true` if the model has the specified property, `false` otherwise.

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

```php
abstract public function isEmpty(): bool
```

Whether the model has no loaded raw data.

**Returns**

`bool`: `true` if the model has no loaded raw data, `false` otherwise.

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

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

Checks if the property is of the specified type.

**Parameters**

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

**Returns**

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

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

```php
abstract public function load(iterable $data, string|null $modelName = null): bool
```

Populates the model with input data.

The `load()` method gets the model name from the [`getModelName()`](/docs/extensions/model/reference/modelinterface/index.md#method-getModelName) method (which you may override), unless
the `$modelName` parameter is given.
If the model name is an empty string, `load()` populates the model with the whole `$data` array instead of
`$data['ModelName']`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$data` | `iterable` | Data array to load, typically server request properties. |
| `$modelName` | `string\|null` | Scope from which to get data. |

**Returns**

`bool`: `true` if the model is successfully populated with some data, `false` otherwise.

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

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

Sets the value for the specified property.

**Parameters**

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

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

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

Sets values for multiple properties.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$data` | `array` | Key value pairs to set for the properties. |
| `$except` | `array` | Properties to exclude from the setting using camelCase names. If not empty, the listed properties are skipped. If empty, all properties from `$data` are applied. |

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

```php
abstract public function toArray(bool $snakeCase = false, array $exceptProperties = []): array<string, mixed>
```

Returns model properties as an array.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$snakeCase` | `bool` | Whether keys should be converted to snake_case. |
| `$exceptProperties` | `array` | List of properties to exclude. |

**Returns**

`array<string, mixed>`