批量验证是否有错误
@param array $data
@param array $rules 验证规则 <code>[
'待验证数组下标' => [
[
'验证方法1' => true,
'message' => '错误提示信息'
],
[
'验证方法1' => true,
'验证方法2' => '参数值',
'message' => '错误提示信息'
],
...
]<code>
@param bool $firstErrBreak = false 第一次验证不通过时返回
@return bool
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);
	}
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;
    }
1203984
protected function validate_use_validator( array $data, array $rules ) {
		if ( empty( $rules ) ) {
			return;
		}

		$validator = new Validator( $data, $rules );

		$validator->labels(
			$this->fields->pluck( 'name', 'id' )->all()
		);

		if ( $validator->fails() ) {
			foreach ( $rules as $key => $rule ) {
				$this->add_error( $key, $validator->errors( $key ) );
			}
		}
	}
653902
public function createActions(array $data): array
    {
        return array_map(function ($data) {
            if ($data instanceof RuleAction) {
                return $data;
            }

            return RuleAction::createFromData((array) $data);
        }, $data);
    }
544307
public function createRules(array $data): array
    {
        return array_map(function ($data) {
            if ($data instanceof Rule) {
                return $data;
            }

            return Rule::createFromData((array) $data);
        }, $data);
    }
1001584
public static function CreateFromResponse(array $data)
    {
        $name = $data["name"];
        $object_type_id = $data["object_type_id"];
        $id = $data["id"];

        $builder = new RuleBuilder($name, $object_type_id, $id);

        $events = self::_splitRule($data["events"]);
        $conditions = array();
        if ($data["conditions"] != null)
        {
            $conditions = self::_splitRule($data["conditions"]);
        }
        $actions = self::_splitRule($data["actions"]);

        foreach($events as $event)
        {
            // separate rule and params
            $parsed = self::_parseParams($event);
            $builder->addEvent($parsed["name"], $parsed["params"]);
        }
        foreach($actions as $action)
        {
            // separate rule and params
            $parsed = self::_parseParams($action);
            $builder->addAction($parsed["name"], $parsed["params"]);
        }
        if (!empty($conditions))
        {
            foreach($conditions as $condition)
            {
                // determine operators
                $operators = self::_operatorClassifier($data["conditions"]);
                $or_rule = $operators["or_rules"];
                $and_rule = $operators["and_rules"];
                $first_rule = $operators["first_rule"];
                // separate rule and param
                $parsed = self::_parseParams($condition);

                if (in_array($condition, $first_rule))
                {
                    $builder->addCondition($parsed["name"], $parsed["params"]);
                }
                else if (in_array($condition, $or_rule))
                {
                    $builder->addCondition($parsed["name"], $parsed["params"], "OR");
                }
                else if (in_array($condition, $and_rule))
                {
                    $builder->addCondition($parsed["name"], $parsed["params"], "AND");
                }
            }
        }
        return $builder;
    }
132566
protected function parseRulesArray(array $rules)
    {
        $parsedRules = [];

        $rawRules = (new ValidationRuleParser($this->data))->explode($rules);

        foreach ($rawRules->rules as $attribute => $attributeRules) {
            foreach ($attributeRules as $attributeRule) {
                $parsedRule = $this->parseRuleString($attributeRule);
                if ($parsedRule) {
                    $parsedRules[$attribute][] = $parsedRule;
                }
            }
        }

        return $parsedRules;
    }
1250357
protected function getRulesAsArray($rules) {
        $rulesArray = (is_string($rules)) ? explode('|', $rules) : $rules;

        return array_map(function($rule) {
            if ($rule instanceof \Closure) {
                return $rule($this->field->getNameKey());
            }

            return $rule;
        }, $rulesArray);
    }
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;
    }
71362
public function getMessageRecord(array $data)
    {
        $result = $this->wechat->httpRaw(self::WECHAT_MESSAGE_RECORD_GET_PREFIX, $data, [
            'access_token' => $this->wechat->getAccessToken()
        ]);
        return isset($result['errcode']) && !$result['errcode'] ? $result['recordlist'] : false;
    }
1360292
protected function filterDetectedRules(array $rules, AppliesToResource $resource)
    {

        if (!$this->typeProvider) {
            return $rules;
        }

        $filteredRules = [];

        foreach ($rules as $key=>$rule) {

            if (!$type = $this->xType($key)) {
                continue;
            }

            if ($this->shouldRemoveRules($key, $type)) {
                continue;
            }

            if ($type->readonly) {
                $filteredRules[$key] = 'forbidden';
                continue;
            }

            $filteredRules[$key] = $rule;
        }

        return $filteredRules;
    }
