Interface Yiisoft\Arrays\ArrayableInterface
ArrayableInterface should be implemented by classes that want to support customizable representation
of their instances.
For example, if a class implements ArrayableInterface, by calling {@see \
The methods {@see \
Public Methods
| Method | Description | Defined By |
|---|---|---|
| extraFields() | Returns the list of additional fields that can be returned by {@see toArray()} in addition to those listed in {@see fields()}. | Yiisoft\ |
| fields() | Returns the list of fields that should be returned by default by {@see toArray()} when no specific fields are specified. | Yiisoft\ |
| toArray() | Converts the object into an array. | Yiisoft\ |
Method Details
Returns the list of additional fields that can be returned by {@see toArray()} in addition to those listed in {@see fields()}.
This method is similar to {@see \
See also:
| public abstract array extraFields ( ) | ||
| return | array |
The list of expandable field names or field definitions. Please refer
to {@see \ |
|---|---|---|
public function extraFields(): array;
Returns the list of fields that should be returned by default by {@see toArray()} when no specific fields are specified.
A field is a named element in the returned array by {@see \
This method should return an array of field names or field definitions. If the former, the field name will be treated as an object property name whose value will be used as the field value. If the latter, the array key should be the field name while the array value should be the corresponding field definition which can be either an object property name or a PHP callable returning the corresponding field value. The signature of the callable should be:
function ($model, $field) {
// return field value
}
For example, the following code declares four fields:
email: the field name is the same as the property nameemail;firstNameandlastName: the field names arefirstNameandlastName, and their values are obtained from thefirst_nameandlast_nameproperties;fullName: the field name isfullName. Its value is obtained by concatenatingfirst_nameandlast_name.
return [
'email',
'firstName' => 'first_name',
'lastName' => 'last_name',
'fullName' => function ($model) {
return $model->first_name . ' ' . $model->last_name;
},
];
See also toArray().
| public abstract array fields ( ) | ||
| return | array |
The list of field names or field definitions. |
|---|---|---|
public function fields(): array;
Converts the object into an array.
| public abstract array toArray ( string[] $fields = [], string[] $expand = [], boolean $recursive = true ) | ||
| $fields | string[] |
The fields that the output array should contain. Fields not specified
in {@see \ |
| $expand | string[] |
The additional fields that the output array should contain.
Fields not specified in {@see \ |
| $recursive | boolean |
Whether to recursively return array representation of embedded objects. |
| return | array |
The array representation of the object. |
|---|---|---|
public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array;
User Contributed Notes
Leave a comment
Join the conversation to share a note.