Skip to content
PHP Ecosystem
Explore

ModelInterface

API reference › Model › ModelInterface

interfaceUIAwesome\Model\ModelInterface
Sourcesrc/ModelInterface.php
Implemented by 2 types

Interface implemented by classes supporting model data binding.

MethodDescriptionDefined by
add() abstractAdds a new property to the model.ModelInterface
getData() abstractReturns the raw data for the model.ModelInterface
getModelName() abstractReturns the model name that this model class should use.ModelInterface
getNames() abstractReturns the list of property names.ModelInterface
getTypes() abstractReturns the list of property types indexed by property names.ModelInterface
getValue() abstractReturns the value (raw data) for the specified property.ModelInterface
has() abstractChecks if the model has the specified property.ModelInterface
isEmpty() abstractWhether the model has no loaded raw data.ModelInterface
isType() abstractChecks if the property is of the specified type.ModelInterface
load() abstractPopulates the model with input data.ModelInterface
setValue() abstractSets the value for the specified property.ModelInterface
setValues() abstractSets values for multiple properties.ModelInterface
toArray() abstractReturns model properties as an array.ModelInterface
abstract public function add(string $property, array|string $type): void

Adds a new property to the model.

Parameters

ParameterTypeDescription
$propertystringProperty name.
$typearray|stringProperty 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.
abstract public function getData(): array

Returns the raw data for the model.

Returns

array: Raw data for the model.

abstract public function getModelName(): string

Returns the model name that this model class should use.

The model name is mainly used by BaseModel 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()

abstract public function getNames(): list<string>

Returns the list of property names.

Returns

list<string>: List of property names.

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.

abstract public function getValue(string $property): mixed

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

Parameters

ParameterTypeDescription
$propertystringProperty name.

Returns

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

abstract public function has(string $property): bool

Checks if the model has the specified property.

Parameters

ParameterTypeDescription
$propertystringProperty name.

Returns

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

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.

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

Checks if the property is of the specified type.

Parameters

ParameterTypeDescription
$propertystringProperty name.
$typestringType name.

Returns

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

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() 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

ParameterTypeDescription
$dataiterableData array to load, typically server request properties.
$modelNamestring|nullScope from which to get data.

Returns

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

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

Sets the value for the specified property.

Parameters

ParameterTypeDescription
$propertystringProperty name.
$valuemixedValue to set.
abstract public function setValues(array $data, array $except = []): void

Sets values for multiple properties.

Parameters

ParameterTypeDescription
$dataarrayKey value pairs to set for the properties.
$exceptarrayProperties to exclude from the setting using camelCase names. If not empty, the listed properties are skipped. If empty, all properties from $data are applied.
abstract public function toArray(bool $snakeCase = false, array $exceptProperties = []): array<string, mixed>

Returns model properties as an array.

Parameters

ParameterTypeDescription
$snakeCaseboolWhether keys should be converted to snake_case.
$exceptPropertiesarrayList of properties to exclude.

Returns

array<string, mixed>