-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidator.php
199 lines (188 loc) · 5.97 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
193
194
195
196
197
198
199
<?php
class Validator
{
//获取用户传进来的数据
public static function GetData($arrParamName)
{
$data = array();
if ($_SERVER['REQUEST_METHOD'] == 'POST')//POST方式提交,获取POSt的数据,否则取GET数据
{
foreach ($arrParamName as $filed=> $var_name) {
if(is_int($filed))
$data[$var_name] = $_POST[$var_name];
else
$data[$var_name]=$_POST[$filed];
}
} else {
foreach ($arrParamName as $filed=> $var_name) {
if(is_int($filed))
$data[$var_name] = $_GET[$var_name];
else
$data[$var_name]=$_GET[$filed];
}
}
return $data;
}
//成功返回空,失败返回错误信息
public static function ValidateInput($data, $rules, $errMsg)
{
$strErrMsg='';
foreach ($rules as $key => $rule) {
if (!is_array($rules)) {
throw new \Exception($key . '配置错误');
}
//取规则的具体配置
foreach ($rule as $ruleName => $ruleValue) {
if (is_int($ruleName))
{
$ruleKey = $ruleValue;
$ruleValue='';
}
else
{
$ruleKey = $ruleName;
}
$ruleKey = trim(strtolower($ruleKey));
$value='';
if(isset($data[$key]))
{
$value=$data[$key];
}
$msg=static::validate_field($key,$ruleKey, $ruleValue, $value);
if($msg!=='')
{
if(isset($errMsg[$key]))
{
$msg=$errMsg[$key][$ruleKey];
}
$strErrMsg.=$msg.'<br/>';
}
}
}
return $strErrMsg;
}
//成功返回空'',失败返回错误信息
protected static function validate_field($key,$ruleName, $rule, $value)
{
switch ($ruleName) {
case 'regex'://正则
if (!empty($value) && !static::validate_reg($rule, $value)) {
return $key . '填写错误';
}
break;
case 'ip':
if (!empty($value) && !static::ip($value)) {
return $key . '不是一个有效的IP';
}
break;
case 'required':
if (!static::required($value)) {
return '请填写' . $key;
}
break;
case 'numeric'://数字,整数或小数
if (!empty($value) && !static::numeric($value)) {
return $key . '只能是数字';
}
break;
case 'int'://整数
if (!empty($value) && !static::integer($value)) {
return $key . '只能是整数';
}
break;
case 'url':
if (!empty($value) && !static::url($value)) {
return $key . '不是一个有效的url';
}
break;
case 'maxlength':
if (!empty($value) && !static::maxlength($value, $rule)) {
return $key . '的长度不能超过' . $rule;
}
break;
case 'minlength':
if (!empty($value) && !static::minlength($value, $rule)) {
return $key . '的长度不能小于' . $rule;
}
break;
case 'max':
if (!empty($value) && !static::max($value, $rule)) {
return $key . '不能大于' . $rule;
}
break;
case 'min':
if (!empty($value) && !static::min($value, $rule)) {
return $key . '不能小于' . $rule;
}
break;
case 'email':
if (!empty($value) && !static::email($value)) {
return $key . '邮件格式不正确';
}
break;
}
return '';//不支持的配置,直接忽略
}
//判断是否有效的email
protected static function email($value) {
return filter_var($value, FILTER_VALIDATE_EMAIL);
}
//验证正则表达式
protected static function validate_reg($reg, $value)
{
if (!preg_match($reg, $value)) {
return false;
} else {
return true;
}
}
//验证ip地址
protected static function ip($value)
{
return filter_var($value, FILTER_VALIDATE_IP);
}
//必填项验证判断是否为空
protected static function required($value)
{
return (!is_null($value) && (trim($value) != ''));
}
//判断是否为数字,整数或小数
protected static function numeric($value)
{
return is_numeric($value);
}
//判断是否整数
protected static function integer($value)
{
return is_int($value) || ($value == (string)(int)$value);
}
//验证是否url
protected static function url($value)
{
return filter_var($value, FILTER_VALIDATE_URL);
}
//验证字符串的长度
protected static function maxlength($value, $length)
{
return (mb_strlen($value) <= $length);
}
//验证字符串的长度
protected static function minlength($value, $length)
{
return (mb_strlen($value) >= $length);
}
//验证最大值
protected static function max($value, $max)
{
$value = $value + 0;
$max = $max + 0;
return $value <= $max;
}
//验证最小值
protected static function min($value, $min)
{
$value = $value + 0;
$min = $min + 0;
return $value >= $min;
}
}