验证表单令牌
@access public
@param  mixed $value 字段值
@param  mixed $rule 验证规则
@param  array $data 数据
@return bool
181950
public function token($value, $rule, $data) {
        $rule = !empty($rule) ? $rule : '__token__';
        $session = Container::get('session');

        if (!isset($data[$rule]) || !$session->has($rule)) {
            // 令牌数据无效
            return false;
        }

        // 令牌验证
        if (isset($data[$rule]) && $session->get($rule) === $data[$rule]) {
            // 防止重复提交
            $session->delete($rule); // 验证完成销毁session
            return true;
        }

        // 开启TOKEN重置
        $session->delete($rule);

        return false;
    }
758771
public function setRelation($name, $value, $data = [])
    {
        // 检测修改器
        $method = 'set' . Db::parseName($name, 1) . 'Attr';

        if (method_exists($this, $method)) {
            $value = $this->$method($value, array_merge($this->data, $data));
        }

        $this->relation[$name] = $value;

        return $this;
    }
1233563
public function parse($value, $rule) {
        $this->formatEnumRule($rule);

        $this->formatEnumValue($value, $rule);

        return $value;
    }
157754
protected function regex($value, $rule)
    {
        if (isset($this->regex[$rule])) {
            $rule=$this->regex[$rule];
        }
        if (0 !== strpos($rule, '/') && !preg_match('/\/[imsU]{0,4}$/', $rule)) {
            // 不是正则表达式则两端补上/
            $rule='/^'.$rule.'$/';
        }
        return 1 === preg_match($rule, (string) $value);
    }
1220005
public static function checkCompareValues($rules, $value)
    {
        foreach(is_array($rules) ? $rules : [$rules] as $rule)
        {
            if(!static::checkCompareValue($rule, $value))
            {
                return false;
            }
        }
        return true;
    }
388412
public function apply(Rule $rule, $value)
    {
        //Build pattern
        $pattern = ($this->checkUnicodeSupport() && $rule->unicodePattern !== null)
            ? $rule->unicodePattern
            : $rule->pattern;

        return preg_replace($pattern, '', $value);
    }
351915
public function validate(array $data, array $rules, $firstErrBreak = true)
	{
		$this->errors = [];

		foreach ($rules as $field => $fieldRules) {
            // 待验证字符串
            $value = @$data[$field];

            if (isset($fieldRules['message'])) {
                // 字段的验证规则为一维数组
                if (false === $this->validateItem($value, $fieldRules)) {
                    $this->errors[] = $fieldRules['message'];
                    if ($firstErrBreak) {
                        return false;
                    }
                }
            } else {
                // 字段的验证规则为二维数组
                foreach ($fieldRules as $ruleItem) {
                    if (false === $this->validateItem($value, $ruleItem)) {
                        $this->errors[] = @$ruleItem['message'];
                        if ($firstErrBreak) {
                            return false;
                        }
                    }
                }
            }
        }

		return empty($this->errors);
	}
1143163
public function applyFilters($value, $rules = array())
    {
        $valueTO = $this->valueTO->forge($value);
        // loop through all the rules to validate $value.
        foreach ($rules as $rule => $parameter) {
            // skip rules with option as FALSE.
            if ($parameter === false) {
                continue;
            }
            $this->filter->apply($rule, $valueTO, $parameter);

            // loop break.
            if ($valueTO->getBreak()) {
                break;
            }
        }

        return $valueTO;
    }
