<?php
/**
 * Created by PhpStorm.
 * User: Leo
 * Date: 2017/11/12
 * Time: 下午3:59
 */

namespace common\components;

use common\helpers\Validator;
use yii\base\InvalidConfigException;

class Model extends \yii\base\Model {

    use \common\traits\ModelTrait;

    /**
     * @var \common\libs\logging\operate\AdminOperate
     */
    protected $adminOperateLogger;
    /**
     * @var \common\libs\logging\operate\UserOperate
     */
    protected $userOperateLogger;
    protected $systemLogger;

    /**
     * 扩展父类方法用于校验表单
     * @return \ArrayObject
     * @throws InvalidConfigException
     */
    public function createValidators()
    {
        $validators = new \ArrayObject();
        foreach ($this->rules() as $rule) {
            if ($rule instanceof \yii\validators\Validator) {
                $validators->append($rule);
            } elseif (is_array($rule) && isset($rule[0], $rule[1])) { // attributes, validator type
                if(array_key_exists($rule[1], Validator::$extensionRule)){
                    // 启用扩展规则
                    $otherParams = array_merge(['validateMethod'=>$rule[1]], array_slice($rule, 2));
                    $validator = \yii\validators\Validator::createValidator('\common\helpers\Validator', $this, (array) $rule[0], $otherParams);
                } else {
                    $validator = \yii\validators\Validator::createValidator($rule[1], $this, (array) $rule[0], array_slice($rule, 2));
                }
                $validators->append($validator);
            } else {
                throw new InvalidConfigException('Invalid validation rule: a rule must specify both attribute names and validator type.');
            }
        }
        return $validators;
    }

}