Validator.php
3.8 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
<?php
class LtValidator
{
public $configHandle;
protected $errorMessages;
public function __construct()
{
if (! $this->configHandle instanceof LtConfig)
{
if (class_exists("LtObjectUtil", false))
{
$this->configHandle = LtObjectUtil::singleton("LtConfig");
}
else
{
$this->configHandle = new LtConfig;
}
}
}
public function init()
{
$this->errorMessages = $this->configHandle->get('validator.error_messages');
}
/**
* Validate an element
*
* @param mixed $value
* @param array $dtd
* @return array
*/
public function validate($value, $dtd)
{
$errorMessages = array();
$label = $dtd->label;
if (is_array($dtd->rules) && count($dtd->rules))
{
$messages = isset($dtd->messages) ? $dtd->messages : array();
foreach ($dtd->rules as $key => $val)
{
// callback_user_function
if ('callback_' == substr($key, 0, 9))
{
$method = substr($key, 9);
// 定义了过程函数
if (function_exists($method))
{
if (!$method($value, $dtd->rules[$key]))
{
if (isset($this->errorMessages[$key]))
{
$messages[$key] = $this->errorMessages[$key];
}
else
{
$messages[$key] = "validator.error_messages[$key] empty";
}
$errorMessages[$key] = sprintf($messages[$key], $label, $dtd->rules[$key]);
}
continue;
}
// 定义了类方法
$rc = new ReflectionClass($val);
if ($rc->hasMethod($method))
{
$rcMethod = $rc->getMethod($method);
if ($rcMethod->isStatic())
{
$ret = $rcMethod->invoke(null, $value, $dtd->rules[$key]);
}
else
{
// 非静态方法需要一个实例 有待考虑单例
$rcInstance = $rc->newInstance();
$ret = $rcMethod->invoke($rcInstance, $value, $dtd->rules[$key]);
}
if (!$ret)
{
if (isset($this->errorMessages[$key]))
{
$messages[$key] = $this->errorMessages[$key];
}
else
{
$messages[$key] = "validator.error_messages[$key] empty";
}
$errorMessages[$key] = sprintf($messages[$key], $label, $dtd->rules[$key]);
}
continue;
}
continue;
}
// end callback_user_function
$validateFunction = '_' . $key;
if ((is_bool($dtd->rules[$key]) || 0 < strlen($dtd->rules[$key])) && !$this->$validateFunction($value, $dtd->rules[$key]))
{
if (empty($messages[$key]))
{
if (isset($this->errorMessages[$key]))
{
$messages[$key] = $this->errorMessages[$key];
}
else
{
$messages[$key] = "validator.error_messages[$key] empty";
}
}
$errorMessages[$key] = sprintf($messages[$key], $label, $dtd->rules[$key]);
}
}
}
return $errorMessages;
}
protected function _ban($value, $ruleValue)
{
return !preg_match($ruleValue, $value);
}
protected function _mask($value, $ruleValue)
{
return preg_match($ruleValue, $value);
}
protected function _equal_to($value, $ruleValue)
{
return $value === $ruleValue;
}
protected function _max_length($value, $ruleValue)
{
return mb_strlen($value) <= $ruleValue;
}
protected function _min_length($value, $ruleValue)
{
return mb_strlen($value) >= $ruleValue;
}
protected function _max_value($value, $ruleValue)
{
return $value <= $ruleValue;
}
protected function _min_value($value, $ruleValue)
{
return $value >= $ruleValue;
}
protected function _min_selected($value, $ruleValue)
{
return count($value) >= $ruleValue;
}
protected function _max_selected($value, $ruleValue)
{
return count($value) <= $ruleValue;
}
protected function _required($value, $ruleValue)
{
if (false == $ruleValue)
{
return true;
}
else
{
return is_array($value) && count($value) || strlen($value);
}
}
}