1 | <?php |
||
24 | trait ArrayableTrait |
||
25 | { |
||
26 | /** |
||
27 | * Returns the list of fields that should be returned by default by [[toArray()]] when no specific fields are specified. |
||
28 | * |
||
29 | * A field is a named element in the returned array by [[toArray()]]. |
||
30 | * |
||
31 | * This method should return an array of field names or field definitions. |
||
32 | * If the former, the field name will be treated as an object property name whose value will be used |
||
33 | * as the field value. If the latter, the array key should be the field name while the array value should be |
||
34 | * the corresponding field definition which can be either an object property name or a PHP callable |
||
35 | * returning the corresponding field value. The signature of the callable should be: |
||
36 | * |
||
37 | * ```php |
||
38 | * function ($model, $field) { |
||
39 | * // return field value |
||
40 | * } |
||
41 | * ``` |
||
42 | * |
||
43 | * For example, the following code declares four fields: |
||
44 | * |
||
45 | * - `email`: the field name is the same as the property name `email`; |
||
46 | * - `firstName` and `lastName`: the field names are `firstName` and `lastName`, and their |
||
47 | * values are obtained from the `first_name` and `last_name` properties; |
||
48 | * - `fullName`: the field name is `fullName`. Its value is obtained by concatenating `first_name` |
||
49 | * and `last_name`. |
||
50 | * |
||
51 | * ```php |
||
52 | * return [ |
||
53 | * 'email', |
||
54 | * 'firstName' => 'first_name', |
||
55 | * 'lastName' => 'last_name', |
||
56 | * 'fullName' => function () { |
||
57 | * return $this->first_name . ' ' . $this->last_name; |
||
58 | * }, |
||
59 | * ]; |
||
60 | * ``` |
||
61 | * |
||
62 | * In this method, you may also want to return different lists of fields based on some context |
||
63 | * information. For example, depending on the privilege of the current application user, |
||
64 | * you may return different sets of visible fields or filter out some fields. |
||
65 | * |
||
66 | * The default implementation of this method returns the public object member variables indexed by themselves. |
||
67 | * |
||
68 | * @return array the list of field names or field definitions. |
||
69 | * @see toArray() |
||
70 | */ |
||
71 | 1 | public function fields() |
|
76 | |||
77 | /** |
||
78 | * Returns the list of fields that can be expanded further and returned by [[toArray()]]. |
||
79 | * |
||
80 | * This method is similar to [[fields()]] except that the list of fields returned |
||
81 | * by this method are not returned by default by [[toArray()]]. Only when field names |
||
82 | * to be expanded are explicitly specified when calling [[toArray()]], will their values |
||
83 | * be exported. |
||
84 | * |
||
85 | * The default implementation returns an empty array. |
||
86 | * |
||
87 | * You may override this method to return a list of expandable fields based on some context information |
||
88 | * (e.g. the current application user). |
||
89 | * |
||
90 | * @return array the list of expandable field names or field definitions. Please refer |
||
91 | * to [[fields()]] on the format of the return value. |
||
92 | * @see toArray() |
||
93 | * @see fields() |
||
94 | */ |
||
95 | public function extraFields() |
||
99 | |||
100 | /** |
||
101 | * Converts the model into an array. |
||
102 | * |
||
103 | * This method will first identify which fields to be included in the resulting array by calling [[resolveFields()]]. |
||
104 | * It will then turn the model into an array with these fields. If `$recursive` is true, |
||
105 | * any embedded objects will also be converted into arrays. |
||
106 | * When embeded objects are [[Arrayable]], their respective nested fields will be extracted and passed to [[toArray()]]. |
||
107 | * |
||
108 | * If the model implements the [[Linkable]] interface, the resulting array will also have a `_link` element |
||
109 | * which refers to a list of links as specified by the interface. |
||
110 | * |
||
111 | * @param array $fields the fields being requested. |
||
112 | * If empty or if it contains '*', all fields as specified by [[fields()]] will be returned. |
||
113 | * Fields can be nested, separated with dots (.). e.g.: item.field.sub-field |
||
114 | * `$recursive` must be true for nested fields to be extracted. If `$recursive` is false, only the root fields will be extracted. |
||
115 | * @param array $expand the additional fields being requested for exporting. Only fields declared in [[extraFields()]] |
||
116 | * will be considered. |
||
117 | * Expand can also be nested, separated with dots (.). e.g.: item.expand1.expand2 |
||
118 | * `$recursive` must be true for nested expands to be extracted. If `$recursive` is false, only the root expands will be extracted. |
||
119 | * @param bool $recursive whether to recursively return array representation of embedded objects. |
||
120 | * @return array the array representation of the object |
||
121 | */ |
||
122 | 6 | public function toArray(array $fields = [], array $expand = [], $recursive = true) |
|
123 | { |
||
124 | 6 | $data = []; |
|
125 | 6 | foreach ($this->resolveFields($fields, $expand) as $field => $definition) { |
|
126 | 6 | $attribute = is_string($definition) ? $this->$definition : call_user_func($definition, $this, $field); |
|
127 | |||
128 | 6 | if ($recursive) { |
|
129 | 6 | $nestedFields = $this->extractFieldsFor($fields, $field); |
|
130 | 6 | $nestedExpand = $this->extractFieldsFor($expand, $field); |
|
131 | 6 | if ($attribute instanceof Arrayable) { |
|
132 | 2 | $attribute = $attribute->toArray($nestedFields, $nestedExpand); |
|
133 | 6 | } elseif (is_array($attribute)) { |
|
134 | 1 | $attribute = array_map( |
|
135 | 1 | function ($item) use ($nestedFields, $nestedExpand) { |
|
136 | 1 | if ($item instanceof Arrayable) { |
|
137 | 1 | return $item->toArray($nestedFields, $nestedExpand); |
|
138 | } else { |
||
139 | return $item; |
||
140 | } |
||
141 | 1 | }, |
|
142 | 1 | $attribute |
|
143 | ); |
||
144 | } |
||
145 | } |
||
146 | 6 | $data[$field] = $attribute; |
|
147 | } |
||
148 | |||
149 | 6 | if ($this instanceof Linkable) { |
|
150 | $data['_links'] = Link::serialize($this->getLinks()); |
||
151 | } |
||
152 | |||
153 | 6 | return $recursive ? ArrayHelper::toArray($data) : $data; |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * Extracts the root field names from nested fields. |
||
158 | * Nested fields are separated with dots (.). e.g: "item.id" |
||
159 | * The previous example would extract "item". |
||
160 | * |
||
161 | * @param array $fields The fields requested for extraction |
||
162 | * @return array root fields extracted from the given nested fields |
||
163 | * @since 2.0.14 |
||
164 | */ |
||
165 | 6 | protected function extractRootFields(array $fields) |
|
166 | { |
||
167 | 6 | $result = []; |
|
168 | |||
169 | 6 | foreach ($fields as $field) { |
|
170 | 3 | $result[] = current(explode(".", $field, 2)); |
|
171 | } |
||
172 | |||
173 | 6 | if (in_array('*', $result, true)) { |
|
174 | 1 | $result = []; |
|
175 | } |
||
176 | |||
177 | 6 | return array_unique($result); |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * Extract nested fields from a fields collection for a given root field |
||
182 | * Nested fields are separated with dots (.). e.g: "item.id" |
||
183 | * The previous example would extract "id". |
||
184 | * |
||
185 | * @param array $fields The fields requested for extraction |
||
186 | * @param string $rootField The root field for which we want to extract the nested fields |
||
187 | * @return array nested fields extracted for the given field |
||
188 | * @since 2.0.14 |
||
189 | */ |
||
190 | 6 | protected function extractFieldsFor(array $fields, $rootField) |
|
202 | |||
203 | /** |
||
204 | * Determines which fields can be returned by [[toArray()]]. |
||
205 | * This method will first extract the root fields from the given fields. |
||
206 | * Then it will check the requested root fields against those declared in [[fields()]] and [[extraFields()]] |
||
207 | * to determine which fields can be returned. |
||
208 | * @param array $fields the fields being requested for exporting |
||
209 | * @param array $expand the additional fields being requested for exporting |
||
210 | * @return array the list of fields to be exported. The array keys are the field names, and the array values |
||
211 | * are the corresponding object property names or PHP callables returning the field values. |
||
212 | */ |
||
213 | 6 | protected function resolveFields(array $fields, array $expand) |
|
243 | } |
||
244 |