7
7
use Closure ;
8
8
use Yiisoft \ActiveRecord \AbstractActiveRecord ;
9
9
use Yiisoft \ActiveRecord \ActiveRecordInterface ;
10
+ use Yiisoft \Arrays \ArrayableInterface ;
10
11
11
12
use function array_combine ;
12
13
use function array_keys ;
19
20
*
20
21
* @method array relatedRecords()
21
22
* @see AbstractActiveRecord::relatedRecords()
23
+ *
24
+ * @psalm-import-type FieldsArray from ArrayableInterface
22
25
*/
23
26
trait ArrayableTrait
24
27
{
25
28
use \Yiisoft \Arrays \ArrayableTrait;
26
29
27
30
/**
28
- * @return array The default implementation returns the names of the relations that have been populated into this
29
- * record.
31
+ * Returns the list of fields that can be expanded further and returned by {@see toArray()}.
32
+ *
33
+ * This method is similar to {@see fields()} except that the list of fields returned by this method are not returned
34
+ * by default by {@see toArray()}. Only when field names to be expanded are explicitly specified when calling
35
+ * {@see toArray()}, will their values be exported.
36
+ *
37
+ * The default implementation returns the names of the relations defined in this `ActiveRecord` class indexed by
38
+ * themselves.
39
+ *
40
+ * You may override this method to return a list of expandable fields.
41
+ *
42
+ * @return (string|Closure)[] The list of expandable field names or field definitions. Please refer
43
+ * to {@see fields()} on the format of the return value.
44
+ *
45
+ * @psalm-return FieldsArray
30
46
*/
31
47
public function extraFields (): array
32
48
{
@@ -36,7 +52,50 @@ public function extraFields(): array
36
52
}
37
53
38
54
/**
39
- * @psalm-return array<string, string|Closure>
55
+ * Returns the list of fields that should be returned by default by {@see toArray()}.
56
+ *
57
+ * A field is a named element in the returned array by {@see toArray()}.
58
+ *
59
+ * This method should return an array of field names or field definitions.
60
+ * If the former, the field name will be treated as an object property name whose value will be used
61
+ * as the field value. If the latter, the array key should be the field name while the array value should be
62
+ * the corresponding field definition, which can be either an object property name or a PHP callable
63
+ * returning the corresponding field value. The signature of the callable should be:
64
+ *
65
+ * ```php
66
+ * function ($model, $field) {
67
+ * // return field value
68
+ * }
69
+ * ```
70
+ *
71
+ * For example, the following code declares four fields:
72
+ *
73
+ * - `email`: the field name is the same as the property name `email`;
74
+ * - `firstName` and `lastName`: the field names are `firstName` and `lastName`, and their
75
+ * values are obtained from the `first_name` and `last_name` properties;
76
+ * - `fullName`: the field name is `fullName`. Its value is obtained by concatenating `first_name`
77
+ * and `last_name`.
78
+ *
79
+ * ```php
80
+ * return [
81
+ * 'email',
82
+ * 'firstName' => 'first_name',
83
+ * 'lastName' => 'last_name',
84
+ * 'fullName' => function () {
85
+ * return $this->first_name . ' ' . $this->last_name;
86
+ * },
87
+ * ];
88
+ * ```
89
+ *
90
+ * In this method, you may also want to return different lists of fields based on some context
91
+ * information. For example, depending on the privilege of the current application user,
92
+ * you may return different sets of visible fields or filter out some fields.
93
+ *
94
+ * The default implementation returns the names of the properties of this record indexed by themselves.
95
+ *
96
+ * @return (string|Closure)[] The list of field names or field definitions.
97
+ *
98
+ * @psalm-return FieldsArray
40
99
*/
41
100
public function fields (): array
42
101
{
0 commit comments