Take a look at vendor/yiisoft/yii2/base/arrayabletrait today.
<?PHP/** * @linkhttp://www.yiiframework.com/* @copyright Copyright (c) Yii software LLC * @licensehttp://www.yiiframework.com/license/ */namespaceYii\Base; use yii;use yii\helpers\arrayhelper;use yii\web\link;use yii\web\linkable; //traits is a code reuse mechanism that is prepared for PHP-like single-inheritance languages. //trait to reduce the limitations of single-inheritance languages, developers are free to reuse the set of methods in separate classes within different hierarchies. //the semantics of traits and class combinations are defined as a way to reduce complexity and avoid typical problems associated with traditional multi-inheritance and mixed-Class (Mixin). //trait if you want to use the trait in Class, use the Using keywordTrait arrayabletrait{/** * The returned field list should return the default [[ToArray ()]] when no specific field is specified. * A field is an array element called return [[ToArray ()]] * * * This method should return an array field name and field definition * If the latter, the array key should be the field name, and the array value should * corresponding field definition can be an object's property name or a P The HP call * returns the corresponding field value * as the field values. If the latter, the array key should is the field name while the array value should is * the corresponding field Defini tion which can be either an object property name or a PHP callable * returning the corresponding field value. The signature of the callable should be: * @see ToArray ()*/ Publicfunction fields () {//Gets the list of names of public member variables for this object, assigned to $fields$fields = Array_keys (Yii::getobjectvars ($ This)); //array_combine-creates an array with the value of an array as its key name, and the value of another array as its value//returns the array, keys and values are all $fields returnarray_combine ($fields, $fields); } //returns a list of fields that can be further expanded to return the [[ToArray ()]]. //This method is similar to [[Field ()]] except for the list of fields returned. Publicfunction Extrafields () {return []; } //converts the model to an array. //The method first determines which fields are included in the generated array by calling [[Resolvefields ()]]. //It will convert the model of these fields into an array. If the dollar is true,//any embedded object will also be converted to an array//If the model implements a [[link]] interface, the resulting array will also "_link" the element//refers to the interface specified by a list of links Publicfunction ToArray (Array $fields = [], array $expand = [], $recursive =true) {$data= []; foreach($ This->resolvefields ($fields, $expand) as$field =$definition) { //if it is a string, it returns the property of the current object, otherwise calls Call_user_func to execute the $definition function$data [$field] = is_string ($definition)? $ This$definition: Call_user_func ($definition, $ This, $field); } if($ Thisinstanceof linkable) {$data ['_links'] = Link::serialize ($ This-getlinks ()); } return$recursive?Arrayhelper::toarray ($data): $data; } /** * Determines which fields can is returned by [[ToArray ()]]. * Decide which fields are returned by ToArray () * This method will check the requested fields against those declared in [[[Fields]] ' an d [[Extrafields ()]] * To determine which, can be returned. * This method checks the fields declared in the Requested field [[Fields ()]] and [[Extrafields ()]) to determine which fields can be recovered. * @param array $fields The field being requested for exporting * is requested. * @param array $expand the additional field being requested for exporting * expanded export of requested other fields * @return array the LIS T of fields to be exported. The array keys are the field names, and the array values * is the corresponding object property names or PHP callable s returning the field values. * The list of exported fields. The name of the key field for the array, the name of the object property that corresponds to the array value, or the field value returned by the PHP call. */ protectedfunction Resolvefields (array $fields, array $expand) {$result= []; //The fields obtained in the cyclic $this->fields () foreach($ This->fields () as$field =$definition) { if(Is_integer ($field)) {//if the $field is int, the $definition is assigned to $field$field =$definition; } if(Empty ($fields) | | In_array ($field, $fields,true)) { //if the $fields is empty, or if $field is in the $fields, the $definition is assigned to the $result//that is, the $fields is empty, and all of the object's properties are put into the result//when not empty, if the property of the current object exists in the $fields, the value of the property defined in the object is placed in the result$result [$field] =$definition; } } if(Empty ($expand)) {return$result; } //The fields obtained in the cyclic $this->extrafields () foreach($ This->extrafields () as$field =$definition) { if(Is_integer ($field)) {//if the $field is int, the $definition is assigned to $field$field =$definition; } if(In_array ($field, $expand,true)) { //if the $field is in the $expand, the $definition is assigned to the $result//that is, the extended property of the current object exists in the $fields, and the value of the extended property defined in the object is placed in the result$result [$field] =$definition; } } return$result; }}
Yii2 Framework Essay 34