zoukankan      html  css  js  c++  java
  • 学习yii2.0框架阅读代码(六)

    vendor/yiisoft/yii2/base/ArrayableTrait.

    <?php
    /**
     * @link http://www.yiiframework.com/
     * @copyright Copyright (c) 2008 Yii Software LLC
     * @license http://www.yiiframework.com/license/
     */
    
    namespace yiiase;
    
    use Yii;
    use yiihelpersArrayHelper;
    use yiiwebLink;
    use yiiwebLinkable;
    
       
       //traits 是一种为类似 PHP 的单继承语言而准备的代码复用机制。
       //trait 为了减少单继承语言的限制,使开发人员能够自由地在不同层次结构内独立的类中复用方法集。
       //traits 和类组合的语义是定义了一种方式来减少复杂性,避免传统多继承和混入类(Mixin)相关的典型问题。
       //trait 如果要在 Class 中使用该 Trait,那么使用 use 关键字
    trait ArrayableTrait
    {
        
        /**
         * 返回的字段列表应该返回默认的[[toArray()]]当没有指定特定的字段。
         *
         * 一个字段是一个名叫返回的数组元素的[[toArray()]]
         *
         * *此方法应该返回一个数组字段名和字段定义
         * 如果是后者,数组键应该字段名,而数组值应该*对应的字段定义可以是一个对象的属性名或一个PHP调用*返回相应的字段值
         * 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:
         * @see toArray()
         */
        public function fields()
        {
            // 获取该对象的 public 成员变量的名列表,赋给 $fields
            $fields = array_keys(Yii::getObjectVars($this));
            // array_combine — 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值
            // 返回数组, keys 和 values 都是 $fields
            return array_combine($fields, $fields);
        }
    
       //返回字段的列表,可以进一步扩大,返回的[[toArray()]]。
       //这种方法类似于[[字段()]]除了返回的字段列表。 
        public function extraFields()
        {
            return [];
        }
        //将模型转换为一个数组。
        //该方法首先确定哪些字段被包括在生成的数组通过调用[[resolveFields()]]。
        //它将把这些字段的模型转换为一个数组。如果美元递归的是真的,  
        //任何嵌入对象也将转化为数组  
        //如果模型实现了[[链接]]接口,生成的数组也会“_link”元素 
        //指一个链接列表所指定的接口
    
        public function toArray(array $fields = [], array $expand = [], $recursive = true)
        {
            $data = [];
            foreach ($this->resolveFields($fields, $expand) as $field => $definition) {
                // 如果是 string, 就返回当前对象的该属性, 否则调用 call_user_func 去执行 $definition 函数
                $data[$field] = is_string($definition) ? $this->$definition : call_user_func($definition, $this, $field);
            }
    
            if ($this instanceof Linkable) {
                $data['_links'] = Link::serialize($this->getLinks());
            }
    
            return $recursive ? ArrayHelper::toArray($data) : $data;
        }
    
        /**
         * Determines which fields can be returned by [[toArray()]].
         * 决定哪些 fields 会通过 toArray() 返回
         * This method will check the requested fields against those declared in [[fields()]] and [[extraFields()]]
         * to determine which fields can be returned.
         * @param array $fields the fields being requested for exporting
         * @param array $expand the additional fields being requested for exporting
         * @return array 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.
         */
        protected function resolveFields(array $fields, array $expand)
        {
            $result = [];
    
            // 循环 $this->fields() 中取得的 fields
            foreach ($this->fields() as $field => $definition) {
                if (is_integer($field)) {
                    // 如果 $field 是 int, 就将 $definition 赋值给 $field
                    $field = $definition;
                }
                if (empty($fields) || in_array($field, $fields, true)) {
                    // 如果 $fields 为空, 或者 $field 在 $fields 中, 就将 $definition 赋到 $result 中
                    // 即 $fields 为空,就将所有的对象的属性都放入到结果中
                    // 不为空时,如果当前对象的属性在 $fields 中存在, 就将对象中定义的该属性的值放入到结果中
                    $result[$field] = $definition;
                }
            }
    
            if (empty($expand)) {
                return $result;
            }
    
            // 循环 $this->extraFields() 中取得的 fields
            foreach ($this->extraFields() as $field => $definition) {
                if (is_integer($field)) {
                    // 如果 $field 是 int, 就将 $definition 赋值给 $field
                    $field = $definition;
                }
                if (in_array($field, $expand, true)) {
                    // 如果$field 在 $expand 中, 就将 $definition 赋到 $result 中
                    // 即当前对象的扩展属性在 $fields 中存在, 就将对象中定义的该扩展属性的值放入到结果中
                    $result[$field] = $definition;
                }
            }
    
            return $result;
        }
    }

    php

  • 相关阅读:
    DBA_VMware虚拟机安装和简介(案例)
    DBA_Oracle基本体系内存和进程结构(概念)
    IREP_SOA Integration SOAP概述(概念)
    IREP_SOA Integration WSDL概述(概念)
    IREP_SOA Integration程序注释语法Annotations(概念)
    DBA_Oracle日志文件
    BPEL_Oracle BPEL新一代工作流介绍(概念)
    DBA_Oracle基本体系架构(概念)
    DBA_Tablespace表空间的概念和管控(概念)
    WebADI_Oracle ERP R12使用前WebADI设定(案例)
  • 原文地址:https://www.cnblogs.com/xwzj/p/5402382.html
Copyright © 2011-2022 走看看