1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// +---------------------------------------------------------------------- |
4
|
|
|
// | ThinkLibrary 6.0 for ThinkPhP 6.0 |
5
|
|
|
// +---------------------------------------------------------------------- |
6
|
|
|
// | 版权所有 2017~2020 [ https://www.dtapp.net ] |
7
|
|
|
// +---------------------------------------------------------------------- |
8
|
|
|
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary |
9
|
|
|
// +---------------------------------------------------------------------- |
10
|
|
|
// | 开源协议 ( https://mit-license.org ) |
11
|
|
|
// +---------------------------------------------------------------------- |
12
|
|
|
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary |
13
|
|
|
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary |
14
|
|
|
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary |
15
|
|
|
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary |
16
|
|
|
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git |
17
|
|
|
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library |
18
|
|
|
// +---------------------------------------------------------------------- |
19
|
|
|
|
20
|
|
|
namespace DtApp\ThinkLibrary\helper; |
21
|
|
|
|
22
|
|
|
use DtApp\ThinkLibrary\Helper; |
23
|
|
|
use think\Validate; |
24
|
|
|
|
25
|
|
|
class ValidateHelper extends Helper |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* 快捷输入并验证( 支持 规则 # 别名 ) |
29
|
|
|
* |
30
|
|
|
* age.require => message // 最大值限定 |
31
|
|
|
* age.between:1,120 => message // 范围限定 |
32
|
|
|
* name.require => message // 必填内容 |
33
|
|
|
* name.default => 100 // 获取并设置默认值 |
34
|
|
|
* region.value => value // 固定字段数值内容 |
35
|
|
|
* 更多规则参照 ThinkPHP 官方的验证类 |
36
|
|
|
* |
37
|
|
|
* @param array $rules 验证规则( 验证信息数组 ) |
38
|
|
|
* @param string $input 输入内容 ( post. 或 get. ) |
39
|
|
|
* @param callable|null $callable 异常处理操作 |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function init(array $rules, $input = '', ?callable $callable = null): array |
43
|
|
|
{ |
44
|
|
|
if (is_string($input)) { |
|
|
|
|
45
|
|
|
$type = trim($input, '.') ?: 'request'; |
46
|
|
|
$input = $this->app->request->$type(); |
47
|
|
|
} |
48
|
|
|
[$data, $rule, $info] = [[], [], []]; |
49
|
|
|
foreach ($rules as $name => $message) { |
50
|
|
|
if (is_numeric($name)) { |
51
|
|
|
[$name, $alias] = explode('#', $message . '#'); |
52
|
|
|
$data[$name] = $input[($alias ?: $name)] ?? null; |
53
|
|
|
} elseif (strpos($name, '.') === false) { |
54
|
|
|
$data[$name] = $message; |
55
|
|
|
} elseif (preg_match('|^(.*?)\.(.*?)#(.*?)#?$|', $name . '#', $matches)) { |
56
|
|
|
[, $_key, $_rule, $alias] = $matches; |
57
|
|
|
if (in_array($_rule, ['value', 'default'])) { |
58
|
|
|
if ($_rule === 'value') { |
59
|
|
|
$data[$_key] = $message; |
60
|
|
|
} elseif ($_rule === 'default') { |
61
|
|
|
$data[$_key] = $input[($alias ?: $_key)] ?? $message; |
62
|
|
|
} |
63
|
|
|
} else { |
64
|
|
|
$info[explode(':', $name)[0]] = $message; |
65
|
|
|
$data[$_key] = $data[$_key] ?? ($input[($alias ?: $_key)] ?? null); |
66
|
|
|
$rule[$_key] = isset($rule[$_key]) ? ($rule[$_key] . '|' . $_rule) : $_rule; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
$validate = new Validate(); |
71
|
|
|
if ($validate->rule($rule)->message($info)->check($data)) { |
72
|
|
|
return $data; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (is_callable($callable)) { |
76
|
|
|
return $callable($validate->getError()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->class->error($validate->getError()); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
} |