|
@@ -13,16 +13,279 @@ namespace Lib; |
|
@@ -13,16 +13,279 @@ namespace Lib; |
13
|
class Verify {
|
13
|
class Verify {
|
14
|
|
14
|
|
15
|
/**
|
15
|
/**
|
|
|
16
|
+ * @var array
|
|
|
17
|
+ */
|
|
|
18
|
+ public array $data;
|
|
|
19
|
+
|
|
|
20
|
+ /**
|
|
|
21
|
+ * @var array
|
|
|
22
|
+ */
|
|
|
23
|
+ public array $message;
|
|
|
24
|
+
|
|
|
25
|
+ /**
|
|
|
26
|
+ * 别名
|
|
|
27
|
+ * @var array
|
|
|
28
|
+ */
|
|
|
29
|
+ public array $alias;
|
|
|
30
|
+
|
|
|
31
|
+ /**
|
16
|
* 验证邮箱
|
32
|
* 验证邮箱
|
17
|
* @param $email
|
33
|
* @param $email
|
18
|
* @return false|int
|
34
|
* @return false|int
|
19
|
* @author:dc
|
35
|
* @author:dc
|
20
|
* @time 2023/3/10 16:04
|
36
|
* @time 2023/3/10 16:04
|
21
|
*/
|
37
|
*/
|
22
|
- public static function email($email){
|
38
|
+ public static function sEmail($email){
|
23
|
return preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',$email);
|
39
|
return preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',$email);
|
24
|
}
|
40
|
}
|
25
|
|
41
|
|
|
|
42
|
+ /**
|
|
|
43
|
+ * 验证手机
|
|
|
44
|
+ * @param $mobile
|
|
|
45
|
+ * @return false|int
|
|
|
46
|
+ * @author:dc
|
|
|
47
|
+ * @time 2023/3/13 11:00
|
|
|
48
|
+ */
|
|
|
49
|
+ public static function sMobile($mobile){
|
|
|
50
|
+ return preg_match('/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/',$mobile);
|
|
|
51
|
+ }
|
|
|
52
|
+
|
|
|
53
|
+ /**
|
|
|
54
|
+ * 验证域名
|
|
|
55
|
+ * @param $domain
|
|
|
56
|
+ * @return false|int
|
|
|
57
|
+ * @author:dc
|
|
|
58
|
+ * @time 2023/3/13 10:59
|
|
|
59
|
+ */
|
|
|
60
|
+ public static function sDomain($domain){
|
|
|
61
|
+ return preg_match('/[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/',$domain);
|
|
|
62
|
+ }
|
|
|
63
|
+
|
|
|
64
|
+ /**
|
|
|
65
|
+ * 验证url
|
|
|
66
|
+ * @param $url
|
|
|
67
|
+ * @return false|int
|
|
|
68
|
+ * @author:dc
|
|
|
69
|
+ * @time 2023/3/13 11:00
|
|
|
70
|
+ */
|
|
|
71
|
+ public static function sUrl($url){
|
|
|
72
|
+ return preg_match('/[a-zA-z]+://[^\s]* 或 ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$/',$url);
|
|
|
73
|
+ }
|
|
|
74
|
+
|
|
|
75
|
+ /**
|
|
|
76
|
+ * 验证身份证
|
|
|
77
|
+ * @param $idCard
|
|
|
78
|
+ * @return false|int
|
|
|
79
|
+ * @author:dc
|
|
|
80
|
+ * @time 2023/3/13 11:01
|
|
|
81
|
+ */
|
|
|
82
|
+ public static function sIdCard($idCard){
|
|
|
83
|
+ return preg_match('/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/',$idCard);
|
|
|
84
|
+ }
|
|
|
85
|
+
|
|
|
86
|
+
|
|
|
87
|
+
|
|
|
88
|
+
|
|
|
89
|
+ /**
|
|
|
90
|
+ * 验证数据
|
|
|
91
|
+ * @param array $rule
|
|
|
92
|
+ * @param array $message
|
|
|
93
|
+ * @return array|false|float|int|mixed|null
|
|
|
94
|
+ * @author:dc
|
|
|
95
|
+ * @time 2023/3/10 17:56
|
|
|
96
|
+ */
|
|
|
97
|
+ public static function checks(array $rule, array $message=[]){
|
|
|
98
|
+ $verify = new static();
|
|
|
99
|
+ $verify->message = $message;
|
|
|
100
|
+
|
|
|
101
|
+ $data= app()->request();
|
|
|
102
|
+ if(is_array($data)){
|
|
|
103
|
+ $verify->data = &$data;
|
|
|
104
|
+ }
|
|
|
105
|
+
|
|
|
106
|
+ foreach ($rule as $key=>$item){
|
|
|
107
|
+ // 别名
|
|
|
108
|
+ $key = explode('|',$key);
|
|
|
109
|
+ $verify->alias[$key[0]] = $key[1]??$key[0];
|
|
|
110
|
+ // 验证规则
|
|
|
111
|
+ foreach ($item as $ak=>$av){
|
|
|
112
|
+ if(is_numeric($ak)){
|
|
|
113
|
+ $param = [$key[0]];
|
|
|
114
|
+ $ak = $av;
|
|
|
115
|
+ }else{
|
|
|
116
|
+ $param = [$key[0],$av];
|
|
|
117
|
+ }
|
|
|
118
|
+ // 验证规则,不能以下划线开始
|
|
|
119
|
+ if(str_starts_with($ak, '_') || !method_exists($verify,$ak)){
|
|
|
120
|
+ throw new Err($ak.':验证规则不存在',600);
|
|
|
121
|
+ }
|
|
|
122
|
+ if(!$verify->{$ak}(...$param)){
|
|
|
123
|
+ app()->e(
|
|
|
124
|
+ $verify->message[$key[0]][$ak]??['verify_'.$ak, [$verify->alias[$key[0]]??$key[0],print_r($av,true)]],
|
|
|
125
|
+ 600
|
|
|
126
|
+ );
|
|
|
127
|
+ }
|
|
|
128
|
+ }
|
|
|
129
|
+ }
|
|
|
130
|
+
|
|
|
131
|
+ return $verify->data;
|
|
|
132
|
+ }
|
|
|
133
|
+
|
|
|
134
|
+ /**
|
|
|
135
|
+ * 获取表单值
|
|
|
136
|
+ * @param $key
|
|
|
137
|
+ * @return mixed|string
|
|
|
138
|
+ * @author:dc
|
|
|
139
|
+ * @time 2023/3/10 18:03
|
|
|
140
|
+ */
|
|
|
141
|
+ private function _get($key){
|
|
|
142
|
+ return $this->data[$key]??'';
|
|
|
143
|
+ }
|
|
|
144
|
+
|
|
|
145
|
+ /**
|
|
|
146
|
+ * 必须
|
|
|
147
|
+ * @param $key
|
|
|
148
|
+ * @return false
|
|
|
149
|
+ * @author:dc
|
|
|
150
|
+ * @time 2023/3/13 10:11
|
|
|
151
|
+ */
|
|
|
152
|
+ public function required($key){
|
|
|
153
|
+ return $this->_get($key) !== '';
|
|
|
154
|
+ }
|
|
|
155
|
+
|
|
|
156
|
+
|
|
|
157
|
+ /**
|
|
|
158
|
+ * 验证长度
|
|
|
159
|
+ * @param $key
|
|
|
160
|
+ * @param $value
|
|
|
161
|
+ * @return bool
|
|
|
162
|
+ * @author:dc
|
|
|
163
|
+ * @time 2023/3/13 10:11
|
|
|
164
|
+ */
|
|
|
165
|
+ public function max($key,$value):bool {
|
|
|
166
|
+ return mb_strlen($this->_get($key)) <= $value;
|
|
|
167
|
+ }
|
|
|
168
|
+
|
|
|
169
|
+ /**
|
|
|
170
|
+ * 最小字符
|
|
|
171
|
+ * @param $key
|
|
|
172
|
+ * @param $value
|
|
|
173
|
+ * @return bool
|
|
|
174
|
+ * @author:dc
|
|
|
175
|
+ * @time 2023/3/13 10:11
|
|
|
176
|
+ */
|
|
|
177
|
+ public function min($key,$value):bool {
|
|
|
178
|
+ return mb_strlen($this->_get($key)) >= $value;
|
|
|
179
|
+ }
|
|
|
180
|
+
|
|
|
181
|
+
|
|
|
182
|
+ /**
|
|
|
183
|
+ * 是否是数组
|
|
|
184
|
+ * @param $key
|
|
|
185
|
+ * @param $value
|
|
|
186
|
+ * @return bool
|
|
|
187
|
+ * @author:dc
|
|
|
188
|
+ * @time 2023/3/13 10:11
|
|
|
189
|
+ */
|
|
|
190
|
+ public function array($key,$value):bool {
|
|
|
191
|
+ return is_array($value);
|
|
|
192
|
+ }
|
|
|
193
|
+
|
|
|
194
|
+
|
|
|
195
|
+ /**
|
|
|
196
|
+ * 整数
|
|
|
197
|
+ * @param $key
|
|
|
198
|
+ * @param $value
|
|
|
199
|
+ * @return bool
|
|
|
200
|
+ * @author:dc
|
|
|
201
|
+ * @time 2023/3/13 10:15
|
|
|
202
|
+ */
|
|
|
203
|
+ public function integer($key,$value):bool {
|
|
|
204
|
+ return is_integer($value);
|
|
|
205
|
+ }
|
|
|
206
|
+
|
|
|
207
|
+
|
|
|
208
|
+ /**
|
|
|
209
|
+ * 数字,整数,浮点,负数
|
|
|
210
|
+ * @param $key
|
|
|
211
|
+ * @param $value
|
|
|
212
|
+ * @return bool
|
|
|
213
|
+ * @author:dc
|
|
|
214
|
+ * @time 2023/3/13 10:16
|
|
|
215
|
+ */
|
|
|
216
|
+ public function number($key,$value):bool {
|
|
|
217
|
+ return is_numeric($value);
|
|
|
218
|
+ }
|
|
|
219
|
+
|
|
|
220
|
+
|
|
|
221
|
+ /**
|
|
|
222
|
+ * 正则
|
|
|
223
|
+ * @param $key
|
|
|
224
|
+ * @param $value
|
|
|
225
|
+ * @return bool
|
|
|
226
|
+ * @author:dc
|
|
|
227
|
+ * @time 2023/3/13 10:20
|
|
|
228
|
+ */
|
|
|
229
|
+ public function reg($key,$value):bool {
|
|
|
230
|
+ return (bool) preg_match($value,$this->_get($key));
|
|
|
231
|
+ }
|
|
|
232
|
+
|
|
|
233
|
+
|
|
|
234
|
+ /**
|
|
|
235
|
+ * 之间
|
|
|
236
|
+ * @param $key
|
|
|
237
|
+ * @param $value
|
|
|
238
|
+ * @return bool
|
|
|
239
|
+ * @author:dc
|
|
|
240
|
+ * @time 2023/3/13 10:31
|
|
|
241
|
+ */
|
|
|
242
|
+ public function between($key,$value){
|
|
|
243
|
+ $data = $this->_get($key);
|
|
|
244
|
+ return $data >= $value[0] && $data <= $value[1];
|
|
|
245
|
+ }
|
|
|
246
|
+
|
|
|
247
|
+
|
|
|
248
|
+ /**
|
|
|
249
|
+ * 邮箱
|
|
|
250
|
+ * @param $key
|
|
|
251
|
+ * @return false|int
|
|
|
252
|
+ * @author:dc
|
|
|
253
|
+ * @time 2023/3/13 10:55
|
|
|
254
|
+ */
|
|
|
255
|
+ public function email($key){
|
|
|
256
|
+ $emails = $this->_get($key);
|
|
|
257
|
+ if(is_array($emails)){
|
|
|
258
|
+ foreach ($emails as $email){
|
|
|
259
|
+ if(!static::sEmail($email)){
|
|
|
260
|
+ return false;
|
|
|
261
|
+ }
|
|
|
262
|
+ }
|
|
|
263
|
+ }else{
|
|
|
264
|
+ return static::sEmail($emails);
|
|
|
265
|
+ }
|
|
|
266
|
+ }
|
|
|
267
|
+
|
|
|
268
|
+ /**
|
|
|
269
|
+ * 手机号
|
|
|
270
|
+ * @param $key
|
|
|
271
|
+ * @return false|int
|
|
|
272
|
+ * @author:dc
|
|
|
273
|
+ * @time 2023/3/13 11:06
|
|
|
274
|
+ */
|
|
|
275
|
+ public function mobile($key){
|
|
|
276
|
+ $mobiles = $this->_get($key);
|
|
|
277
|
+ if(is_array($mobiles)){
|
|
|
278
|
+ foreach ($mobiles as $mobile){
|
|
|
279
|
+ if(!static::sEmail($mobile)){
|
|
|
280
|
+ return false;
|
|
|
281
|
+ }
|
|
|
282
|
+ }
|
|
|
283
|
+ }else{
|
|
|
284
|
+ return static::sEmail($mobiles);
|
|
|
285
|
+ }
|
|
|
286
|
+ }
|
|
|
287
|
+
|
|
|
288
|
+
|
26
|
|
289
|
|
27
|
|
290
|
|
28
|
} |
291
|
} |