forked from klein/klein.php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidator.php
201 lines (177 loc) · 5.75 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
<?php
/**
* Klein (klein.php) - A fast & flexible router for PHP
*
* @author Chris O'Hara <[email protected]>
* @author Trevor Suarez (Rican7) (contributor and v2 refactorer)
* @copyright (c) Chris O'Hara
* @link https://github.com/klein/klein.php
* @license MIT
*/
namespace Klein;
use BadMethodCallException;
use Klein\Exceptions\ValidationException;
/**
* Validator
*/
class Validator
{
/**
* Class properties
*/
/**
* The available validator methods
*
* @type array
*/
public static $methods = array();
/**
* The string to validate
*
* @type string
*/
protected $str;
/**
* The custom exception message to throw on validation failure
*
* @type string
*/
protected $err;
/**
* Flag for whether the default validation methods have been added or not
*
* @type boolean
*/
protected static $default_added = false;
/**
* Methods
*/
/**
* Sets up the validator chain with the string and optional error message
*
* @param string $str The string to validate
* @param string $err The optional custom exception message to throw on validation failure
*/
public function __construct($str, $err = null)
{
$this->str = $str;
$this->err = $err;
if (!static::$default_added) {
static::addDefault();
}
}
/**
* Adds default validators on first use
*
* @return void
*/
public static function addDefault()
{
static::$methods['null'] = function ($str) {
return $str === null || $str === '';
};
static::$methods['len'] = function ($str, $min, $max = null) {
$len = strlen($str);
return null === $max ? $len === $min : $len >= $min && $len <= $max;
};
static::$methods['int'] = function ($str) {
return (string)$str === ((string)(int)$str);
};
static::$methods['float'] = function ($str) {
return (string)$str === ((string)(float)$str);
};
static::$methods['email'] = function ($str) {
return filter_var($str, FILTER_VALIDATE_EMAIL) !== false;
};
static::$methods['url'] = function ($str) {
return filter_var($str, FILTER_VALIDATE_URL) !== false;
};
static::$methods['ip'] = function ($str) {
return filter_var($str, FILTER_VALIDATE_IP) !== false;
};
static::$methods['remoteip'] = function ($str) {
return filter_var($str, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false;
};
static::$methods['alnum'] = function ($str) {
return ctype_alnum($str);
};
static::$methods['alpha'] = function ($str) {
return ctype_alpha($str);
};
static::$methods['contains'] = function ($str, $needle) {
return strpos($str, $needle) !== false;
};
static::$methods['regex'] = function ($str, $pattern) {
return preg_match($pattern, $str);
};
static::$methods['chars'] = function ($str, $chars) {
return preg_match("/^[$chars]++$/i", $str);
};
static::$default_added = true;
}
/**
* Add a custom validator to our list of validation methods
*
* @param string $method The name of the validator method
* @param callable $callback The callback to perform on validation
* @return void
*/
public static function addValidator($method, $callback)
{
static::$methods[strtolower($method)] = $callback;
}
/**
* Magic "__call" method
*
* Allows the ability to arbitrarily call a validator with an optional prefix
* of "is" or "not" by simply calling an instance property like a callback
*
* @param string $method The callable method to execute
* @param array $args The argument array to pass to our callback
* @throws BadMethodCallException If an attempt was made to call a validator modifier that doesn't exist
* @throws ValidationException If the validation check returns false
* @return Validator|boolean
*/
public function __call($method, $args)
{
$reverse = false;
$validator = $method;
$method_substr = substr($method, 0, 2);
if ($method_substr === 'is') { // is<$validator>()
$validator = substr($method, 2);
} elseif ($method_substr === 'no') { // not<$validator>()
$validator = substr($method, 3);
$reverse = true;
}
$validator = strtolower($validator);
if (!$validator || !isset(static::$methods[$validator])) {
throw new BadMethodCallException('Unknown method '. $method .'()');
}
$validator = static::$methods[$validator];
array_unshift($args, $this->str);
switch (count($args)) {
case 1:
$result = $validator($args[0]);
break;
case 2:
$result = $validator($args[0], $args[1]);
break;
case 3:
$result = $validator($args[0], $args[1], $args[2]);
break;
case 4:
$result = $validator($args[0], $args[1], $args[2], $args[3]);
break;
default:
$result = call_user_func_array($validator, $args);
break;
}
$result = (bool)($result ^ $reverse);
if (false === $this->err) {
return $result;
} elseif (false === $result) {
throw new ValidationException($this->err);
}
return $this;
}
}