-
Notifications
You must be signed in to change notification settings - Fork 1
/
Validator.php
192 lines (178 loc) · 6.71 KB
/
Validator.php
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
<?php
/**
* common validator
* 通用PHP数据验证器:
* 规则:
* type:数据类型,可选值为int,string,object,bool,numeric,array
* notNull:值是否不为空,可选值为true 或者false,当验证对象值为空且验证规则允许此值为空时,跳过其他规则验证
* len:值的长度
* lenMax:值的最大长度
* lenMin:值的最小长度
* valueMax:值的最大值
* valueMin:值的小小值
* match:正则表达式匹配
示例代码
include 'Validator.php';
$data = array(
'name' => 'zhangtao',
'age' => 18,
'sex' => true,
'address' => 'beijing',
'postcode' => '100034',
);
$rules = array(
'name' => array('type' => 'string', 'len' => 8, 'lenMax' => 8, 'lenMin' => 2, 'notNull' => true),
'sex' => array('type' => 'bool'),
'address' => array('type' => 'string', 'match' => "/jing$/i"),
'postcode' => array('type' => 'numeric', 'len' => 6),
);
$v=new Validator();
$v->init($rules, $data);
$r = $v->validate();
print_r($v->errors);
var_dump($r);
* @author tonera
*/
class Validator {
private $rules = array();
private $data = array();
private $errorsCode = array(
'40000' => 'The value\'s type is wrong',
'40001' => 'The value is null',
'40002' => 'The value\'s length is wrong',
'40003' => 'The value\'s max length is wrong',
'40004' => 'The value\'s min length is wrong',
'40005' => 'The value is over the max value',
'40006' => 'The value is less the min value',
'40007' => 'The value is not match the pattern',
'40008' => 'The value is not an array',
'40009' => 'The validatation method is not exists',
);
public $errors = array();
const E_TYPE = '40000';
const E_NOTNULL = '40001';
const E_LEN = '40002';
const E_LENMAX = '40003';
const E_LENMIN = '40004';
const E_VALUEMAX = '40005';
const E_VALUEMIN = '40006';
const E_MATCH = '40007';
const E_NOTARRAY = '40008';
const E_METHOD = '40009';
/**
* 初始化验证器规则和值
* @param array $rules 验证规则 eg.array('k'=>array('type'=>'int', 'valueMax'=>5, 'min'=>2))
* @param array $data 验证对象 eg. array('k'=>234,...)
*/
public function init($rules, $data) {
$this->rules = $rules;
$this->data = $data;
}
/**
* 验证方法
* @return boolean 成功 or 失败
*/
public function validate() {
$this->errors = array();
if (!is_array($this->data)) {
$this->errors[] = array(self::E_NOTARRAY, $this->errorsCode[self::E_NOTARRAY] . '[data is not an array]');
return false;
}
foreach ($this->data as $key => $value) {
if (isset($this->rules[$key])) {
//如果允许为空且当前值为空,则不验证
if (empty($value) and ! $this->rules[$key]['notNull']) {
continue;
}
foreach ($this->rules[$key] as $func => $limitValue) {
$method = "_" . $func;
if (!method_exists($this, $method)) {
$this->errors[] = array(self::E_METHOD, $this->errorsCode[self::E_METHOD] . '[validation method]');
} else {
$this->$method($key, $value);
}
}
}
}
if (count($this->errors) > 0) {
return false;
} else {
return true;
}
}
private function _type($k, $v) {
if (!isset($this->rules[$k])) {
return;
}
switch ($this->rules[$k]['type']) {
case 'int':
if (!is_integer($v)) {
$this->errors[] = array(self::E_TYPE, $k . ':' . $this->errorsCode[self::E_TYPE] . '[int]');
}
break;
case 'string':
if (!is_string($v)) {
$this->errors[] = array(self::E_TYPE, $k . ':' . $this->errorsCode[self::E_TYPE] . '[string]');
}
break;
case 'array':
if (!is_array($v)) {
$this->errors[] = array(self::E_TYPE, $k . ':' . $this->errorsCode[self::E_TYPE] . '[array]');
}
break;
case 'object':
if (!is_object($v)) {
$this->errors[] = array(self::E_TYPE, $k . ':' . $this->errorsCode[self::E_TYPE] . '[object]');
}
break;
case 'numeric':
if (!is_numeric($v)) {
$this->errors[] = array(self::E_TYPE, $k . ':' . $this->errorsCode[self::E_TYPE] . '[numeric]');
}
break;
case 'bool':
if (!is_bool($v)) {
$this->errors[] = array(self::E_TYPE, $k . ':' . $this->errorsCode[self::E_TYPE] . '[bool]');
}
break;
default:
$this->errors[] = array(self::E_TYPE, $k . ':' . $this->errorsCode[self::E_TYPE] . '[unknown]');
break;
}
}
private function _notNull($k, $v) {
if (empty($v)) {
$this->errors[] = array(self::E_NOTNULL, $k . ':' . $this->errorsCode[self::E_NOTNULL] . '[notNull]');
}
}
private function _len($k, $v) {
if ($this->rules[$k]['len'] != strlen($v)) {
$this->errors[] = array(self::E_LEN, $k . ':' . $this->errorsCode[self::E_LEN] . '[length]');
}
}
private function _lenMax($k, $v) {
if ($this->rules[$k]['lenMax'] < strlen($v)) {
$this->errors[] = array(self::E_LENMAX, $k . ':' . $this->errorsCode[self::E_LENMAX] . '[lenMax]');
}
}
private function _lenMin($k, $v) {
if ($this->rules[$k]['lenMin'] > strlen($v)) {
$this->errors[] = array(self::E_LENMIN, $k . ':' . $this->errorsCode[self::E_LENMIN] . '[lenMin]');
}
}
private function _valueMax($k, $v) {
if ($this->rules[$k]['valueMax'] < strlen($v)) {
$this->errors[] = array(self::E_VALUEMAX, $k . ':' . $this->errorsCode[self::E_VALUEMAX] . '[valueMax]');
}
}
private function _valueMin($k, $v) {
if ($this->rules[$k]['valueMin'] > strlen($v)) {
$this->errors[] = array(self::E_VALUEMIN, $k . ':' . $this->errorsCode[self::E_VALUEMIN] . '[valueMin]');
}
}
private function _match($k, $v) {
if (!preg_match($this->rules[$k]['match'], $v)) {
$this->errors[] = array(self::E_MATCH, $k . ':' . $this->errorsCode[self::E_MATCH] . '[match]');
}
}
}