708332
public function validate(array $data)
    {
        $rules = $this->getRules();
        foreach ($rules as $field => $rule) {
            if ((isset($rule['required']) && $rule['required'] === true && isset($data[$field]))
                || ((!isset($rule['required']) || !$rule['required'])) && isset($data[$field])) {
                foreach ($rule as $key => $value) {
                    if ($key !== 'required') {
                        foreach ($value as $validator) {
                            if (!is_array($validator)) {
                                $validator = [$validator];
                            }
                            if (!Validator::validate($validator[0], $field,
                                    $data, $validator)) {
                                $this->addError($field,
                                    isset($rule['errorMessage']) ? $rule['errorMessage']
                                            : false);
                            }
                        }
                    }
                }
            } elseif (isset($rule['required']) && $rule['required'] === true) {
                $this->addError($field,
                    isset($rule['errorMessage']) ? $rule['errorMessage'] : false);
            }
        }
        return !$this->hasErrors();
    }
1183457
public function validate($data = [])
    {
        if ($this->isReadonly()) {
            return true;
        }

        $registered = LegoRegister::getDefault(FieldSelfDefinedValidation::class);
        if ($registered) {
            call_user_func_array($registered, [$this, $this->data]);
        }

        $value = $this->getNewValue();

        /**
         * Run Laravel Validation
         * ide-helper comment.
         *
         * @var Field
         * @var \Illuminate\Validation\Validator $validator
         */
        $validator = Validator::make(
            $data ?: [$this->elementName() => $value],
            [$this->elementName() => $this->rules()],
            [],
            [$this->elementName() => $this->description()]
        );

        if ($validator->fails()) {
            $this->errors()->merge($validator->messages());

            return false;
        }

        // 调用自定义的 Validators
        foreach ($this->validators as $closure) {
            $error = call_user_func($closure, $value);
            if (is_string($error)) {
                $this->errors()->add('error', $error);

                return false;
            }
        }

        return true;
    }
954637
public function validate($rules = [], $errors = [])
    {
        foreach ($rules as $param => $subRules) {
            $paramValue = $this->getParam($param);
            foreach ($subRules as $key => $rule) {
                $isError = false;
                if (!is_array($rule)) {
                    switch ($rule) {
                        case self::VALIDATE_REQUIRED:
                            if (is_null($paramValue)) {
                                $isError = true;
                            }
                            break;
                        case self::VALIDATE_MOBILE:
                            if (!ValidationHelper::isMobile($paramValue)) {
                                $isError = true;
                            }
                            break;
                    }
                } else {
                    switch ($key) {
                    case self::VALIDATE_ENUM:
                        if (!in_array($paramValue, $rule)) {
                            $isError = true;
                        }
                        break;
                    }
                }
                if ($isError) {
                    $errCode = self::ERROR_INVALID_PARAM;
                    $errMsg = self::errorMsg[self::ERROR_INVALID_PARAM];
                    if (is_array($rule)) {
                        $rule = $key;
                    }
                    if (isset($errors[$param][$rule]['code'])) {
                        $errCode = $errors[$param][$rule]['code'];
                    }
                    if (isset($errors[$param][$rule]['msg'])) {
                        $errMsg = str_replace('{param}', $param, $errors[$param][$rule]['msg']);
                    }
                    throw new ParamException($errMsg, $errCode);
                }
            }
        }
    }
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;
	}
476810
public function validate(array $data, array $rules, array $messages = [], array $customAttributes = [])
    {
        return $this->make($data, $rules, $messages, $customAttributes)->validate();
    }
555043
public function validateContext($data, $rules)
	{
		// Start off with a valid status.
		$status = 'valid';

		// We only need to validate context if context rules are given.
		if(array_key_exists('context', $rules))
		{
			foreach($rules['context'] as $rule)
			{
				switch($rule)
				{
					case 'unique_email':
						$userPayload = $this->service['user']->get([['field' => 'email', 'value' => $data['email'], 'operator' => '=', 'or' => false]]);

						if($userPayload->getStatus() == 'found')
						{
							if(in_array($data['email'], $userPayload->getData()->pluck('email')->toArray()))
							{
								$status = $rule;
							}
						}
					break;

					case 'unique_username':
						$userPayload = $this->service['user']->get([['field' => 'username', 'value' => $data['username'], 'operator' => '=', 'or' => false]]);
						
						if($userPayload->getStatus() == 'found')
						{
							if(in_array($data['username'], $userPayload->getData()->pluck('username')->toArray()))
							{
								$status = $rule;
							}
						}
					break;

					default:
					break;
				}
			}
		}

		// @todo: check if we need to unset this.
		// Remove context rules as they're no longer needed.
		unset($rules['context']);

		return new Payload($data, $status);
	}