/
Validator.php
345 lines (302 loc) · 7.69 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
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
<?php
/**
* naf\util\Validator class (Validator.php, Wed Jan 10 12:32:57 MSK 2007 12:32:57)
* Simple validation of input data, based on filter extension for PHP.
* Filter extension is enabled by default since php 5.2.0
*
* Validator supports fluent interface ( http://www.martinfowler.com/bliki/FluentInterface.html )
*
* example of usage:
* $v = new Validator();
* $v->addRequired('domain', 'Domain field is required')
* ->addRule('domain',
* array('filter' => FILTER_VALIDATE_URL, 'flags' => array(FILTER_FLAG_HOST_REQUIRED)),
* 'Domain must be a valid URL')
* ->addRule('domain',
* array('filter' => FILTER_CALLBACK, 'options' => array($this, 'filterUniqueDomain')),
* 'Domain name must be unique within the database')
* ->addRule('enabled', FILTER_VALIDATE_BOOLEAN, '"Enabled" field must be a boolean value');
*
* $vResult = $v->check($MY_INPUT_DATA);// $MY_INPUT_DATA is supposed to be an associative array (like $_POST or $_GET)
* var_dump($vResult->ok());
* var_dump($vResult->export());
* var_dump($vResult->getErrorList());
*
* @package naf\util
* @subpackage Validator
*/
namespace naf\util;
class Validator {
/**#@+
* @var array
*/
protected $_rules = array();
protected $_messages = array();
protected $_required = array();
protected $_equals = array();
protected $_validated = array();
/**#@-*/
/**
* @var Validator_Result
*/
protected $_result;
/**
* Constructor
*/
function __construct()
{
$this->_result = new Validator_Result();
}
/**
* Add required field
*
* @param string $key
* @param string $message
* @return Validator
*/
function addRequired($key, $message)
{
$this->_validated[$key] = null;
$this->_required[$key] = $message;
return $this;
}
/**
* Add equal fields. Useful for password confirmation.
*
* @param string $key1
* @param string $key2
* @param string $message
* @return Validator
*/
function addEquals($key1, $key2, $message)
{
$this->_equals[] = array($key1, $key2, $message);
$this->_validated[$key1] = null;
$this->_validated[$key2] = null;
return $this;
}
/**
* Add rule
*
* @param string $key
* @param array | int filter @see filter_* functions ( http://php.net/filter )
* @param string $message
* @return Validator
*/
function addRule($key, $filter, $message)
{
$this->_validated[$key] = null;
foreach ($this->_rules as $index => $stack)
if (! array_key_exists($key, $stack))
return $this->_doAddRule($key, $filter, $message, $index);
return $this->_doAddRule($key, $filter, $message, null);
}
/**
* @param string $key
* @param string $message
* @param int $flags
* @return Validator
*/
function addStringRule($key, $message, $flags = null)
{
if (null === $flags)
$flags = FILTER_FLAG_STRIP_LOW;
return $this->addRule($key, array('filter' => FILTER_SANITIZE_STRING, 'flags' => $flags), $message);
}
/**
* @param string $key
* @param string $message
* @param int $flags
* @return Validator
*/
function addRawStringRule($key, $message, $flags = null)
{
return $this->addRule($key, array('filter' => FILTER_UNSAFE_RAW, 'flags' => $flags), $message);
}
/**
* @param string $key
* @param string $message
* @param string $regexp
* @return Validator
*/
function addRegexpRule($key, $message, $regexp)
{
return $this->addRule($key,
array('filter' => FILTER_VALIDATE_REGEXP, 'options' => array('regexp' => $regexp)),
$message);
}
/**
* @param string $key
* @param string $message
* @return Validator
*/
function addEmailRule($key, $message)
{
return $this->addRule($key, FILTER_VALIDATE_EMAIL, $message);
}
/**
* @param string $key
* @param string $message
* @return Validator
*/
function addUrlRule($key, $message)
{
return $this->addRule($key, FILTER_VALIDATE_URL, $message);
}
/**
* @param string $key
* @param string $message
* @return Validator
*/
function addIntegerRule($key, $message, $options = null)
{
return $this->addRule($key, array('filter' => FILTER_VALIDATE_INT, 'options' => $options), $message);
}
/**
* @param string $key
* @param string $message
* @return Validator
*/
function addFloatRule($key, $message, $flags = null)
{
return $this->addRule($key, array('filter' => FILTER_VALIDATE_FLOAT, 'flags' => $flags), $message);
}
/**
* @param string $key
* @param string $message
* @return Validator
*/
function addBooleanRule($key, $message)
{
return $this->addRule($key, FILTER_VALIDATE_BOOLEAN, $message);
}
/**
* @param string $key
* @param callback $callback
* @param string $message
* @return Validator
*/
function addCallbackRule($key, $callback, $message)
{
return $this->addRule($key, array('filter' => FILTER_CALLBACK, 'options' => $callback), $message);
}
/**
* @param string $key
* @param callback $callback
* @param string $message
* @return Validator
*/
function addDateRule($key, $message)
{
return $this->addRule($key, array('filter' => FILTER_CALLBACK, 'options' => array($this, '_valiDate')), $message);
}
/**
* Validate string $value as a date specification
*
* @param string $value
* @return string or bool FALSE on failure
*/
protected function _valiDate($value) {
if (! ($timestamp = strtotime($value)))
return false;
$d = date('Y-m-d H:i:s', $timestamp);
$datetime = explode(' ', $d);
if ('00:00:00' == $datetime[1])
return $datetime[0];
else
return $d;
}
/**
* Perform validation check
*
* @param array $input
* @return Validator_Result
*/
function check($input)
{
array_walk_recursive($input, array($this, '_prepareInput'));
$this->_result->reset();
$this->_result->importRaw(array_intersect_key($input, $this->_validated));
if ($this->_failRequired($input) ||
$this->_failEquals($input) ||
$this->_failRules($input))
{
;
}
return $this->_result;
}
/**
* @return Validator_Result
*/
final function result()
{
return $this->_result;
}
protected function _failRequired($input)
{
foreach ($this->_required as $key => $message)
if (empty($input[$key]))
$this->_result->addError($key, $message);
return ! $this->_result->ok();
}
protected function _failEquals($input)
{
foreach ($this->_equals as $spec)
if (@$input[$spec[0]] != @$input[$spec[1]])
$this->_result->addError($spec[1], $spec[2]);
return ! $this->_result->ok();
}
protected function _failRules($input)
{
$output = array();
foreach ($input as $key => $value)
if (! empty($value))
$output[$key] = $value;
foreach ($this->_rules as $index => $stack)
{
$tmp_output = filter_var_array($output, array_intersect_key($stack, $output));
foreach ($tmp_output as $key => $value)
{
if (empty($this->_rules[$index][$key])) continue;
if (FILTER_VALIDATE_BOOLEAN == $this->_rules[$index][$key]['filter'])
{
$tmp_output[$key] = (bool) $value;
}
elseif (false === $value)
{
$this->_result->addError($key, $this->_messages[$index][$key]);
}
}
$output = array_merge($output, $tmp_output);
}
$output = array_merge($input, $output);
$ok = $this->_result->ok();
if ($ok) {
$this->_result->import($output);
}
return ! $ok;
}
protected function _prepareInput(& $value)
{
if (is_string($value)) $value = trim($value);
}
/**
* @return Validator
*/
protected function _doAddRule($key, $filter, $message, $index)
{
if (null === $index)
{
$index = count($this->_rules);
$this->_rules[$index] = array();
$this->_messages[$index] = array();
}
if (is_int($filter))
{// make filters uniform
$filter = array('filter' => $filter);
}
$this->_rules[$index][$key] = $filter;
$this->_messages[$index][$key] = $message;
return $this;
}
}