ValidateRule::__call()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2021 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\validate;
14
15
/**
16
 * Class ValidateRule
17
 * @package think\validate
18
 * @method ValidateRule confirm(mixed $rule, string $msg = '') static 验证是否和某个字段的值一致
19
 * @method ValidateRule different(mixed $rule, string $msg = '') static 验证是否和某个字段的值是否不同
20
 * @method ValidateRule egt(mixed $rule, string $msg = '') static 验证是否大于等于某个值
21
 * @method ValidateRule gt(mixed $rule, string $msg = '') static 验证是否大于某个值
22
 * @method ValidateRule elt(mixed $rule, string $msg = '') static 验证是否小于等于某个值
23
 * @method ValidateRule lt(mixed $rule, string $msg = '') static 验证是否小于某个值
24
 * @method ValidateRule eg(mixed $rule, string $msg = '') static 验证是否等于某个值
25
 * @method ValidateRule in(mixed $rule, string $msg = '') static 验证是否在范围内
26
 * @method ValidateRule notIn(mixed $rule, string $msg = '') static 验证是否不在某个范围
27
 * @method ValidateRule between(mixed $rule, string $msg = '') static 验证是否在某个区间
28
 * @method ValidateRule notBetween(mixed $rule, string $msg = '') static 验证是否不在某个区间
29
 * @method ValidateRule length(mixed $rule, string $msg = '') static 验证数据长度
30
 * @method ValidateRule max(mixed $rule, string $msg = '') static 验证数据最大长度
31
 * @method ValidateRule min(mixed $rule, string $msg = '') static 验证数据最小长度
32
 * @method ValidateRule after(mixed $rule, string $msg = '') static 验证日期
33
 * @method ValidateRule before(mixed $rule, string $msg = '') static 验证日期
34
 * @method ValidateRule expire(mixed $rule, string $msg = '') static 验证有效期
35
 * @method ValidateRule allowIp(mixed $rule, string $msg = '') static 验证IP许可
36
 * @method ValidateRule denyIp(mixed $rule, string $msg = '') static 验证IP禁用
37
 * @method ValidateRule regex(mixed $rule, string $msg = '') static 使用正则验证数据
38
 * @method ValidateRule token(mixed $rule='__token__', string $msg = '') static 验证表单令牌
39
 * @method ValidateRule is(mixed $rule, string $msg = '') static 验证字段值是否为有效格式
40
 * @method ValidateRule isRequire(mixed $rule = null, string $msg = '') static 验证字段必须
41
 * @method ValidateRule isNumber(mixed $rule = null, string $msg = '') static 验证字段值是否为数字
42
 * @method ValidateRule isArray(mixed $rule = null, string $msg = '') static 验证字段值是否为数组
43
 * @method ValidateRule isInteger(mixed $rule = null, string $msg = '') static 验证字段值是否为整形
44
 * @method ValidateRule isFloat(mixed $rule = null, string $msg = '') static 验证字段值是否为浮点数
45
 * @method ValidateRule isMobile(mixed $rule = null, string $msg = '') static 验证字段值是否为手机
46
 * @method ValidateRule isIdCard(mixed $rule = null, string $msg = '') static 验证字段值是否为身份证号码
47
 * @method ValidateRule isChs(mixed $rule = null, string $msg = '') static 验证字段值是否为中文
48
 * @method ValidateRule isChsDash(mixed $rule = null, string $msg = '') static 验证字段值是否为中文字母及下划线
49
 * @method ValidateRule isChsAlpha(mixed $rule = null, string $msg = '') static 验证字段值是否为中文和字母
50
 * @method ValidateRule isChsAlphaNum(mixed $rule = null, string $msg = '') static 验证字段值是否为中文字母和数字
51
 * @method ValidateRule isDate(mixed $rule = null, string $msg = '') static 验证字段值是否为有效格式
52
 * @method ValidateRule isBool(mixed $rule = null, string $msg = '') static 验证字段值是否为布尔值
53
 * @method ValidateRule isAlpha(mixed $rule = null, string $msg = '') static 验证字段值是否为字母
54
 * @method ValidateRule isAlphaDash(mixed $rule = null, string $msg = '') static 验证字段值是否为字母和下划线
55
 * @method ValidateRule isAlphaNum(mixed $rule = null, string $msg = '') static 验证字段值是否为字母和数字
56
 * @method ValidateRule isAccepted(mixed $rule = null, string $msg = '') static 验证字段值是否为yes, on, 或是 1
57
 * @method ValidateRule isEmail(mixed $rule = null, string $msg = '') static 验证字段值是否为有效邮箱格式
58
 * @method ValidateRule isUrl(mixed $rule = null, string $msg = '') static 验证字段值是否为有效URL地址
59
 * @method ValidateRule activeUrl(mixed $rule, string $msg = '') static 验证是否为合格的域名或者IP
60
 * @method ValidateRule ip(mixed $rule, string $msg = '') static 验证是否有效IP
61
 * @method ValidateRule fileExt(mixed $rule, string $msg = '') static 验证文件后缀
62
 * @method ValidateRule fileMime(mixed $rule, string $msg = '') static 验证文件类型
63
 * @method ValidateRule fileSize(mixed $rule, string $msg = '') static 验证文件大小
64
 * @method ValidateRule image(mixed $rule, string $msg = '') static 验证图像文件
65
 * @method ValidateRule method(mixed $rule, string $msg = '') static 验证请求类型
66
 * @method ValidateRule dateFormat(mixed $rule, string $msg = '') static 验证时间和日期是否符合指定格式
67
 * @method ValidateRule unique(mixed $rule, string $msg = '') static 验证是否唯一
68
 * @method ValidateRule behavior(mixed $rule, string $msg = '') static 使用行为类验证
69
 * @method ValidateRule filter(mixed $rule, string $msg = '') static 使用filter_var方式验证
70
 * @method ValidateRule requireIf(mixed $rule, string $msg = '') static 验证某个字段等于某个值的时候必须
71
 * @method ValidateRule requireCallback(mixed $rule, string $msg = '') static 通过回调方法验证某个字段是否必须
72
 * @method ValidateRule requireWith(mixed $rule, string $msg = '') static 验证某个字段有值的情况下必须
73
 * @method ValidateRule must(mixed $rule = null, string $msg = '') static 必须验证
74
 */
