think\Validate::checkItem PHP Method

checkItem() protected method

验证单个字段规则
protected checkItem ( string $field, mixed $value, mixed $rules, array $data, string $title = '', array $msg = [] ) : mixed
$field string 字段名
$value mixed 字段值
$rules mixed 验证规则
$data array 数据
$title string 字段描述
$msg array 提示信息
return mixed
    protected function checkItem($field, $value, $rules, $data, $title = '', $msg = [])
    {
        if ($rules instanceof \Closure) {
            // 匿名函数验证 支持传入当前字段和所有字段两个数据
            $result = call_user_func_array($rules, [$value, $data]);
        } else {
            // 支持多规则验证 require|in:a,b,c|... 或者 ['require','in'=>'a,b,c',...]
            if (is_string($rules)) {
                $rules = explode('|', $rules);
            }
            $i = 0;
            foreach ($rules as $key => $rule) {
                if ($rule instanceof \Closure) {
                    $result = call_user_func_array($rule, [$value, $data]);
                } else {
                    // 判断验证类型
                    if (is_numeric($key) && strpos($rule, ':')) {
                        list($type, $rule) = explode(':', $rule, 2);
                        if (isset($this->alias[$type])) {
                            // 判断别名
                            $type = $this->alias[$type];
                        }
                        $info = $type;
                    } elseif (is_numeric($key)) {
                        $type = 'is';
                        $info = $rule;
                    } else {
                        $info = $type = $key;
                    }
                    // 如果不是require 有数据才会行验证
                    if (0 === strpos($info, 'require') || !is_null($value) && '' !== $value) {
                        // 验证类型
                        $callback = isset(self::$type[$type]) ? self::$type[$type] : [$this, $type];
                        // 验证数据
                        $result = call_user_func_array($callback, [$value, $rule, $data, $field]);
                    } else {
                        $result = true;
                    }
                }
                if (false === $result) {
                    // 验证失败 返回错误信息
                    if (isset($msg[$i])) {
                        $message = $msg[$i];
                        if (is_string($message) && strpos($message, '{%')) {
                            $message = Lang::get(substr($message, 2, -1));
                        }
                    } else {
                        $message = $this->getRuleMsg($field, $title, $info, $rule);
                    }
                    return $message;
                } elseif (true !== $result) {
                    // 返回自定义错误信息
                    return $result;
                }
                $i++;
            }
        }
        return true !== $result ? $result : true;
    }