HumHub Documentation (unofficial)

ArrayableTrait

ArrayableTrait provides a common implementation of the [[Arrayable]] interface.

ArrayableTrait implements [[toArray()]] by respecting the field definitions as declared in [[fields()]] and [[extraFields()]].

Tags
author

Qiang Xue qiang.xue@gmail.com

since
2.0

Table of Contents

Methods

extraFields()  : array<string|int, mixed>
Returns the list of fields that can be expanded further and returned by [[toArray()]].
fields()  : array<string|int, mixed>
Returns the list of fields that should be returned by default by [[toArray()]] when no specific fields are specified.
toArray()  : array<string|int, mixed>
Converts the model into an array.
extractFieldsFor()  : array<string|int, mixed>
Extract nested fields from a fields collection for a given root field Nested fields are separated with dots (.). e.g: "item.id" The previous example would extract "id".
extractRootFields()  : array<string|int, mixed>
Extracts the root field names from nested fields.
resolveFields()  : array<string|int, mixed>
Determines which fields can be returned by [[toArray()]].

Methods

extraFields()

Returns the list of fields that can be expanded further and returned by [[toArray()]].

public extraFields() : array<string|int, mixed>

This method is similar to [[fields()]] except that the list of fields returned by this method are not returned by default by [[toArray()]]. Only when field names to be expanded are explicitly specified when calling [[toArray()]], will their values be exported.

The default implementation returns an empty array.

You may override this method to return a list of expandable fields based on some context information (e.g. the current application user).

Tags
see
toArray()
see
fields()
Return values
array<string|int, mixed>

the list of expandable field names or field definitions. Please refer to [[fields()]] on the format of the return value.

fields()

Returns the list of fields that should be returned by default by [[toArray()]] when no specific fields are specified.

public fields() : array<string|int, mixed>

A field is a named element in the returned array by [[toArray()]].

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 name email;
  • firstName and lastName: the field names are firstName and lastName, and their values are obtained from the first_name and last_name properties;
  • fullName: the field name is fullName. Its value is obtained by concatenating first_name and last_name.
return [
    'email',
    'firstName' => 'first_name',
    'lastName' => 'last_name',
    'fullName' => function () {
        return $this->first_name . ' ' . $this->last_name;
    },
];

In this method, you may also want to return different lists of fields based on some context information. For example, depending on the privilege of the current application user, you may return different sets of visible fields or filter out some fields.

The default implementation of this method returns the public object member variables indexed by themselves.

Tags
see
toArray()
Return values
array<string|int, mixed>

the list of field names or field definitions.

toArray()

Converts the model into an array.

public toArray([array<string|int, mixed> $fields = [] ][, array<string|int, mixed> $expand = [] ][, bool $recursive = true ]) : array<string|int, mixed>

This method will first identify which fields to be included in the resulting array by calling [[resolveFields()]]. It will then turn the model into an array with these fields. If $recursive is true, any embedded objects will also be converted into arrays. When embedded objects are [[Arrayable]], their respective nested fields will be extracted and passed to [[toArray()]].

If the model implements the [[Linkable]] interface, the resulting array will also have a _link element which refers to a list of links as specified by the interface.

Parameters
$fields : array<string|int, mixed> = []

the fields being requested. If empty or if it contains '*', all fields as specified by [[fields()]] will be returned. Fields can be nested, separated with dots (.). e.g.: item.field.sub-field $recursive must be true for nested fields to be extracted. If $recursive is false, only the root fields will be extracted.

$expand : array<string|int, mixed> = []

the additional fields being requested for exporting. Only fields declared in [[extraFields()]] will be considered. Expand can also be nested, separated with dots (.). e.g.: item.expand1.expand2 $recursive must be true for nested expands to be extracted. If $recursive is false, only the root expands will be extracted.

$recursive : bool = true

whether to recursively return array representation of embedded objects.

Return values
array<string|int, mixed>

the array representation of the object

extractFieldsFor()

Extract nested fields from a fields collection for a given root field Nested fields are separated with dots (.). e.g: "item.id" The previous example would extract "id".

protected extractFieldsFor(array<string|int, mixed> $fields, string $rootField) : array<string|int, mixed>
Parameters
$fields : array<string|int, mixed>

The fields requested for extraction

$rootField : string

The root field for which we want to extract the nested fields

Tags
since
2.0.14
Return values
array<string|int, mixed>

nested fields extracted for the given field

extractRootFields()

Extracts the root field names from nested fields.

protected extractRootFields(array<string|int, mixed> $fields) : array<string|int, mixed>

Nested fields are separated with dots (.). e.g: "item.id" The previous example would extract "item".

Parameters
$fields : array<string|int, mixed>

The fields requested for extraction

Tags
since
2.0.14
Return values
array<string|int, mixed>

root fields extracted from the given nested fields

resolveFields()

Determines which fields can be returned by [[toArray()]].

protected resolveFields(array<string|int, mixed> $fields, array<string|int, mixed> $expand) : array<string|int, mixed>

This method will first extract the root fields from the given fields. Then it will check the requested root fields against those declared in [[fields()]] and [[extraFields()]] to determine which fields can be returned.

Parameters
$fields : array<string|int, mixed>

the fields being requested for exporting

$expand : array<string|int, mixed>

the additional fields being requested for exporting

Return values
array<string|int, mixed>

the list of fields to be exported. The array keys are the field names, and the array values are the corresponding object property names or PHP callables returning the field values.

Loading…
On this page

Search results