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 | 3 | public function toArray(array $fields = [], array $expand = [], $recursive = true) |
|
154 | |||
155 | /** |
||
156 | * Extracts the root field names from nested fields. |
||
157 | * Nested fields are separated with dots (.). e.g: "item.id" |
||
158 | * The previous example would extract "item". |
||
159 | * |
||
160 | * @param array $fields The fields requested for extraction |
||
161 | * @return array root fields extracted from the given nested fields |
||
162 | * @since 2.0.14 |
||
163 | */ |
||
164 | 3 | protected function extractRootFields(array $fields) |
|
178 | |||
179 | /** |
||
180 | * Extract nested fields from a fields collection for a given root field |
||
181 | * Nested fields are separated with dots (.). e.g: "item.id" |
||
182 | * The previous example would extract "id". |
||
183 | * |
||
184 | * @param array $fields The fields requested for extraction |
||
185 | * @param string $rootField The root field for which we want to extract the nested fields |
||
186 | * @return array nested fields extracted for the given field |
||
187 | * @since 2.0.14 |
||
188 | */ |
||
189 | 3 | protected function extractFieldsFor(array $fields, $rootField) |
|
201 | |||
202 | /** |
||
203 | * Determines which fields can be returned by [[toArray()]]. |
||
204 | * This method will first extract the root fields from the given fields. |
||
205 | * Then it will check the requested root fields against those declared in [[fields()]] and [[extraFields()]] |
||
206 | * to determine which fields can be returned. |
||
207 | * @param array $fields the fields being requested for exporting |
||
208 | * @param array $expand the additional fields being requested for exporting |
||
209 | * @return array the list of fields to be exported. The array keys are the field names, and the array values |
||
210 | * are the corresponding object property names or PHP callables returning the field values. |
||
211 | */ |
||
212 | 3 | protected function resolveFields(array $fields, array $expand) |
|
242 | } |
||
243 |