544553
public function process(array $rules, array $data) {
        
        // If conditionning
        $this->rules[] = new Rule(
            'if_boolean', 
            'if:(\w+)', 
            '<?php if (isset($this->data[\'$1\']) && $this->data[\'$1\']): ?>'
        );
        $this->rules[] = new Rule(
        		'if_condition',
        		'if:(\w+)([!<>=]+)(\w+)',
        		'<?php if (isset($this->data[\'$1\'])){$base = $this->data[\'$1\'];}else{$base = \'$1\';};
        		if (isset($this->data[\'$3\'])){$value = $this->data[\'$3\'];}else{$value = \'$3\';}?>
        		<?php if ($base $2 $value): ?>'
        );
        $this->rules[] = new Rule(
            'ifnot', 
            'ifnot:(\w+)', 
            '<?php if (!isset($this->data[\'$1\']) || !$this->data[\'$1\']): ?>'
        );
        $this->rules[] = new Rule(
            'else', 
            'else', 
            '<?php else: ?>'
        );
        $this->rules[] = new Rule(
            'elseif_boolean', 
            'else:(\w+)', 
            '<?php elseif (isset($this->data[\'$1\']) && $this->data[\'$1\']): ?>'
        );
        $this->rules[] = new Rule(
            'elseif_condition', 
            'else:(\w+)([!<>=]+)(\w+)', 
            '<?php elseif (((isset($this->data[\'$1\']) && isset($this->data[\'$3\'])) && $this->data[\'$1\'] $2 $this->data[\'$3\']) ||
        		((isset($this->data[\'$1\']) && !isset($this->data[\'$3\'])) && $this->data[\'$1\'] $2 \'$3\') ||
        		((!isset($this->data[\'$1\']) && isset($this->data[\'$3\'])) && \'$1\' $2 $this->data[\'$3\']) ||
        		((!isset($this->data[\'$1\']) && !isset($this->data[\'$3\'])) && \'$1\' $2 \'$3\')): ?>'
        );
        $this->rules[] = new Rule(
            'endif', 
            'endif', 
            '<?php endif; ?>'
        );
        
        // Loops
        $this->rules[] = new Rule(
            'loop', 
            'loop:(\w+)', 
            '<?php foreach ($this->data[\'$1\'] as $element): $this->wrap($element); ?>'
        );
        $this->rules[] = new Rule(
            'endloop', 
            'endloop', 
            '<?php $this->unwrap(); endforeach; ?>'
        );
        
        // Importing
        $this->rules[] = new Rule(
            'import_view', 
            'import:(([^\/\s]+\/)?(.*))', 
            '<?php echo $this->importFile(\'$1\'); ?>'
        );
        
        // Clean variables
        $this->rules[] = new Rule(
            'escape_var', 
            'escape:(\w+)', 
            '<?php $this->showVariable(\'$1\', true); ?>'
        );
        
        // Custom rules
        $this->rules = array_merge($this->rules, $rules);
        
        // Variables
        $this->rules[] = new Rule(
            'variable', 
            '(\w+)', 
            '<?php $this->showVariable(\'$1\'); ?>'
        );
        
        // Arrays
        $this->rules[] = new Rule(
        		'variable_array',
        		'(\w+)\[(\w+)\]',
        		'<?php echo (isset($this->data[\'$1\'][\'$2\'])) ? $this->data[\'$1\'][\'$2\'] : "&#123;$1[$2]&#125"; ?>'
        );
        
        $this->rules[] = new Rule(
        		'variable_array_escape',
        		'escape:(\w+)\[(\w+)\]',
        		'<?php echo htmlentities($this->showVariable(\'$1\')[\'$2\']); ?>'
        );
        
        $this->data = $data;
        $this->stack = array();
        
        foreach ($this->rules as $rule) {
            $this->template = preg_replace('/\{' . $rule->rule() . '\}/', $rule->replacement(), $this->template);
        }
        
        $this->template = '?>' . $this->template;
        
        ob_start();
        eval($this->template);
        return ob_get_clean();
    }
684023
public static function in(Validator $validator, $data, $pattern, $rule, $parameters)
    {
        foreach (Validator::getValues($data, $pattern) as $attribute => $value) {
            if (null === $value) {
                continue;
            }
            if (in_array($value, $parameters)) {
                continue;
            }

            $validator->addError($attribute, $rule, ['%values' => implode(', ', $parameters)]);
        }
    }
1074601
public function is($value, $rules)
    {
        $valTO = $this->apply($value, $rules);
        if ($valTO->fails()) {
            return false;
        }

        return $valTO->getValue();
    }
597733
public function validate($attribute, $rules, $value)
    {
        if (is_int($value)) {
            $value = (string)$value;
        } elseif (is_bool($value)) {
            $value = $value ? '1' : '0';
        } elseif ($value === null) {
            $value = '';
        }

        foreach ($rules as $k => $v) {
            if (is_int($k)) {
                $value = $this->_validate($attribute, $v, [], $value);
            } else {
                $value = $this->_validate($attribute, $k, is_array($v) ? $v : explode('-', $v), $value);
            }
        }

        return $value;
    }
572330
public function check($data, $rules = [], $scene = '') {
        $this->error = [];

        if (empty($rules)) {
            // 读取验证规则
            $rules = $this->rule;
        }

        // 获取场景定义
        $this->getScene($scene);

        foreach ($this->append as $key => $rule) {
            if (!isset($rules[$key])) {
                $rules[$key] = $rule;
            }
        }

        foreach ($rules as $key => $rule) {
            // field => 'rule1|rule2...' field => ['rule1','rule2',...]
            if (strpos($key, '|')) {
                // 字段|描述 用于指定属性名称
                list($key, $title) = explode('|', $key);
            }
            else {
                $title = isset($this->field[$key]) ? $this->field[$key] : $key;
            }

            // 场景检测
            if (!empty($this->only) && !in_array($key, $this->only)) {
                continue;
            }

            // 获取数据 支持二维数组
            $value = $this->getDataValue($data, $key);

            // 字段验证
            if ($rule instanceof \Closure) {
                $result = call_user_func_array($rule, [$value, $data]);
            }
            elseif ($rule instanceof ValidateRule) {
                //  验证因子
                $result = $this->checkItem($key, $value, $rule->getRule(), $data, $rule->getTitle() ?: $title, $rule->getMsg());
            }
            else {
                $result = $this->checkItem($key, $value, $rule, $data, $title);
            }

            if (true !== $result) {
                // 没有返回true 则表示验证失败
                if (!empty($this->batch)) {
                    // 批量验证
                    if (is_array($result)) {
                        $this->error = array_merge($this->error, $result);
                        $this->field = array_merge($this->field, $key);
                    }
                    else {
                        $this->error[$key] = $result;
                        $this->field[] = $key;
                    }
                }
                else {
                    $this->error = $result;
                    $this->field = $key;
                    return false;
                }
            }
        }

        return !empty($this->error) ? false : true;
    }
1206895
public function getSanitize($data, $rules)
    {
        // iterate through each field
        foreach ($rules as $field => $rule)
        {
            // turn the listing of rules with a "|" separator into an array
            // yeah, $rule can contain multiple rules (ie, multiple php functions)
            $phpFunctions = explode('|', $rule);

            // iterate through each rule
            foreach($phpFunctions as $phpFunction)
            {
                $data[$field] = call_user_func_array($phpFunction, [$data[$field] ]);

                // debug
                //echo "<br>The field ".$field." is now = ".$data[$field]." (".$singleFunction.")";
            }
        }

        return $data;
    }
609242
protected function egt($value, $rule, $data)
    {
        $val = $this->getDataValue($data, $rule);

        return !is_null($val) && $value >= $val;
    }
358052
public function apply(Rule $rule, $value)
    {
        //Check for Whitespace support
        $whitespaceChar = ($rule->allowWhitespace)? " ":"";

        $rule->unicodePattern = '/[^\p{N}' . $whitespaceChar . ']/';
        $rule->pattern        = '/[^0-9' . $whitespaceChar . ']/';

        return parent::apply($rule, $value);
    }
934210
public function validate( $data, array $rules = [] )
	{
		$this->normalizeData( $data );
		$this->setRules( $rules );

		foreach( $this->rules as $attribute => $rules ) {
			foreach( $rules as $rule ) {
				$this->validateAttribute( $rule, $attribute );

				if( $this->shouldStopValidating( $attribute ))break;
			}
		}

		return $this->errors;
	}