学习yii2.0框架阅读代码(九)

vendor/yiisoft/yii2/base/Model.php(完)

    /**
     * 该方法将验证每一个模型,可以验证多个模型.
     * be of the same or different types.
     * @param array $models the models to be validated
     * @param array $attributeNames list of attribute names that should be validated.
     * 如果该参数为空,这意味着任何属性中列出的适用
     * 需要验证规则
     * @return 判断所有的模型是有效的。如果一个错误将返回false
     */
    public static function validateMultiple($models, $attributeNames = null)
    {
        $valid = true;
        /* @var $model Model */
        foreach ($models as $model) {
            $valid = $model->validate($attributeNames) && $valid;
        }

        return $valid;
    }

    /**
     * 当没有指定特定的字段,返回的字段列表应该返回默认的[[toArray()]]
     * 此方法应该返回一个数组字段名和定义的字段
     * 如果是前者,字段名称将被视为一个对象属性名称的值将被使用
     * 如果是后者,数组的键应该是在数组的值应该是字段名
     * 相应的字段定义可以是一个对象的属性名或一个PHP调用
     *
     * ```php
     * function ($field, $model) {
     *     // return field value
     * }
     * ```
     *
     * 例如,下面的代码声明四个属性:
     * ```php
     * return [
     *     'email',
     *     'firstName' => 'first_name',
     *     'lastName' => 'last_name',
     *     'fullName' => function ($model) {
     *         return $model->first_name . ' ' . $model->last_name;
     *     },
     * ];
     * 这个方法返回的默认实现[[attributes()]]相同的属性名
     * @return 数组列表字段名和定义的字段.
     * @see toArray()
     */
    public function fields()
    {
        $fields = $this->attributes();
        // array_combine — 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值
        return array_combine($fields, $fields);
    }

    /**
     * 返回一个迭代器遍历属性的模型.
     * 该方法需要一个IteratorAggregate接口.
     * @return ArrayIterator迭代器遍历列表中的项目.
     */
    public function getIterator()
    {
        // 获取该 model 的所有属性
        $attributes = $this->getAttributes();
        // ArrayIterator 这个迭代器允许在遍历数组和对象时删除和更新值与键
        // 当你想多次遍历相同数组时你需要实例化 ArrayObject,然后让这个实例创建一个 ArrayIteratror 实例, 然后使用 foreach 或者 手动调用 getIterator() 方法
        return new ArrayIterator($attributes);
    }

    /**
     * 返回是否有指定偏移位置的一个元素.
     * 该方法所需的SPL ArrayAccess接口
     * It is implicitly called when you use something like `isset($model[$offset])`.
     * @param mixed $offset the offset to check on
     * @return boolean
     */
    public function offsetExists($offset)
    {
        // 将 isset($model[$offset]) 重写为 isset($model->$offset)
        return $this->$offset !== null;
    }

    /**
     * 返回指定偏移位置的元素.
     * 该方法所需的SPL ArrayAccess接口
     * It is implicitly called when you use something like `$value = $model[$offset];`.
     * @param mixed $offset the offset to retrieve element.
     * @return mixed the element at the offset, null if no element is found at the offset
     */
    public function offsetGet($offset)
    {
        // 将获取 $model[$offset] 重写为 $model->$offset
        return $this->$offset;
    }

    /**
     * offset设置指定偏移位置的元素
     * @param integer $offset the offset to set element
     * @param mixed $item the element value
     */
    public function offsetSet($offset, $item)
    {
        // 将 $model[$offset] = $item 重写为 $model->$offset = $item
        $this->$offset = $item;
    }

    /**
     * 设置指定偏移位置的元素值为null.
     * 该方法所需的SPL ArrayAccess接口
     * @param mixed $offset the offset to unset element
     */
    public function offsetUnset($offset)
    {
        // 将 unset($model[$offset]) 重写为 $model->$offset = null
        $this->$offset = null;
    }
}
原文地址:https://www.cnblogs.com/xwzj/p/5406390.html