1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace icy2003\php\ihelpers; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use icy2003\php\I; |
7
|
|
|
|
8
|
|
|
class Validator |
9
|
|
|
{ |
10
|
|
|
protected static $_instance; |
11
|
|
|
private $__data = []; |
12
|
|
|
private $__old_data = []; |
13
|
|
|
private $__safeField = []; |
14
|
|
|
private $__messages = []; |
15
|
|
|
private $__codes = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var self 验证成功 |
19
|
|
|
*/ |
20
|
|
|
const CODE_SUCCEEDED = 0; |
21
|
|
|
/** |
22
|
|
|
* @var self 验证失败 |
23
|
|
|
*/ |
24
|
|
|
const CODE_VALIDATE_FAILED = -1; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var self 必填验证失败 |
28
|
|
|
*/ |
29
|
|
|
const CODE_VALIDATE_REQUIRED = -2; |
30
|
|
|
/** |
31
|
|
|
* @var self 范围验证失败 |
32
|
|
|
*/ |
33
|
|
|
const CODE_VALIDATE_IN = -3; |
34
|
|
|
/** |
35
|
|
|
* @var self 正则验证失败 |
36
|
|
|
*/ |
37
|
|
|
const CODE_VALIDATE_MATCH = -4; |
38
|
|
|
/** |
39
|
|
|
* @var self 手机号格式验证失败 |
40
|
|
|
*/ |
41
|
|
|
const CODE_VALIDATE_MOBILE = -5; |
42
|
|
|
/** |
43
|
|
|
* @var self 邮箱格式验证失败 |
44
|
|
|
*/ |
45
|
|
|
const CODE_VALIDATE_EMAIL = -6; |
46
|
|
|
/** |
47
|
|
|
* @var self 唯一性验证失败 |
48
|
|
|
*/ |
49
|
|
|
const CODE_VALIDATE_UNIQUE = -7; |
50
|
|
|
/** |
51
|
|
|
* @var self 回调验证失败 |
52
|
|
|
*/ |
53
|
|
|
const CODE_VALIDATE_CALL = -8; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var self 必填验证器 |
57
|
|
|
*/ |
58
|
|
|
const VALIDATOR_REQUIRED = '_required'; |
59
|
|
|
/** |
60
|
|
|
* @var self 范围验证器 |
61
|
|
|
*/ |
62
|
|
|
const VALIDATOR_IN = '_in'; |
63
|
|
|
/** |
64
|
|
|
* @var self 正则验证器 |
65
|
|
|
*/ |
66
|
|
|
const VALIDATOR_MATCH = '_match'; |
67
|
|
|
/** |
68
|
|
|
* @var self 手机号格式验证器 |
69
|
|
|
*/ |
70
|
|
|
const VALIDATOR_MOBILE = '_mobile'; |
71
|
|
|
/** |
72
|
|
|
* @var self 邮箱格式验证器 |
73
|
|
|
*/ |
74
|
|
|
const VALIDATOR_EMAIL = '_email'; |
75
|
|
|
/** |
76
|
|
|
* @var self 唯一性验证器 |
77
|
|
|
*/ |
78
|
|
|
const VALIDATOR_UNIQUE = '_unique'; |
79
|
|
|
/** |
80
|
|
|
* @var self 回调验证器 |
81
|
|
|
*/ |
82
|
|
|
const VALIDATOR_CALL = '_call'; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var self 默认值过滤器 |
86
|
|
|
*/ |
87
|
|
|
const FILTER_DEFAULT = '_default'; |
88
|
|
|
/** |
89
|
|
|
* @var self 设置过滤器 |
90
|
|
|
*/ |
91
|
|
|
const FILTER_SET = '_set'; |
92
|
|
|
/** |
93
|
|
|
* @var self 回调过滤器 |
94
|
|
|
*/ |
95
|
|
|
const FILTER_FILTER = '_filter'; |
96
|
|
|
/** |
97
|
|
|
* @var self 安全过滤器 |
98
|
|
|
*/ |
99
|
|
|
const FILTER_SAFE = '_safe'; |
100
|
|
|
/** |
101
|
|
|
* @var self 删除过滤器 |
102
|
|
|
*/ |
103
|
|
|
const FILTER_UNSET = '_unset'; |
104
|
|
|
|
105
|
|
|
private function __construct() |
106
|
|
|
{ |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function __clone() |
110
|
|
|
{ |
111
|
|
|
} |
112
|
|
|
/** |
113
|
|
|
* 创建一个验证器 |
114
|
|
|
* |
115
|
|
|
* @return static |
116
|
|
|
*/ |
117
|
|
|
public static function create() |
118
|
|
|
{ |
119
|
|
|
if (!static::$_instance instanceof static) { |
120
|
|
|
static::$_instance = new static(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return static::$_instance; |
124
|
|
|
} |
125
|
|
|
/** |
126
|
|
|
* 预加载数据 |
127
|
|
|
* |
128
|
|
|
* @param array $data |
129
|
|
|
* @return static |
130
|
|
|
*/ |
131
|
|
|
public function load($data) |
132
|
|
|
{ |
133
|
|
|
$this->__data = $data; |
134
|
|
|
$this->__old_data = $data; |
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
protected function _clear() |
139
|
|
|
{ |
140
|
|
|
$this->__messages = []; |
141
|
|
|
} |
142
|
|
|
/** |
143
|
|
|
* 验证规则 |
144
|
|
|
* |
145
|
|
|
* @param array $rules |
146
|
|
|
* @return static |
147
|
|
|
*/ |
148
|
|
|
public function rules($rules) |
149
|
|
|
{ |
150
|
|
|
$this->_clear(); |
151
|
|
|
if (!empty($rules)) { |
152
|
|
|
foreach ($rules as $rule) { |
153
|
|
|
if (!Arrays::keyExistsAll([0, 1], $rule)) { |
154
|
|
|
throw new Exception('rules error'); |
155
|
|
|
} |
156
|
|
|
$fieldArray = is_array($rule[0]) ? $rule[0] : explode(',', $rule[0]); |
157
|
|
|
$ruleName = $rule[1]; |
158
|
|
|
$method = $ruleName . 'Validator'; |
159
|
|
|
if (method_exists($this, $method)) { |
160
|
|
|
foreach ($fieldArray as $field) { |
161
|
|
|
array_push($this->__safeField, $field); |
162
|
|
|
$this->$method($this->__old_data, $field, $rule); |
163
|
|
|
} |
164
|
|
|
} else { |
165
|
|
|
echo $method; |
166
|
|
|
throw new Exception('method error'); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
protected function _isEmpty($data) |
174
|
|
|
{ |
175
|
|
|
return empty($data); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
protected function _requiredValidator($data, $field, $rule) |
179
|
|
|
{ |
180
|
|
|
if (null === I::get($data, $field)) { |
181
|
|
|
$this->__messages[$field][] = I::get($rule, 'message', $field . ' 必填'); |
182
|
|
|
$this->__codes[$field][] = I::get($rule, 'code', self::CODE_VALIDATE_REQUIRED); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
protected function _inValidator($data, $field, $rule) |
187
|
|
|
{ |
188
|
|
|
if (!array_key_exists('range', $rule)) { |
189
|
|
|
throw new Exception('range error'); |
190
|
|
|
} |
191
|
|
|
$value = I::get($data, $field); |
192
|
|
|
$range = (array)I::get($rule, 'range', []); |
193
|
|
|
$isStrict = (bool)I::get($rule, 'isStrict', false); |
194
|
|
|
if (!in_array($value, $range, $isStrict)) { |
195
|
|
|
$this->__messages[$field][] = I::get($rule, 'message', $field . ' 不在范围内'); |
196
|
|
|
$this->__codes[$field][] = I::get($rule, 'code', self::CODE_VALIDATE_IN); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
protected function _matchValidator($data, $field, $rule) |
201
|
|
|
{ |
202
|
|
|
if (!array_key_exists('pattern', $rule)) { |
203
|
|
|
throw new Exception('pattern error'); |
204
|
|
|
} |
205
|
|
|
$value = (string)I::get($data, $field); |
206
|
|
|
$pattern = (string)I::get($rule, 'pattern', '//'); |
207
|
|
|
if (!preg_match($pattern, $value)) { |
208
|
|
|
$this->__messages[$field][] = I::get($rule, 'message', $field . ' 格式不正确'); |
209
|
|
|
$this->__codes[$field][] = I::get($rule, 'code', self::CODE_VALIDATE_MATCH); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
protected function _mobileValidator($data, $field, $rule) |
214
|
|
|
{ |
215
|
|
|
$rule['pattern'] = '/^1\\d{10}$/'; |
216
|
|
|
$rule['message'] = I::get($rule, 'message', $field . ' 手机号格式不正确'); |
217
|
|
|
$rule['code'] = I::get($rule, 'code', self::CODE_VALIDATE_MOBILE); |
218
|
|
|
$this->_matchValidator($data, $field, $rule); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
protected function _emailValidator($data, $field, $rule) |
222
|
|
|
{ |
223
|
|
|
$rule['pattern'] = '/^[\w\-\.]+@[\w\-]+(\.\w+)+$/'; |
224
|
|
|
$rule['message'] = I::get($rule, 'message', $field . ' 邮箱格式不正确'); |
225
|
|
|
$rule['code'] = I::get($rule, 'code', self::CODE_VALIDATE_EMAIL); |
226
|
|
|
$this->_matchValidator($data, $field, $rule); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
protected function _uniqueValidator($data, $field, $rule) |
230
|
|
|
{ |
231
|
|
|
$value = I::get($data, $field); |
232
|
|
|
if (array_key_exists('model', $rule)) { |
233
|
|
|
|
234
|
|
|
} else { |
235
|
|
|
$function = I::get($rule, 'function'); |
236
|
|
|
if (!is_callable($function)) { |
237
|
|
|
throw new Exception('function error'); |
238
|
|
|
} |
239
|
|
|
if (!$function($value)) { |
240
|
|
|
$this->__messages[$field][] = I::get($rule, 'message', $field . ' 不唯一'); |
241
|
|
|
$this->__codes[$field][] = I::get($rule, 'code', self::CODE_VALIDATE_UNIQUE); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
protected function _callValidator($data, $field, $rule) |
247
|
|
|
{ |
248
|
|
|
if (!array_key_exists('function', $rule)) { |
249
|
|
|
throw new Exception('function error'); |
250
|
|
|
} |
251
|
|
|
$function = I::get($rule, 'function'); |
252
|
|
|
if (!is_callable($function)) { |
253
|
|
|
throw new Exception('function call error'); |
254
|
|
|
} |
255
|
|
|
if (!$function(I::get($data, $field))) { |
256
|
|
|
$this->__messages[$field][] = I::get($rule, 'message', $field . ' 验证不通过'); |
257
|
|
|
$this->__codes[$field][] = I::get($rule, 'code', self::CODE_VALIDATE_CALL); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
protected function _defaultValidator($data, $field, $rule) |
262
|
|
|
{ |
263
|
|
|
if (!array_key_exists('value', $rule)) { |
264
|
|
|
throw new Exception('value error'); |
265
|
|
|
} |
266
|
|
|
$value = I::get($rule, 'value'); |
267
|
|
|
$isStrict = I::get($rule, 'isStrict', false); |
268
|
|
|
$defaultValue = is_callable($value) ? $value() : $value; |
269
|
|
|
if (true === $isStrict) { |
270
|
|
|
$this->__data[$field] = I::get($data, $field, $defaultValue); |
271
|
|
|
} else { |
272
|
|
|
$this->__data[$field] = !empty($data[$field]) ? $data[$field] : $defaultValue; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
protected function _setValidator($data, $field, $rule) |
277
|
|
|
{ |
278
|
|
|
if (!array_key_exists('value', $rule)) { |
279
|
|
|
throw new Exception('value error'); |
280
|
|
|
} |
281
|
|
|
$value = I::get($rule, 'value'); |
282
|
|
|
$this->__data[$field] = is_callable($value) ? $value() : $value; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
protected function _filterValidator($data, $field, $rule) |
286
|
|
|
{ |
287
|
|
|
if (!array_key_exists('function', $rule)) { |
288
|
|
|
throw new Exception('function error'); |
289
|
|
|
} |
290
|
|
|
$function = I::get($rule, 'function'); |
291
|
|
|
if (!is_callable($function)) { |
292
|
|
|
throw new Exception('function call error'); |
293
|
|
|
} |
294
|
|
|
$this->__data[$field] = $function(I::get($data, $field)); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
protected function _safeValidator($data, $field, $rule) |
298
|
|
|
{ |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
protected function _unsetValidator($data, $field, $rule) |
302
|
|
|
{ |
303
|
|
|
unset($this->__data[$field]); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
public function getMessages() |
307
|
|
|
{ |
308
|
|
|
return $this->__messages; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
public function getMessage() |
312
|
|
|
{ |
313
|
|
|
foreach ($this->__messages as $field => $messages) { |
314
|
|
|
foreach ($messages as $k => $message) { |
315
|
|
|
$code = $this->__codes[$field][$k]; |
316
|
|
|
return [$code, $message]; |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
return [self::CODE_SUCCEEDED, 'success']; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
public function data() |
323
|
|
|
{ |
324
|
|
|
$this->__data = array_intersect_key($this->__data, array_flip($this->__safeField)); |
325
|
|
|
return $this->__data; |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|