Completed
Pull Request — 6.0 (#2470)
by
unknown
21:34
created

Validate::unique()   F

Complexity

Conditions 17
Paths 384

Size

Total Lines 68
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 17
eloc 43
nc 384
nop 4
dl 0
loc 68
ccs 0
cts 33
cp 0
crap 306
rs 2.0833
c 4
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
14
15
use Closure;
16
use think\exception\ValidateException;
17
use think\helper\Str;
18
use think\validate\ValidateRule;
19
20
/**
21
 * 数据验证类
22
 * @package think
23
 */
24
class Validate
25
{
26
    /**
27
     * 自定义验证类型
28
     * @var array
29
     */
30
    protected $type = [];
31
32
    /**
33
     * 验证类型别名
34
     * @var array
35
     */
36
    protected $alias = [
37
        '>' => 'gt', '>=' => 'egt', '<' => 'lt', '<=' => 'elt', '=' => 'eq', 'same' => 'eq',
38
    ];
39
40
    /**
41
     * 当前验证规则
42
     * @var array
43
     */
44
    protected $rule = [];
45
46
    /**
47
     * 验证提示信息
48
     * @var array
49
     */
50
    protected $message = [];
51
52
    /**
53
     * 验证字段描述
54
     * @var array
55
     */
56
    protected $field = [];
57
58
    /**
59
     * 默认规则提示
60
     * @var array
61
     */
62
    protected $typeMsg = [
63
        'require'     => ':attribute require',
64
        'must'        => ':attribute must',
65
        'number'      => ':attribute must be numeric',
66
        'integer'     => ':attribute must be integer',
67
        'float'       => ':attribute must be float',
68
        'boolean'     => ':attribute must be bool',
69
        'email'       => ':attribute not a valid email address',
70
        'mobile'      => ':attribute not a valid mobile',
71
        'array'       => ':attribute must be a array',
72
        'accepted'    => ':attribute must be yes,on or 1',
73
        'date'        => ':attribute not a valid datetime',
74
        'file'        => ':attribute not a valid file',
75
        'image'       => ':attribute not a valid image',
76
        'alpha'       => ':attribute must be alpha',
77
        'alphaNum'    => ':attribute must be alpha-numeric',
78
        'alphaDash'   => ':attribute must be alpha-numeric, dash, underscore',
79
        'activeUrl'   => ':attribute not a valid domain or ip',
80
        'chs'         => ':attribute must be chinese',
81
        'chsAlpha'    => ':attribute must be chinese or alpha',
82
        'chsAlphaNum' => ':attribute must be chinese,alpha-numeric',
83
        'chsDash'     => ':attribute must be chinese,alpha-numeric,underscore, dash',
84
        'url'         => ':attribute not a valid url',
85
        'ip'          => ':attribute not a valid ip',
86
        'dateFormat'  => ':attribute must be dateFormat of :rule',
87
        'in'          => ':attribute must be in :rule',
88
        'notIn'       => ':attribute be notin :rule',
89
        'between'     => ':attribute must between :1 - :2',
90
        'notBetween'  => ':attribute not between :1 - :2',
91
        'length'      => 'size of :attribute must be :rule',
92
        'max'         => 'max size of :attribute must be :rule',
93
        'min'         => 'min size of :attribute must be :rule',
94
        'after'       => ':attribute cannot be less than :rule',
95
        'before'      => ':attribute cannot exceed :rule',
96
        'expire'      => ':attribute not within :rule',
97
        'allowIp'     => 'access IP is not allowed',
98
        'denyIp'      => 'access IP denied',
99
        'confirm'     => ':attribute out of accord with :2',
100
        'different'   => ':attribute cannot be same with :2',
101
        'egt'         => ':attribute must greater than or equal :rule',
102
        'gt'          => ':attribute must greater than :rule',
103
        'elt'         => ':attribute must less than or equal :rule',
104
        'lt'          => ':attribute must less than :rule',
105
        'eq'          => ':attribute must equal :rule',
106
        'unique'      => ':attribute has exists',
107
        'regex'       => ':attribute not conform to the rules',
108
        'method'      => 'invalid Request method',
109
        'token'       => 'invalid token',
110
        'fileSize'    => 'filesize not match',
111
        'fileExt'     => 'extensions to upload is not allowed',
112
        'fileMime'    => 'mimetype to upload is not allowed',
113
    ];
114
115
    /**
116
     * 当前验证场景
117
     * @var string
118
     */
119
    protected $currentScene;
120
121
    /**
122
     * 内置正则验证规则
123
     * @var array
124
     */
125
    protected $defaultRegex = [
126
        'alpha'       => '/^[A-Za-z]+$/',
127
        'alphaNum'    => '/^[A-Za-z0-9]+$/',
128
        'alphaDash'   => '/^[A-Za-z0-9\-\_]+$/',
129
        'chs'         => '/^[\x{4e00}-\x{9fa5}\x{9fa6}-\x{9fef}\x{3400}-\x{4db5}\x{20000}-\x{2ebe0}]+$/u',
130
        'chsAlpha'    => '/^[\x{4e00}-\x{9fa5}\x{9fa6}-\x{9fef}\x{3400}-\x{4db5}\x{20000}-\x{2ebe0}a-zA-Z]+$/u',
131
        'chsAlphaNum' => '/^[\x{4e00}-\x{9fa5}\x{9fa6}-\x{9fef}\x{3400}-\x{4db5}\x{20000}-\x{2ebe0}a-zA-Z0-9]+$/u',
132
        'chsDash'     => '/^[\x{4e00}-\x{9fa5}\x{9fa6}-\x{9fef}\x{3400}-\x{4db5}\x{20000}-\x{2ebe0}a-zA-Z0-9\_\-]+$/u',
133
        'mobile'      => '/^1[3-9]\d{9}$/',
134
        'idCard'      => '/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/',
135
        'zip'         => '/\d{6}/',
136
    ];
137
138
    /**
139
     * Filter_var 规则
140
     * @var array
141
     */
142
    protected $filter = [
143
        'email'   => FILTER_VALIDATE_EMAIL,
144
        'ip'      => [FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6],
145
        'integer' => FILTER_VALIDATE_INT,
146
        'url'     => FILTER_VALIDATE_URL,
147
        'macAddr' => FILTER_VALIDATE_MAC,
148
        'float'   => FILTER_VALIDATE_FLOAT,
149
    ];
150
151
    /**
152
     * 验证场景定义
153
     * @var array
154
     */
155
    protected $scene = [];
156
157
    /**
158
     * 验证失败错误信息
159
     * @var string|array
160
     */
161
    protected $error = [];
162
163
    /**
164
     * 是否批量验证
165
     * @var bool
166
     */
167
    protected $batch = false;
168
169
    /**
170
     * 验证失败是否抛出异常
171
     * @var bool
172
     */
173
    protected $failException = false;
174
175
    /**
176
     * 场景需要验证的规则
177
     * @var array
178
     */
179
    protected $only = [];
180
181
    /**
182
     * 场景需要移除的验证规则
183
     * @var array
184
     */
185
    protected $remove = [];
186
187
    /**
188
     * 场景需要追加的验证规则
189
     * @var array
190
     */
191
    protected $append = [];
192
193
    /**
194
     * 验证正则定义
195
     * @var array
196
     */
197
    protected $regex = [];
198
199
    /**
200
     * Db对象
201
     * @var Db
202
     */
203
    protected $db;
204
205
    /**
206
     * 语言对象
207
     * @var Lang
208
     */
209
    protected $lang;
210
211
    /**
212
     * 请求对象
213
     * @var Request
214
     */
215
    protected $request;
216
217
    /**
218
     * @var Closure[]
219
     */
220
    protected static $maker = [];
221
222
    /**
223
     * 构造方法
224
     * @access public
225
     */
226
    public function __construct()
227
    {
228
        if (!empty(static::$maker)) {
229
            foreach (static::$maker as $maker) {
230
                call_user_func($maker, $this);
231
            }
232
        }
233
    }
234
235
    /**
236
     * 设置服务注入
237
     * @access public
238
     * @param Closure $maker
239
     * @return void
240
     */
241
    public static function maker(Closure $maker)
242
    {
243
        static::$maker[] = $maker;
244
    }
245
246
    /**
247
     * 设置Lang对象
248
     * @access public
249
     * @param Lang $lang Lang对象
250
     * @return void
251
     */
252
    public function setLang(Lang $lang)
253
    {
254
        $this->lang = $lang;
255
    }
256
257
    /**
258
     * 设置Db对象
259
     * @access public
260
     * @param Db $db Db对象
261
     * @return void
262
     */
263
    public function setDb(Db $db)
264
    {
265
        $this->db = $db;
266
    }
267
268
    /**
269
     * 设置Request对象
270
     * @access public
271
     * @param Request $request Request对象
272
     * @return void
273
     */
274
    public function setRequest(Request $request)
275
    {
276
        $this->request = $request;
277
    }
278
279
    /**
280
     * 添加字段验证规则
281
     * @access protected
282
     * @param string|array $name 字段名称或者规则数组
283
     * @param mixed        $rule 验证规则或者字段描述信息
284
     * @return $this
285
     */
286
    public function rule($name, $rule = '')
287
    {
288
        if (is_array($name)) {
289
            $this->rule = $name + $this->rule;
290
            if (is_array($rule)) {
291
                $this->field = array_merge($this->field, $rule);
292
            }
293
        } else {
294
            $this->rule[$name] = $rule;
295
        }
296
297
        return $this;
298
    }
299
300
    /**
301
     * 注册验证(类型)规则
302
     * @access public
303
     * @param string   $type     验证规则类型
304
     * @param callable $callback callback方法(或闭包)
305
     * @param string   $message  验证失败提示信息
306
     * @return $this
307
     */
308
    public function extend(string $type, callable $callback = null, string $message = null)
309
    {
310
        $this->type[$type] = $callback;
311
312
        if ($message) {
313
            $this->typeMsg[$type] = $message;
314
        }
315
316
        return $this;
317
    }
318
319
    /**
320
     * 设置验证规则的默认提示信息
321
     * @access public
322
     * @param string|array $type 验证规则类型名称或者数组
323
     * @param string       $msg  验证提示信息
324
     * @return void
325
     */
326
    public function setTypeMsg($type, string $msg = null): void
327
    {
328
        if (is_array($type)) {
329
            $this->typeMsg = array_merge($this->typeMsg, $type);
330
        } else {
331
            $this->typeMsg[$type] = $msg;
332
        }
333
    }
334
335
    /**
336
     * 设置提示信息
337
     * @access public
338
     * @param array $message 错误信息
339
     * @return Validate
340
     */
341
    public function message(array $message)
342
    {
343
        $this->message = array_merge($this->message, $message);
344
345
        return $this;
346
    }
347
348
    /**
349
     * 设置验证场景
350
     * @access public
351
     * @param string $name 场景名
352
     * @return $this
353
     */
354
    public function scene(string $name)
355
    {
356
        // 设置当前场景
357
        $this->currentScene = $name;
358
359
        return $this;
360
    }
361
362
    /**
363
     * 判断是否存在某个验证场景
364
     * @access public
365
     * @param string $name 场景名
366
     * @return bool
367
     */
368
    public function hasScene(string $name): bool
369
    {
370
        return isset($this->scene[$name]) || method_exists($this, 'scene' . $name);
371
    }
372
373
    /**
374
     * 设置批量验证
375
     * @access public
376
     * @param bool $batch 是否批量验证
377
     * @return $this
378
     */
379
    public function batch(bool $batch = true)
380
    {
381
        $this->batch = $batch;
382
383
        return $this;
384
    }
385
386
    /**
387
     * 设置验证失败后是否抛出异常
388
     * @access protected
389
     * @param bool $fail 是否抛出异常
390
     * @return $this
391
     */
392
    public function failException(bool $fail = true)
393
    {
394
        $this->failException = $fail;
395
396
        return $this;
397
    }
398
399
    /**
400
     * 指定需要验证的字段列表
401
     * @access public
402
     * @param array $fields 字段名
403
     * @return $this
404
     */
405
    public function only(array $fields)
406
    {
407
        $this->only = $fields;
408
409
        return $this;
410
    }
411
412
    /**
413
     * 移除某个字段的验证规则
414
     * @access public
415
     * @param string|array $field 字段名
416
     * @param mixed        $rule  验证规则 true 移除所有规则
417
     * @return $this
418
     */
419
    public function remove($field, $rule = null)
420
    {
421
        if (is_array($field)) {
422
            foreach ($field as $key => $rule) {
423
                if (is_int($key)) {
424
                    $this->remove($rule);
425
                } else {
426
                    $this->remove($key, $rule);
427
                }
428
            }
429
        } else {
430
            if (is_string($rule)) {
431
                $rule = explode('|', $rule);
432
            }
433
434
            $this->remove[$field] = $rule;
435
        }
436
437
        return $this;
438
    }
439
440
    /**
441
     * 追加某个字段的验证规则
442
     * @access public
443
     * @param string|array $field 字段名
444
     * @param mixed        $rule  验证规则
445
     * @return $this
446
     */
447
    public function append($field, $rule = null)
448
    {
449
        if (is_array($field)) {
450
            foreach ($field as $key => $rule) {
451
                $this->append($key, $rule);
452
            }
453
        } else {
454
            if (is_string($rule)) {
455
                $rule = explode('|', $rule);
456
            }
457
458
            $this->append[$field] = $rule;
459
        }
460
461
        return $this;
462
    }
463
464
    /**
465
     * 数据自动验证
466
     * @access public
467
     * @param array $data  数据
468
     * @param array $rules 验证规则
469
     * @return bool
470
     */
471
    public function check(array $data, array $rules = []): bool
472
    {
473
        $this->error = [];
474
475
        if ($this->currentScene) {
476
            $this->getScene($this->currentScene);
477
        }
478
479
        if (empty($rules)) {
480
            // 读取验证规则
481
            $rules = $this->rule;
482
        }
483
484
        foreach ($this->append as $key => $rule) {
485
            if (!isset($rules[$key])) {
486
                $rules[$key] = $rule;
487
                unset($this->append[$key]);
488
            }
489
        }
490
491
        foreach ($rules as $key => $rule) {
492
            // field => 'rule1|rule2...' field => ['rule1','rule2',...]
493
            if (strpos($key, '|')) {
494
                // 字段|描述 用于指定属性名称
495
                [$key, $title] = explode('|', $key);
496
            } else {
497
                $title = $this->field[$key] ?? $key;
498
            }
499
500
            // 场景检测
501
            if (!empty($this->only) && !in_array($key, $this->only)) {
502
                continue;
503
            }
504
505
            // 获取数据 支持二维数组
506
            $value = $this->getDataValue($data, $key);
507
508
            // 字段验证
509
            if ($rule instanceof Closure) {
510
                $result = call_user_func_array($rule, [$value, $data]);
511
            } elseif ($rule instanceof ValidateRule) {
512
                //  验证因子
513
                $result = $this->checkItem($key, $value, $rule->getRule(), $data, $rule->getTitle() ?: $title, $rule->getMsg());
514
            } else {
515
                $result = $this->checkItem($key, $value, $rule, $data, $title);
516
            }
517
518
            if (true !== $result) {
519
                // 没有返回true 则表示验证失败
520
                if (!empty($this->batch)) {
521
                    // 批量验证
522
                    $this->error[$key] = $result;
523
                } elseif ($this->failException) {
524
                    throw new ValidateException($result);
525
                } else {
526
                    $this->error = $result;
527
                    return false;
528
                }
529
            }
530
        }
531
532
        if (!empty($this->error)) {
533
            if ($this->failException) {
534
                throw new ValidateException($this->error);
535
            }
536
            return false;
537
        }
538
539
        return true;
540
    }
541
542
    /**
543
     * 根据验证规则验证数据
544
     * @access public
545
     * @param mixed $value 字段值
546
     * @param mixed $rules 验证规则
547
     * @return bool
548
     */
549
    public function checkRule($value, $rules): bool
550
    {
551
        if ($rules instanceof Closure) {
552
            return call_user_func_array($rules, [$value]);
553
        } elseif ($rules instanceof ValidateRule) {
554
            $rules = $rules->getRule();
555
        } elseif (is_string($rules)) {
556
            $rules = explode('|', $rules);
557
        }
558
559
        foreach ($rules as $key => $rule) {
560
            if ($rule instanceof Closure) {
561
                $result = call_user_func_array($rule, [$value]);
562
            } else {
563
                // 判断验证类型
564
                [$type, $rule] = $this->getValidateType($key, $rule);
565
566
                $callback = $this->type[$type] ?? [$this, $type];
567
568
                $result = call_user_func_array($callback, [$value, $rule]);
569
            }
570
571
            if (true !== $result) {
572
                if ($this->failException) {
573
                    throw new ValidateException($result);
574
                }
575
576
                return $result;
577
            }
578
        }
579
580
        return true;
581
    }
582
583
    /**
584
     * 验证单个字段规则
585
     * @access protected
586
     * @param string $field 字段名
587
     * @param mixed  $value 字段值
588
     * @param mixed  $rules 验证规则
589
     * @param array  $data  数据
590
     * @param string $title 字段描述
591
     * @param array  $msg   提示信息
592
     * @return mixed
593
     */
594
    protected function checkItem(string $field, $value, $rules, $data, string $title = '', array $msg = [])
595
    {
596
        if (isset($this->remove[$field]) && true === $this->remove[$field] && empty($this->append[$field])) {
597
            // 字段已经移除 无需验证
598
            return true;
599
        }
600
601
        // 支持多规则验证 require|in:a,b,c|... 或者 ['require','in'=>'a,b,c',...]
602
        if (is_string($rules)) {
603
            $rules = explode('|', $rules);
604
        }
605
606
        if (isset($this->append[$field])) {
607
            // 追加额外的验证规则
608
            $rules = array_unique(array_merge($rules, $this->append[$field]), SORT_REGULAR);
609
            unset($this->append[$field]);
610
        }
611
612
        if (empty($rules)) {
613
            return true;
614
        }
615
616
        $i = 0;
617
        foreach ($rules as $key => $rule) {
618
            if ($rule instanceof Closure) {
619
                $result = call_user_func_array($rule, [$value, $data]);
620
                $info   = is_numeric($key) ? '' : $key;
621
            } else {
622
                // 判断验证类型
623
                [$type, $rule, $info] = $this->getValidateType($key, $rule);
624
625
                if (isset($this->append[$field]) && in_array($info, $this->append[$field])) {
626
                } elseif (isset($this->remove[$field]) && in_array($info, $this->remove[$field])) {
627
                    // 规则已经移除
628
                    $i++;
629
                    continue;
630
                }
631
632
                if (isset($this->type[$type])) {
633
                    $result = call_user_func_array($this->type[$type], [$value, $rule, $data, $field, $title]);
634
                } elseif ('must' == $info || 0 === strpos($info, 'require') || (!is_null($value) && '' !== $value)) {
635
                    $result = call_user_func_array([$this, $type], [$value, $rule, $data, $field, $title]);
636
                } else {
637
                    $result = true;
638
                }
639
            }
640
641
            if (false === $result) {
642
                // 验证失败 返回错误信息
643
                if (!empty($msg[$i])) {
644
                    $message = $msg[$i];
645
                    if (is_string($message) && strpos($message, '{%') === 0) {
646
                        $message = $this->lang->get(substr($message, 2, -1));
647
                    }
648
                } else {
649
                    $message = $this->getRuleMsg($field, $title, $info, $rule);
650
                }
651
652
                return $message;
653
            } elseif (true !== $result) {
654
                // 返回自定义错误信息
655
                if (is_string($result) && false !== strpos($result, ':')) {
656
                    $result = str_replace(':attribute', $title, $result);
657
658
                    if (strpos($result, ':rule') && is_scalar($rule)) {
659
                        $result = str_replace(':rule', (string) $rule, $result);
660
                    }
661
                }
662
663
                return $result;
664
            }
665
            $i++;
666
        }
667
668
        return $result ?? true;
669
    }
670
671
    /**
672
     * 获取当前验证类型及规则
673
     * @access public
674
     * @param mixed $key
675
     * @param mixed $rule
676
     * @return array
677
     */
678
    protected function getValidateType($key, $rule): array
679
    {
680
        // 判断验证类型
681
        if (!is_numeric($key)) {
682
            if (isset($this->alias[$key])) {
683
                // 判断别名
684
                $key = $this->alias[$key];
685
            }
686
            return [$key, $rule, $key];
687
        }
688
689
        if (strpos($rule, ':')) {
690
            [$type, $rule] = explode(':', $rule, 2);
691
            if (isset($this->alias[$type])) {
692
                // 判断别名
693
                $type = $this->alias[$type];
694
            }
695
            $info = $type;
696
        } elseif (method_exists($this, $rule)) {
697
            $type = $rule;
698
            $info = $rule;
699
            $rule = '';
700
        } else {
701
            $type = 'is';
702
            $info = $rule;
703
        }
704
705
        return [$type, $rule, $info];
706
    }
707
708
    /**
709
     * 验证是否和某个字段的值一致
710
     * @access public
711
     * @param mixed  $value 字段值
712
     * @param mixed  $rule  验证规则
713
     * @param array  $data  数据
714
     * @param string $field 字段名
715
     * @return bool
716
     */
717
    public function confirm($value, $rule, array $data = [], string $field = ''): bool
718
    {
719
        if ('' == $rule) {
720
            if (strpos($field, '_confirm')) {
721
                $rule = strstr($field, '_confirm', true);
722
            } else {
723
                $rule = $field . '_confirm';
724
            }
725
        }
726
727
        return $this->getDataValue($data, $rule) === $value;
728
    }
729
730
    /**
731
     * 验证是否和某个字段的值是否不同
732
     * @access public
733
     * @param mixed $value 字段值
734
     * @param mixed $rule  验证规则
735
     * @param array $data  数据
736
     * @return bool
737
     */
738
    public function different($value, $rule, array $data = []): bool
739
    {
740
        return $this->getDataValue($data, $rule) != $value;
741
    }
742
743
    /**
744
     * 验证是否大于等于某个值
745
     * @access public
746
     * @param mixed $value 字段值
747
     * @param mixed $rule  验证规则
748
     * @param array $data  数据
749
     * @return bool
750
     */
751
    public function egt($value, $rule, array $data = []): bool
752
    {
753
        return $value >= $this->getDataValue($data, $rule);
754
    }
755
756
    /**
757
     * 验证是否大于某个值
758
     * @access public
759
     * @param mixed $value 字段值
760
     * @param mixed $rule  验证规则
761
     * @param array $data  数据
762
     * @return bool
763
     */
764
    public function gt($value, $rule, array $data = []): bool
765
    {
766
        return $value > $this->getDataValue($data, $rule);
767
    }
768
769
    /**
770
     * 验证是否小于等于某个值
771
     * @access public
772
     * @param mixed $value 字段值
773
     * @param mixed $rule  验证规则
774
     * @param array $data  数据
775
     * @return bool
776
     */
777
    public function elt($value, $rule, array $data = []): bool
778
    {
779
        return $value <= $this->getDataValue($data, $rule);
780
    }
781
782
    /**
783
     * 验证是否小于某个值
784
     * @access public
785
     * @param mixed $value 字段值
786
     * @param mixed $rule  验证规则
787
     * @param array $data  数据
788
     * @return bool
789
     */
790
    public function lt($value, $rule, array $data = []): bool
791
    {
792
        return $value < $this->getDataValue($data, $rule);
793
    }
794
795
    /**
796
     * 验证是否等于某个值
797
     * @access public
798
     * @param mixed $value 字段值
799
     * @param mixed $rule  验证规则
800
     * @return bool
801
     */
802
    public function eq($value, $rule): bool
803
    {
804
        return $value == $rule;
805
    }
806
807
    /**
808
     * 必须验证
809
     * @access public
810
     * @param mixed $value 字段值
811
     * @param mixed $rule  验证规则
812
     * @return bool
813
     */
814
    public function must($value, $rule = null): bool
815
    {
816
        return !empty($value) || '0' == $value;
817
    }
818
819
    /**
820
     * 验证字段值是否为有效格式
821
     * @access public
822
     * @param mixed  $value 字段值
823
     * @param string $rule  验证规则
824
     * @param array  $data  数据
825
     * @return bool
826
     */
827
    public function is($value, string $rule, array $data = []): bool
828
    {
829
        switch (Str::camel($rule)) {
830
            case 'require':
831
                // 必须
832
                $result = !empty($value) || '0' == $value;
833
                break;
834
            case 'accepted':
835
                // 接受
836
                $result = in_array($value, ['1', 'on', 'yes']);
837
                break;
838
            case 'date':
839
                // 是否是一个有效日期
840
                $result = false !== strtotime($value);
841
                break;
842
            case 'activeUrl':
843
                // 是否为有效的网址
844
                $result = checkdnsrr($value);
845
                break;
846
            case 'boolean':
847
            case 'bool':
848
                // 是否为布尔值
849
                $result = in_array($value, [true, false, 0, 1, '0', '1'], true);
850
                break;
851
            case 'number':
852
                $result = ctype_digit((string) $value);
853
                break;
854
            case 'alphaNum':
855
                $result = ctype_alnum($value);
856
                break;
857
            case 'array':
858
                // 是否为数组
859
                $result = is_array($value);
860
                break;
861
            case 'file':
862
                $result = $value instanceof File;
863
                break;
864
            case 'image':
865
                $result = $value instanceof File && in_array($this->getImageType($value->getRealPath()), [1, 2, 3, 6]);
866
                break;
867
            case 'token':
868
                $result = $this->token($value, '__token__', $data);
869
                break;
870
            default:
871
                if (isset($this->type[$rule])) {
872
                    // 注册的验证规则
873
                    $result = call_user_func_array($this->type[$rule], [$value]);
874
                } elseif (function_exists('ctype_' . $rule)) {
875
                    // ctype验证规则
876
                    $ctypeFun = 'ctype_' . $rule;
877
                    $result   = $ctypeFun($value);
878
                } elseif (isset($this->filter[$rule])) {
879
                    // Filter_var验证规则
880
                    $result = $this->filter($value, $this->filter[$rule]);
881
                } else {
882
                    // 正则验证
883
                    $result = $this->regex($value, $rule);
884
                }
885
        }
886
887
        return $result;
888
    }
889
890
    // 判断图像类型
891
    protected function getImageType($image)
892
    {
893
        if (function_exists('exif_imagetype')) {
894
            return exif_imagetype($image);
895
        }
896
897
        try {
898
            $info = getimagesize($image);
899
            return $info ? $info[2] : false;
900
        } catch (\Exception $e) {
901
            return false;
902
        }
903
    }
904
905
    /**
906
     * 验证表单令牌
907
     * @access public
908
     * @param mixed $value 字段值
909
     * @param mixed $rule  验证规则
910
     * @param array $data  数据
911
     * @return bool
912
     */
913
    public function token($value, string $rule, array $data): bool
914
    {
915
        $rule = !empty($rule) ? $rule : '__token__';
916
        return $this->request->checkToken($rule, $data);
917
    }
918
919
    /**
920
     * 验证是否为合格的域名或者IP 支持A,MX,NS,SOA,PTR,CNAME,AAAA,A6, SRV,NAPTR,TXT 或者 ANY类型
921
     * @access public
922
     * @param mixed $value 字段值
923
     * @param mixed $rule  验证规则
924
     * @return bool
925
     */
926
    public function activeUrl(string $value, string $rule = 'MX'): bool
927
    {
928
        if (!in_array($rule, ['A', 'MX', 'NS', 'SOA', 'PTR', 'CNAME', 'AAAA', 'A6', 'SRV', 'NAPTR', 'TXT', 'ANY'])) {
929
            $rule = 'MX';
930
        }
931
932
        return checkdnsrr($value, $rule);
933
    }
934
935
    /**
936
     * 验证是否有效IP
937
     * @access public
938
     * @param mixed $value 字段值
939
     * @param mixed $rule  验证规则 ipv4 ipv6
940
     * @return bool
941
     */
942
    public function ip($value, string $rule = 'ipv4'): bool
943
    {
944
        if (!in_array($rule, ['ipv4', 'ipv6'])) {
945
            $rule = 'ipv4';
946
        }
947
948
        return $this->filter($value, [FILTER_VALIDATE_IP, 'ipv6' == $rule ? FILTER_FLAG_IPV6 : FILTER_FLAG_IPV4]);
949
    }
950
951
    /**
952
     * 检测上传文件后缀
953
     * @access public
954
     * @param File         $file
955
     * @param array|string $ext 允许后缀
956
     * @return bool
957
     */
958
    protected function checkExt(File $file, $ext): bool
959
    {
960
        if (is_string($ext)) {
961
            $ext = explode(',', $ext);
962
        }
963
964
        return in_array(strtolower($file->extension()), $ext);
965
    }
966
967
    /**
968
     * 检测上传文件大小
969
     * @access public
970
     * @param File    $file
971
     * @param integer $size 最大大小
972
     * @return bool
973
     */
974
    protected function checkSize(File $file, $size): bool
975
    {
976
        return $file->getSize() <= (int) $size;
977
    }
978
979
    /**
980
     * 检测上传文件类型
981
     * @access public
982
     * @param File         $file
983
     * @param array|string $mime 允许类型
984
     * @return bool
985
     */
986
    protected function checkMime(File $file, $mime): bool
987
    {
988
        if (is_string($mime)) {
989
            $mime = explode(',', $mime);
990
        }
991
992
        return in_array(strtolower($file->getMime()), $mime);
993
    }
994
995
    /**
996
     * 验证上传文件后缀
997
     * @access public
998
     * @param mixed $file 上传文件
999
     * @param mixed $rule 验证规则
1000
     * @return bool
1001
     */
1002
    public function fileExt($file, $rule): bool
1003
    {
1004
        if (is_array($file)) {
1005
            foreach ($file as $item) {
1006
                if (!($item instanceof File) || !$this->checkExt($item, $rule)) {
1007
                    return false;
1008
                }
1009
            }
1010
            return true;
1011
        } elseif ($file instanceof File) {
1012
            return $this->checkExt($file, $rule);
1013
        }
1014
1015
        return false;
1016
    }
1017
1018
    /**
1019
     * 验证上传文件类型
1020
     * @access public
1021
     * @param mixed $file 上传文件
1022
     * @param mixed $rule 验证规则
1023
     * @return bool
1024
     */
1025
    public function fileMime($file, $rule): bool
1026
    {
1027
        if (is_array($file)) {
1028
            foreach ($file as $item) {
1029
                if (!($item instanceof File) || !$this->checkMime($item, $rule)) {
1030
                    return false;
1031
                }
1032
            }
1033
            return true;
1034
        } elseif ($file instanceof File) {
1035
            return $this->checkMime($file, $rule);
1036
        }
1037
1038
        return false;
1039
    }
1040
1041
    /**
1042
     * 验证上传文件大小
1043
     * @access public
1044
     * @param mixed $file 上传文件
1045
     * @param mixed $rule 验证规则
1046
     * @return bool
1047
     */
1048
    public function fileSize($file, $rule): bool
1049
    {
1050
        if (is_array($file)) {
1051
            foreach ($file as $item) {
1052
                if (!($item instanceof File) || !$this->checkSize($item, $rule)) {
1053
                    return false;
1054
                }
1055
            }
1056
            return true;
1057
        } elseif ($file instanceof File) {
1058
            return $this->checkSize($file, $rule);
1059
        }
1060
1061
        return false;
1062
    }
1063
1064
    /**
1065
     * 验证图片的宽高及类型
1066
     * @access public
1067
     * @param mixed $file 上传文件
1068
     * @param mixed $rule 验证规则
1069
     * @return bool
1070
     */
1071
    public function image($file, $rule): bool
1072
    {
1073
        if (!($file instanceof File)) {
1074
            return false;
1075
        }
1076
1077
        if ($rule) {
1078
            $rule = explode(',', $rule);
1079
1080
            [$width, $height, $type] = getimagesize($file->getRealPath());
1081
1082
            if (isset($rule[2])) {
1083
                $imageType = strtolower($rule[2]);
1084
1085
                if ('jpg' == $imageType) {
1086
                    $imageType = 'jpeg';
1087
                }
1088
1089
                if (image_type_to_extension($type, false) != $imageType) {
1090
                    return false;
1091
                }
1092
            }
1093
1094
            [$w, $h] = $rule;
1095
1096
            return $w == $width && $h == $height;
1097
        }
1098
1099
        return in_array($this->getImageType($file->getRealPath()), [1, 2, 3, 6]);
1100
    }
1101
1102
    /**
1103
     * 验证时间和日期是否符合指定格式
1104
     * @access public
1105
     * @param mixed $value 字段值
1106
     * @param mixed $rule  验证规则
1107
     * @return bool
1108
     */
1109
    public function dateFormat($value, $rule): bool
1110
    {
1111
        $info = date_parse_from_format($rule, $value);
1112
        return 0 == $info['warning_count'] && 0 == $info['error_count'];
1113
    }
1114
1115
    /**
1116
     * 验证是否唯一
1117
     * @access public
1118
     * @param mixed  $value 字段值
1119
     * @param mixed  $rule  验证规则 格式:数据表,字段名,排除ID,主键名
1120
     * @param array  $data  数据
1121
     * @param string $field 验证字段名
1122
     * @return bool
1123
     */
1124
    public function unique($value, $rule, array $data = [], string $field = ''): bool
1125
    {
1126
        if (is_string($rule)) {
1127
            $rule = explode(',', $rule);
1128
        }
1129
1130
        if (false !== strpos($rule[0], '\\')) {
1131
            // 指定模型类
1132
            $db = new $rule[0];
1133
        } else {
1134
            $db = $this->db->name($rule[0]);
0 ignored issues
show
Bug introduced by
The method name() does not exist on think\Db. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1134
            /** @scrutinizer ignore-call */ 
1135
            $db = $this->db->name($rule[0]);
Loading history...
1135
        }
1136
1137
        $key = $rule[1] ?? $field;
1138
        $map = [];
1139
1140
        if (strpos($key, '^')) {
1141
            // 支持多个字段验证
1142
            $fields = explode('^', $key);
1143
            $map_arr=[];
1144
            foreach ($fields as $k) {
1145
                if (isset($data[$k])) {
1146
                    $map[] = [$k, '=', $data[$k]];
1147
                    $map_arr[]=$k;
1148
                }
1149
            }
1150
            if(!in_array($field, $map_arr)){
1151
                $map[] = [$field, '=', $data[$field]];
1152
            }
1153
        } elseif (strpos($key, '=')) {
1154
            // 支持复杂验证条件
1155
            $fields = explode('&', $key);
1156
            $map_arr=[];
1157
            foreach ($fields as $k) {
1158
                //判断验证条件是否传参,没有传参就使用$data中对应的值
1159
                if (strpos($k, '=')) {
1160
                    $str_map = explode('=', $k);
1161
                    $map[] = [$str_map[0], '=', $str_map[1]];
1162
                    $map_arr[]=$str_map[0];
1163
                }else{
1164
                    $map[] = [$k, '=', $data[$k]];
1165
                    $map_arr[]=$k;
1166
                }
1167
            }
1168
            if(!in_array($field, $map_arr)){
1169
                $map[] = [$field, '=', $data[$field]];
1170
            }
1171
        } elseif (isset($data[$field])) {
1172
            $map[] = [$key, '=', $data[$field]];
1173
        } else {
1174
            $map = [];
1175
        }
1176
1177
        $pk = !empty($rule[3]) ? $rule[3] : $db->getPk();
1178
1179
        if (is_string($pk)) {
1180
            if (isset($rule[2])) {
1181
                $map[] = [$pk, '<>', $rule[2]];
1182
            } elseif (isset($data[$pk])) {
1183
                $map[] = [$pk, '<>', $data[$pk]];
1184
            }
1185
        }
1186
1187
        if ($db->where($map)->field($pk)->find()) {
1188
            return false;
1189
        }
1190
1191
        return true;
1192
    }
1193
1194
    /**
1195
     * 使用filter_var方式验证
1196
     * @access public
1197
     * @param mixed $value 字段值
1198
     * @param mixed $rule  验证规则
1199
     * @return bool
1200
     */
1201
    public function filter($value, $rule): bool
1202
    {
1203
        if (is_string($rule) && strpos($rule, ',')) {
1204
            [$rule, $param] = explode(',', $rule);
1205
        } elseif (is_array($rule)) {
1206
            $param = $rule[1] ?? 0;
1207
            $rule  = $rule[0];
1208
        } else {
1209
            $param = 0;
1210
        }
1211
1212
        return false !== filter_var($value, is_int($rule) ? $rule : filter_id($rule), $param);
1213
    }
1214
1215
    /**
1216
     * 验证某个字段等于某个值的时候必须
1217
     * @access public
1218
     * @param mixed $value 字段值
1219
     * @param mixed $rule  验证规则
1220
     * @param array $data  数据
1221
     * @return bool
1222
     */
1223
    public function requireIf($value, $rule, array $data = []): bool
1224
    {
1225
        [$field, $val] = explode(',', $rule);
1226
1227
        if ($this->getDataValue($data, $field) == $val) {
1228
            return !empty($value) || '0' == $value;
1229
        }
1230
1231
        return true;
1232
    }
1233
1234
    /**
1235
     * 通过回调方法验证某个字段是否必须
1236
     * @access public
1237
     * @param mixed $value 字段值
1238
     * @param mixed $rule  验证规则
1239
     * @param array $data  数据
1240
     * @return bool
1241
     */
1242
    public function requireCallback($value, $rule, array $data = []): bool
1243
    {
1244
        $result = call_user_func_array([$this, $rule], [$value, $data]);
1245
1246
        if ($result) {
1247
            return !empty($value) || '0' == $value;
1248
        }
1249
1250
        return true;
1251
    }
1252
1253
    /**
1254
     * 验证某个字段有值的情况下必须
1255
     * @access public
1256
     * @param mixed $value 字段值
1257
     * @param mixed $rule  验证规则
1258
     * @param array $data  数据
1259
     * @return bool
1260
     */
1261
    public function requireWith($value, $rule, array $data = []): bool
1262
    {
1263
        $val = $this->getDataValue($data, $rule);
1264
1265
        if (!empty($val)) {
1266
            return !empty($value) || '0' == $value;
1267
        }
1268
1269
        return true;
1270
    }
1271
1272
    /**
1273
     * 验证某个字段没有值的情况下必须
1274
     * @access public
1275
     * @param mixed $value 字段值
1276
     * @param mixed $rule  验证规则
1277
     * @param array $data  数据
1278
     * @return bool
1279
     */
1280
    public function requireWithout($value, $rule, array $data = []): bool
1281
    {
1282
        $val = $this->getDataValue($data, $rule);
1283
1284
        if (empty($val)) {
1285
            return !empty($value) || '0' == $value;
1286
        }
1287
1288
        return true;
1289
    }
1290
1291
    /**
1292
     * 验证是否在范围内
1293
     * @access public
1294
     * @param mixed $value 字段值
1295
     * @param mixed $rule  验证规则
1296
     * @return bool
1297
     */
1298
    public function in($value, $rule): bool
1299
    {
1300
        return in_array($value, is_array($rule) ? $rule : explode(',', $rule));
1301
    }
1302
1303
    /**
1304
     * 验证是否不在某个范围
1305
     * @access public
1306
     * @param mixed $value 字段值
1307
     * @param mixed $rule  验证规则
1308
     * @return bool
1309
     */
1310
    public function notIn($value, $rule): bool
1311
    {
1312
        return !in_array($value, is_array($rule) ? $rule : explode(',', $rule));
1313
    }
1314
1315
    /**
1316
     * between验证数据
1317
     * @access public
1318
     * @param mixed $value 字段值
1319
     * @param mixed $rule  验证规则
1320
     * @return bool
1321
     */
1322
    public function between($value, $rule): bool
1323
    {
1324
        if (is_string($rule)) {
1325
            $rule = explode(',', $rule);
1326
        }
1327
        [$min, $max] = $rule;
1328
1329
        return $value >= $min && $value <= $max;
1330
    }
1331
1332
    /**
1333
     * 使用notbetween验证数据
1334
     * @access public
1335
     * @param mixed $value 字段值
1336
     * @param mixed $rule  验证规则
1337
     * @return bool
1338
     */
1339
    public function notBetween($value, $rule): bool
1340
    {
1341
        if (is_string($rule)) {
1342
            $rule = explode(',', $rule);
1343
        }
1344
        [$min, $max] = $rule;
1345
1346
        return $value < $min || $value > $max;
1347
    }
1348
1349
    /**
1350
     * 验证数据长度
1351
     * @access public
1352
     * @param mixed $value 字段值
1353
     * @param mixed $rule  验证规则
1354
     * @return bool
1355
     */
1356
    public function length($value, $rule): bool
1357
    {
1358
        if (is_array($value)) {
1359
            $length = count($value);
1360
        } elseif ($value instanceof File) {
1361
            $length = $value->getSize();
1362
        } else {
1363
            $length = mb_strlen((string) $value);
1364
        }
1365
1366
        if (is_string($rule) && strpos($rule, ',')) {
1367
            // 长度区间
1368
            [$min, $max] = explode(',', $rule);
1369
            return $length >= $min && $length <= $max;
1370
        }
1371
1372
        // 指定长度
1373
        return $length == $rule;
1374
    }
1375
1376
    /**
1377
     * 验证数据最大长度
1378
     * @access public
1379
     * @param mixed $value 字段值
1380
     * @param mixed $rule  验证规则
1381
     * @return bool
1382
     */
1383
    public function max($value, $rule): bool
1384
    {
1385
        if (is_array($value)) {
1386
            $length = count($value);
1387
        } elseif ($value instanceof File) {
1388
            $length = $value->getSize();
1389
        } else {
1390
            $length = mb_strlen((string) $value);
1391
        }
1392
1393
        return $length <= $rule;
1394
    }
1395
1396
    /**
1397
     * 验证数据最小长度
1398
     * @access public
1399
     * @param mixed $value 字段值
1400
     * @param mixed $rule  验证规则
1401
     * @return bool
1402
     */
1403
    public function min($value, $rule): bool
1404
    {
1405
        if (is_array($value)) {
1406
            $length = count($value);
1407
        } elseif ($value instanceof File) {
1408
            $length = $value->getSize();
1409
        } else {
1410
            $length = mb_strlen((string) $value);
1411
        }
1412
1413
        return $length >= $rule;
1414
    }
1415
1416
    /**
1417
     * 验证日期
1418
     * @access public
1419
     * @param mixed $value 字段值
1420
     * @param mixed $rule  验证规则
1421
     * @param array $data  数据
1422
     * @return bool
1423
     */
1424
    public function after($value, $rule, array $data = []): bool
1425
    {
1426
        return strtotime($value) >= strtotime($rule);
1427
    }
1428
1429
    /**
1430
     * 验证日期
1431
     * @access public
1432
     * @param mixed $value 字段值
1433
     * @param mixed $rule  验证规则
1434
     * @param array $data  数据
1435
     * @return bool
1436
     */
1437
    public function before($value, $rule, array $data = []): bool
1438
    {
1439
        return strtotime($value) <= strtotime($rule);
1440
    }
1441
1442
    /**
1443
     * 验证日期
1444
     * @access public
1445
     * @param mixed $value 字段值
1446
     * @param mixed $rule  验证规则
1447
     * @param array $data  数据
1448
     * @return bool
1449
     */
1450
    public function afterWith($value, $rule, array $data = []): bool
1451
    {
1452
        $rule = $this->getDataValue($data, $rule);
1453
        return !is_null($rule) && strtotime($value) >= strtotime($rule);
1454
    }
1455
1456
    /**
1457
     * 验证日期
1458
     * @access public
1459
     * @param mixed $value 字段值
1460
     * @param mixed $rule  验证规则
1461
     * @param array $data  数据
1462
     * @return bool
1463
     */
1464
    public function beforeWith($value, $rule, array $data = []): bool
1465
    {
1466
        $rule = $this->getDataValue($data, $rule);
1467
        return !is_null($rule) && strtotime($value) <= strtotime($rule);
1468
    }
1469
1470
    /**
1471
     * 验证有效期
1472
     * @access public
1473
     * @param mixed $value 字段值
1474
     * @param mixed $rule  验证规则
1475
     * @return bool
1476
     */
1477
    public function expire($value, $rule): bool
1478
    {
1479
        if (is_string($rule)) {
1480
            $rule = explode(',', $rule);
1481
        }
1482
1483
        [$start, $end] = $rule;
1484
1485
        if (!is_numeric($start)) {
1486
            $start = strtotime($start);
1487
        }
1488
1489
        if (!is_numeric($end)) {
1490
            $end = strtotime($end);
1491
        }
1492
1493
        return time() >= $start && time() <= $end;
1494
    }
1495
1496
    /**
1497
     * 验证IP许可
1498
     * @access public
1499
     * @param mixed $value 字段值
1500
     * @param mixed $rule  验证规则
1501
     * @return bool
1502
     */
1503
    public function allowIp($value, $rule): bool
1504
    {
1505
        return in_array($value, is_array($rule) ? $rule : explode(',', $rule));
1506
    }
1507
1508
    /**
1509
     * 验证IP禁用
1510
     * @access public
1511
     * @param mixed $value 字段值
1512
     * @param mixed $rule  验证规则
1513
     * @return bool
1514
     */
1515
    public function denyIp($value, $rule): bool
1516
    {
1517
        return !in_array($value, is_array($rule) ? $rule : explode(',', $rule));
1518
    }
1519
1520
    /**
1521
     * 使用正则验证数据
1522
     * @access public
1523
     * @param mixed $value 字段值
1524
     * @param mixed $rule  验证规则 正则规则或者预定义正则名
1525
     * @return bool
1526
     */
1527
    public function regex($value, $rule): bool
1528
    {
1529
        if (isset($this->regex[$rule])) {
1530
            $rule = $this->regex[$rule];
1531
        } elseif (isset($this->defaultRegex[$rule])) {
1532
            $rule = $this->defaultRegex[$rule];
1533
        }
1534
1535
        if (is_string($rule) && 0 !== strpos($rule, '/') && !preg_match('/\/[imsU]{0,4}$/', $rule)) {
1536
            // 不是正则表达式则两端补上/
1537
            $rule = '/^' . $rule . '$/';
1538
        }
1539
1540
        return is_scalar($value) && 1 === preg_match($rule, (string) $value);
1541
    }
1542
1543
    /**
1544
     * 获取错误信息
1545
     * @return array|string
1546
     */
1547
    public function getError()
1548
    {
1549
        return $this->error;
1550
    }
1551
1552
    /**
1553
     * 获取数据值
1554
     * @access protected
1555
     * @param array  $data 数据
1556
     * @param string $key  数据标识 支持二维
1557
     * @return mixed
1558
     */
1559
    protected function getDataValue(array $data, $key)
1560
    {
1561
        if (is_numeric($key)) {
1562
            $value = $key;
1563
        } elseif (is_string($key) && strpos($key, '.')) {
1564
            // 支持多维数组验证
1565
            foreach (explode('.', $key) as $key) {
0 ignored issues
show
introduced by
$key is overwriting one of the parameters of this function.
Loading history...
1566
                if (!isset($data[$key])) {
1567
                    $value = null;
1568
                    break;
1569
                }
1570
                $value = $data = $data[$key];
1571
            }
1572
        } else {
1573
            $value = $data[$key] ?? null;
1574
        }
1575
1576
        return $value;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $value does not seem to be defined for all execution paths leading up to this point.
Loading history...
1577
    }
1578
1579
    /**
1580
     * 获取验证规则的错误提示信息
1581
     * @access protected
1582
     * @param string $attribute 字段英文名
1583
     * @param string $title     字段描述名
1584
     * @param string $type      验证规则名称
1585
     * @param mixed  $rule      验证规则数据
1586
     * @return string|array
1587
     */
1588
    protected function getRuleMsg(string $attribute, string $title, string $type, $rule)
1589
    {
1590
        if (isset($this->message[$attribute . '.' . $type])) {
1591
            $msg = $this->message[$attribute . '.' . $type];
1592
        } elseif (isset($this->message[$attribute][$type])) {
1593
            $msg = $this->message[$attribute][$type];
1594
        } elseif (isset($this->message[$attribute])) {
1595
            $msg = $this->message[$attribute];
1596
        } elseif (isset($this->typeMsg[$type])) {
1597
            $msg = $this->typeMsg[$type];
1598
        } elseif (0 === strpos($type, 'require')) {
1599
            $msg = $this->typeMsg['require'];
1600
        } else {
1601
            $msg = $title . $this->lang->get('not conform to the rules');
1602
        }
1603
1604
        if (is_array($msg)) {
1605
            return $this->errorMsgIsArray($msg, $rule, $title);
1606
        }
1607
1608
        return $this->parseErrorMsg($msg, $rule, $title);
1609
    }
1610
1611
    /**
1612
     * 获取验证规则的错误提示信息
1613
     * @access protected
1614
     * @param string $msg   错误信息
1615
     * @param mixed  $rule  验证规则数据
1616
     * @param string $title 字段描述名
1617
     * @return string|array
1618
     */
1619
    protected function parseErrorMsg(string $msg, $rule, string $title)
1620
    {
1621
        if (0 === strpos($msg, '{%')) {
1622
            $msg = $this->lang->get(substr($msg, 2, -1));
1623
        } elseif ($this->lang->has($msg)) {
1624
            $msg = $this->lang->get($msg);
1625
        }
1626
1627
        if (is_array($msg)) {
1628
            return $this->errorMsgIsArray($msg, $rule, $title);
1629
        }
1630
1631
        // rule若是数组则转为字符串
1632
        if (is_array($rule)) {
1633
            $rule = implode(',', $rule);
1634
        }
1635
1636
        if (is_scalar($rule) && false !== strpos($msg, ':')) {
1637
            // 变量替换
1638
            if (is_string($rule) && strpos($rule, ',')) {
1639
                $array = array_pad(explode(',', $rule), 3, '');
1640
            } else {
1641
                $array = array_pad([], 3, '');
1642
            }
1643
            $msg = str_replace(
1644
                [':attribute', ':1', ':2', ':3'],
1645
                [$title, $array[0], $array[1], $array[2]],
1646
                $msg
1647
            );
1648
1649
            if (strpos($msg, ':rule')) {
1650
                $msg = str_replace(':rule', (string) $rule, $msg);
1651
            }
1652
        }
1653
1654
        return $msg;
1655
    }
1656
1657
    /**
1658
     * 错误信息数组处理
1659
     * @access protected
1660
     * @param array $msg   错误信息
1661
     * @param mixed  $rule  验证规则数据
1662
     * @param string $title 字段描述名
1663
     * @return array
1664
     */
1665
    protected function errorMsgIsArray(array $msg, $rule, string $title)
1666
    {
1667
        foreach ($msg as $key => $val) {
1668
            if (is_string($val)) {
1669
                $msg[$key] = $this->parseErrorMsg($val, $rule, $title);
1670
            }
1671
        }
1672
        return $msg;
1673
    }
1674
1675
    /**
1676
     * 获取数据验证的场景
1677
     * @access protected
1678
     * @param string $scene 验证场景
1679
     * @return void
1680
     */
1681
    protected function getScene(string $scene): void
1682
    {
1683
        $this->only = $this->append = $this->remove = [];
1684
1685
        if (method_exists($this, 'scene' . $scene)) {
1686
            call_user_func([$this, 'scene' . $scene]);
1687
        } elseif (isset($this->scene[$scene])) {
1688
            // 如果设置了验证适用场景
1689
            $this->only = $this->scene[$scene];
1690
        }
1691
    }
1692
1693
    /**
1694
     * 动态方法 直接调用is方法进行验证
1695
     * @access public
1696
     * @param string $method 方法名
1697
     * @param array  $args   调用参数
1698
     * @return bool
1699
     */
1700
    public function __call($method, $args)
1701
    {
1702
        if ('is' == strtolower(substr($method, 0, 2))) {
1703
            $method = substr($method, 2);
1704
        }
1705
1706
        array_push($args, lcfirst($method));
1707
1708
        return call_user_func_array([$this, 'is'], $args);
1709
    }
1710
}
1711