75
class ValidateRule
76
{
77
    // 验证字段的名称
78
    protected $title;
79
80
    // 当前验证规则
81
    protected $rule = [];
82
83
    // 验证提示信息
84
    protected $message = [];
85
86
    /**
87
     * 添加验证因子
88
     * @access protected
89
     * @param  string    $name  验证名称
90
     * @param  mixed     $rule  验证规则
91
     * @param  string    $msg   提示信息
92
     * @return $this
93
     */
94
    protected function addItem(string $name, $rule = null, string $msg = '')
95
    {
96
        if ($rule || 0 === $rule) {
97
            $this->rule[$name] = $rule;
98
        } else {
99
            $this->rule[] = $name;
100
        }
101
102
        $this->message[] = $msg;
103
104
        return $this;
105
    }
106
107
    /**
108
     * 获取验证规则
109
     * @access public
110
     * @return array
111
     */
112
    public function getRule(): array
113
    {
114
        return $this->rule;
115
    }
116
117
    /**
118
     * 获取验证字段名称
119
     * @access public
120
     * @return string
121
     */
122
    public function getTitle(): string
123
    {
124
        return $this->title ?: '';
125
    }
126
127
    /**
128
     * 获取验证提示
129
     * @access public
130
     * @return array
131
     */
132
    public function getMsg(): array
133
    {
134
        return $this->message;
135
    }
136
137
    /**
138
     * 设置验证字段名称
139
     * @access public
140
     * @return $this
141
     */
142
    public function title(string $title)
143
    {
144
        $this->title = $title;
145
146
        return $this;
147
    }
148
149
    public function __call($method, $args)
150
    {
151
        if ('is' == strtolower(substr($method, 0, 2))) {
152
            $method = substr($method, 2);
153
        }
154
155
        array_unshift($args, lcfirst($method));
156
157
        return call_user_func_array([$this, 'addItem'], $args);
158
    }
159
160
    public static function __callStatic($method, $args)
161
    {
162
        $rule = new static();
163
164
        if ('is' == strtolower(substr($method, 0, 2))) {
165
            $method = substr($method, 2);
166
        }
167
168
        array_unshift($args, lcfirst($method));
169
170
        return call_user_func_array([$rule, 'addItem'], $args);
171
    }
172
}
173