-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathValidator.php
More file actions
386 lines (325 loc) · 10.4 KB
/
Validator.php
File metadata and controls
386 lines (325 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
/**
* 简单的数据验证器
* Created by thenbsp (thenbsp@gmail.com)
*/
// Example:
//
// require_once APP_PATH.'Library/Validator/Validator.php';
// $data = array(
// 'user' => 'thenbsp',
// 'email' => 'thenbsp111@gmail.com',
// 'phone' => '18629261885',
// 'password' => 'asdfaaaaaaaaa',
// 'repassword' => 'asdfaaaaaaaaa'
// );
// Validator::addData($data);
// Validator::addRule('user', '姓名', 'required|minLength[2]|maxLength[16]');
// Validator::addRule('email', '邮箱', 'required|email|callback[UserModel.validatorHasEmail]');
// Validator::addRule('phone', '手机号', 'required|exactLength[11]|phone');
// Validator::addRule('password', '密码', 'required|minLength[6]|maxLength[16]');
// Validator::addRule('repassword', '确认密码', 'required|matche[password]');
// $isValid = Validator::isValid();
// var_dump($isValid);
// if( ! $isValid ) {
// print_r(Validator::getErrors());
// } else {
// print_r(Validator::getData());
// }
//
class Validator {
/**
* 待验证的数据
* @var array
*/
protected $data = array();
/**
* 数据验证规则
* @var array
*/
protected $rule = array();
/**
* 数据字段名称
* @var array
*/
protected $label = array();
/**
* 验证错误消息
* @var array
*/
protected $error = array();
/**
* 存储静态实例
* @var object
*/
protected static $instance;
/**
* 构告方法(可同时传递待验证数据)
* @param array $data 待验证数据
*/
protected function __construct() { }
/**
* =================================================================
* 验证规则必需以 “_rule_” 开头,并且区分大小写,返回值为 Boolean,
* 返回值如果为 FALSE,同必需返回FALSE以跳出循环,同时调用 addError()
* 方法添加错误信息,如果验证规则有值,将存放于第二个参数,例如:
* required => _rule_required($filed)
* minLength[6] => _rule_minLength($field, 6)
* maxLength[16] => _rule_maxLength($field, 16)
* =================================================================
*/
/**
* 验证规则 required
*/
protected function _rule_required($field) {
if( empty($this->_getData($field)) ) {
$this->_addError($field, "{$field}不能为空");
return FALSE;
}
}
/**
* minLength[x]
*/
protected function _rule_minLength($field, $value) {
if( strlen($this->_getData($field)) < intval($value) ) {
$this->_addError($field, "{$field}长度不能小于{$value}个字符");
return FALSE;
}
}
/**
* 验证规则 maxLength[x]
*/
protected function _rule_maxLength($field, $value) {
if( strlen($this->_getData($field)) > intval($value) ) {
$this->_addError($field, "{$field}长度不能大于{$value}");
return FALSE;
}
}
/**
* 验证规则 exactLength[x]
*/
protected function _rule_exactLength($field, $value) {
if( strlen($this->_getData($field)) !== intval($value) ) {
$this->_addError($field, "{$field}长度必需是{$value}");
return FALSE;
}
}
/**
* 验证规则 email
*/
protected function _rule_email($field) {
if( ! filter_var($this->_getData($field), FILTER_VALIDATE_EMAIL) ) {
$this->_addError($field, "{$field}格式不正确");
return FALSE;
}
}
/**
* 验证规则 phone
*/
protected function _rule_phone($field) {
if( ! preg_match("/^1[3458][0-9]{9}$/", $this->_getData($field)) ) {
$this->_addError($field, "{$field}格式不正确");
return FALSE;
}
}
/**
* 验证规则 matche[x]
*/
protected function _rule_matche($field, $value) {
if( $this->_getData($field) !== $this->_getData($value) ) {
$this->_addError($field, "{$field}和{$value}不匹配");
return FALSE;
}
}
/**
* 验证规则 callback(x)
*/
protected function _rule_callback($field, $value) {
list($class, $method) = array_pad(explode('.', $value), 2, NULL);
// 调用函数
if( is_null($method) ) {
if( ! function_exists($class) ) {
throw new Exception("Function {$class} Not Found");
}
if( ! call_user_func($class, $field) ) {
return FALSE;
}
}
// 调用类
else {
if( ! method_exists($class, $method) ) {
throw new Exception("Method {$class}::{$method} Not Found");
}
$object = new $class;
if( ! call_user_func_array(array($object, $method), array($field)) ) {
return FALSE;
}
}
}
/**
* 添加待验证数据
* @param array $data 待验证数据
*/
protected function _addData($data) {
foreach( $data AS $k=>$v ) {
$this->_setData($k, $v);
}
}
/**
* 设置待验证数据
* @param string $field 验证数据字段
* @param string $value 数据字段值
* @return void
*/
protected function _setData($field, $value) {
if( is_string($value) ) {
$value = trim($value);
}
$this->data[$field] = $value;
}
/**
* 获取待验证数据
* @return array
*/
protected function _getData($field = NULL) {
if( is_null($field) ) {
return $this->data;
}
return isset($this->data[$field]) ? $this->data[$field] : NULL;
}
/**
* 添加验证规则
* @param string $field 验证数据字段
* @param string $label 数据字段名称
* @param string $rules 数据验证规则
*/
protected function _addRule($field, $label, $rules) {
if( ! isset($this->data[$field]) ) {
$this->data[$field] = NULL;
}
$this->_setRule($field, explode('|', $rules));
$this->_addLabel($field, $label);
}
/**
* 设置验证规则
* @param string $field 验证数据字段
* @param string $rules 数据验证规则
*/
protected function _setRule($field, $rules) {
$this->rule[$field] = array_filter($rules);
}
/**
* 获取验证规则
* @return array
*/
protected function _getRule() {
return $this->rule;
}
/**
* 添加数据字段名称
* @param string $field 验证数据字段
* @param string $label 数据字段名称
*/
protected function _addLabel($field, $label) {
$this->label[$field] = $label;
}
/**
* 获取数据字段名称
* @return array
*/
protected function _getLabel($field = NULL) {
if( is_null($field) ) {
return $this->label;
}
return isset($this->label[$field]) ? $this->label[$field] : NULL;
}
/**
* 设置错误消息(可用于外部)
* @param string $field 数据字段
* @param string $errorMessage 错误消息
*/
protected function _addError($field, $errorMessage) {
$this->error[$field] = strtr($errorMessage, $this->_getLabel());
}
/**
* 获取错误消息(可用于外部)
* @return string
*/
protected function _getError() {
return empty($this->error) ? NULL : current($this->error);
}
/**
* 获取全部错误(可用于外部)
* @return array
*/
protected function _getErrors() {
return $this->error;
}
/**
* 获取规则中的值 rule[x]
* @param string $rule 验证规则
* @return string
*/
protected function _getRuleValue($rule) {
$value = NULL;
$valid = preg_match_all('/\[(.*?)\]/', $rule, $matches);
if( $valid AND isset($matches[1][0]) ) {
$value = $matches[1][0];
}
return $value;
}
/**
* 检测数据字段有效性
* @param string $field 验证数据字段
* @param string $rules 数据验证规则
*/
protected function _checkFieldByRule($field, $rules) {
foreach( $rules AS $k=>$v ) {
// 验证规则名称
$type = strpos($v, '[');
$rule = (FALSE !== $type) ? substr($v, 0, $type) : $v;
$name = "_rule_{$rule}";
if( is_callable(array($this, $name)) ) {
// 获取验证规则的值,也就是 [x] 中的 “x”
$value = $this->_getRuleValue($v);
// 如果字符中包含 “[” 但获取失败则直接跳过
if( is_null($value) AND (FALSE !== $type) ) {
continue;
}
// 否则就调用验证规则,验证失败必需跳出循环
if( FALSE === call_user_func_array(array($this, $name), array($field, $value)) ) {
break;
}
}
}
}
/**
* 检测验证结果
* @return boolean
*/
protected function _isValid() {
if( count($this->rule) > 0 ) {
foreach( $this->rule AS $k=>$v ) {
$this->_checkFieldByRule($k, $v);
}
}
return (count($this->error) === 0);
}
/**
* 魔术方法(对外只暴露这一个方法)
* @param string $name 方法名称
* @param array $arguments 参数
* @return void
*/
public static function __callStatic($method, $arguments) {
if( ! self::$instance instanceof self) {
self::$instance = new self();
}
$instance = self::$instance;
$function = "_{$method}";
if( ! is_callable(array($instance, $function)) ) {
throw new Exception( sprintf('Method %s::%s Not Found', __CLASS__, $method) );
}
return call_user_func_array(array($instance, $function), $arguments);
}
}