Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 650 lines (609 sloc) 29.543 kB
e7f3c31 @gwoo going lithium
gwoo authored
1 <?php
2 /**
3 * Lithium: the most rad php framework
4 *
4f1a9c0 @nateabele Updating copyright year.
nateabele authored
5 * @copyright Copyright 2011, Union of RAD (http://union-of-rad.org)
e7f3c31 @gwoo going lithium
gwoo authored
6 * @license http://opensource.org/licenses/bsd-license.php The BSD License
7 */
8
9 namespace lithium\util;
10
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
11 use lithium\util\Set;
12 use InvalidArgumentException;
e7f3c31 @gwoo going lithium
gwoo authored
13
e4bc392 Adding Validator docs.
psychic authored
14 /**
a0930f4 @phishy fixed spelling errors
phishy authored
15 * The `Validator` class provides static access to commonly used data validation logic. These common
bc7d5d7 @nateabele Fixing formatting and adding clarifying detail to documentation in `\…
nateabele authored
16 * routines cover HTML form input data such as phone and credit card numbers, dates and postal
17 * codes, but also include general checks for regular expressions and booleans and numericality.
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
18 *
bc7d5d7 @nateabele Fixing formatting and adding clarifying detail to documentation in `\…
nateabele authored
19 * General data checking is done by using `Validator` statically. Rules can be specified as a
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
20 * parameter to the `rule()` method or accessed directly via the `is[RuleName]()` method name
e4bc392 Adding Validator docs.
psychic authored
21 * convention:
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
22 *
e4bc392 Adding Validator docs.
psychic authored
23 * {{{
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
24 * use lithium\util\Validator;
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
25 *
1a4a9d0 @nateabele Moving class constructor from `\data\collection\Document` to `\data\C…
nateabele authored
26 * // The following are equivalent:
27 * Validator::rule('email', 'foo@example.com'); // true
28 * Validator::isEmail('foo-at-example.com'); // false
e4bc392 Adding Validator docs.
psychic authored
29 * }}}
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
30 *
fbda39a @nateabele Updating `Validator` docs and replacing code snippets with unit test …
nateabele authored
31 * Data can also be validated against multiple rules, each having their own associated error
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
32 * message. The rule structure is array-based and hierarchical based on rule names and
a0930f4 @phishy fixed spelling errors
phishy authored
33 * messages. Responses match the keys present in the `$data` parameter of `check()` up with an array
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
34 * of rules which they violate.
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
35 *
fbda39a @nateabele Updating `Validator` docs and replacing code snippets with unit test …
nateabele authored
36 * {{{ embed:lithium\tests\cases\util\ValidatorTest::testCheckMultipleHasFirstError(1-15) }}}
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
37 *
fbda39a @nateabele Updating `Validator` docs and replacing code snippets with unit test …
nateabele authored
38 * See the `check()` method for more information an multi-value datasets. Custom validation rules
39 * can also be added to `Validator` at runtime. These can either take the form of regular expression
40 * strings or functions supplied to the `add()` method.
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
41 *
42 * ### Rules
43 *
44 * The `Validator` class includes a series of commonly-used rules by default, any of which may be
45 * used in calls to `rule()` or `check()`, or called directly as a method. Additionally, many rules
46 * have a variety of different _formats_ in which they may be specified. The following is the list
47 * of the built-in rules, but keep in mind that none of them are hard-coded. Any rule may be
48 * overridden by adding a new rule of the same name using the `add()` method.
49 *
50 * - `notEmpty`: Checks that a string contains at least one non-whitespace character.
51 *
52 * - `alphaNumeric`: Checks that a string contains only integer or letters.
53 *
54 * - `lengthBetween`: Checks that a string length is within a specified range. Spaces are included
55 * in the character count. The available options are `'min'` and `'max'`, which designate the
56 * minimum and maximum length of the string.
57 *
58 * - `blank`: Checks that a field is left blank **OR** only whitespace characters are present in its
59 * value. Whitespace characters include spaces, tabs, carriage returns and newlines.
60 *
61 * - `creditCard`: Checks that a value is a valid credit card number. This rule is divided into a
62 * series of formats: `'amex'`, `'bankcard'`, `'diners'`, `'disc'`, `'electron'`, `'enroute'`,
63 * `'jcb'`, `'maestro'`, `'mc'`, `'solo'`, `'switch'`, `'visa'`, `'voyager'`, `'fast'`. If no
64 * format value is specified, the value defaults to `'any'`, which will validate the value if
65 * _any_ of the available formats match. You can also use the `'fast'` format, which does a
66 * high-speed, low-fidelity check to ensure that the value looks like a real credit card number.
67 * This rule includes one option, `'deep'`, which (if set to `true`) validates the value using the
68 * [Luhn algorithm](http://en.wikipedia.org/wiki/Luhn_algorithm) if the format validation is
69 * successful. See the `luhn` validator below for more details.
70 *
71 * - `date`: Checks that a value is a valid date that complies with one or more formats. Also
72 * validates leap years. Possible formats are `'dmy'` (27-12-2010 or 27-12-10 separators can be a
73 * space, period, dash, forward slash), `'mdy'` (12-27-2010 or 12-27-10 separators can be a space,
74 * period, dash, forward slash), `'ymd'` (2010-12-27 or 10-12-27 separators can be a space,
75 * period, dash, forward slash), `'dMy'` (27 December 2010 or 27 Dec 2010), `'Mdy'` (December 27,
76 * 2010 or Dec 27, 2010 comma is optional), `'My'` (December 2010 or Dec 2010) or `'my'` (12/2010
77 * separators can be a space, period, dash, forward slash).
78 *
79 * - `time`: Checks that a value is a valid time. Validates time as 24hr (HH:MM) or am/pm
80 * ([ H]H:MM[a|p]m). Does not allow / validate seconds.
81 *
947b374 @davidpersson Adding documentation and tests for boolean string types.
davidpersson authored
82 * - `boolean`: Checks that the value is or looks like a boolean value. The following types of
83 * values are interpreted as boolean and will pass the check.
84 * - boolean (`true`, `false`, `'true'`, `'false'`)
d1a3c57 @davidpersson Updating docblock, fixing quotation marks.
davidpersson authored
85 * - boolean number (`1`, `0`, `'1'`, `'0'`)
947b374 @davidpersson Adding documentation and tests for boolean string types.
davidpersson authored
86 * - boolean text string (`'on'`, `'off'`, `'yes'`, `'no'`)
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
87 *
88 * - `decimal`: Checks that a value is a valid decimal. Takes one option, `'precision'`, which is
89 * an optional integer value defining the level of precision the decimal number must match.
90 *
91 * - `email`: Checks that a value is (probably) a valid email address. The subject of validating
92 * an actual email address is complicated and problematic. A regular expression that correctly
93 * validates addresses against [RFC 5322](http://tools.ietf.org/html/rfc5322) would be several
94 * pages long, with the drawback of being unable to keep up as new top-level domains are added.
95 * Instead, this validator uses PHP's internal input filtering API to check the format, and
96 * provides an option, `'deep'` ( _boolean_) which, if set to `true`, will validate that the email
97 * address' domain contains a valid MX record. Keep in mind, this is just one of the many ways to
98 * validate an email address in the overall context of an application. For other ideas or
99 * examples, [ask Sean](http://seancoates.com/).
100 *
101 * - `ip`: Validates a string as a valid IPv4 or IPv6 address.
102 *
103 * - `money`: Checks that a value is a valid monetary amount. This rule has two formats, `'right'`
104 * and `'left'`, which indicates which side the monetary symbol (i.e. $) appears on.
105 *
106 * - `numeric`: Checks that a value is numeric.
107 *
108 * - `phone`: Check that a value is a valid phone number, non-locale-specific phone number.
109 *
110 * - `postalCode`: Checks that a given value is a valid US postal code.
111 *
112 * - `inRange`: Checks that a numeric value is within a specified range. This value has two options,
113 * `'upper'` and `'lower'`, which specify the boundary of the value.
114 *
115 * - `url`: Checks that a value is a valid URL according to
116 * [RFC 2395](http://www.faqs.org/rfcs/rfc2396.html). Uses PHP's filter API, and accepts any
117 * options accepted for
118 * [the validation URL filter](http://www.php.net/manual/en/filter.filters.validate.php).
119 *
120 * - `luhn`: Checks that a value is a valid credit card number according to the
121 * [Luhn algorithm](http://en.wikipedia.org/wiki/Luhn_algorithm). (See also: the `creditCard`
122 * validator).
123 *
124 * - `inList`: Checks that a value is in a pre-defined list of values. This validator accepts one
125 * option, `'list'`, which is an array containing acceptable values.
126 *
c8d7278 @nateabele Implementing alternative regular expression delimiters for validation…
nateabele authored
127 * - `regex`: Checks that a value appears to be a valid regular expression, possibly
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
128 * containing PCRE-compatible options flags.
129 *
130 * - `uuid`: Checks that a value is a valid UUID.
e4bc392 Adding Validator docs.
psychic authored
131 */
e7f3c31 @gwoo going lithium
gwoo authored
132 class Validator extends \lithium\core\StaticObject {
133
134 /**
135 * An array of validation rules. May contain a single regular expression, an array of regular
136 * expressions (where the array keys define various possible 'formats' of the same rule), or a
137 * closure which accepts a value to be validated, and an array of options, and returns a
138 * boolean value, indicating whether the validation succeeded or failed.
139 *
140 * @var array
141 * @see lithium\util\Validator::add()
142 * @see lithium\util\Validator::rule()
143 */
144 protected static $_rules = array();
145
0c12c78 @jperras Adding missing doc block for util\Validator::.
jperras authored
146 /**
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
147 * Default options used when defining a new validator rule. Each key contains method-specific
148 * options that should always be applied, or options that should be applied to all rules in the
149 * `'defaults'` key.
0c12c78 @jperras Adding missing doc block for util\Validator::.
jperras authored
150 *
151 * @see lithium\util\Validator::add()
152 * @see lithium\util\Validator::rule()
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
153 * @var array
0c12c78 @jperras Adding missing doc block for util\Validator::.
jperras authored
154 */
e7f3c31 @gwoo going lithium
gwoo authored
155 protected static $_options = array(
156 'defaults' => array('contains' => true)
157 );
158
159 /**
160 * Initializes the list of default validation rules.
161 *
162 * @return void
163 */
164 public static function __init() {
165 $alnum = '[A-Fa-f0-9]';
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
166 $class = get_called_class();
167 static::$_methodFilters[$class] = array();
e7f3c31 @gwoo going lithium
gwoo authored
168
169 static::$_rules = array(
170 'alphaNumeric' => '/^[\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]+$/mu',
171 'blank' => '/[^\\s]/',
172 'creditCard' => array(
173 'amex' => '/^3[4|7]\\d{13}$/',
174 'bankcard' => '/^56(10\\d\\d|022[1-5])\\d{10}$/',
175 'diners' => '/^(?:3(0[0-5]|[68]\\d)\\d{11})|(?:5[1-5]\\d{14})$/',
176 'disc' => '/^(?:6011|650\\d)\\d{12}$/',
177 'electron' => '/^(?:417500|4917\\d{2}|4913\\d{2})\\d{10}$/',
178 'enroute' => '/^2(?:014|149)\\d{11}$/',
179 'jcb' => '/^(3\\d{4}|2100|1800)\\d{11}$/',
180 'maestro' => '/^(?:5020|6\\d{3})\\d{12}$/',
181 'mc' => '/^5[1-5]\\d{14}$/',
182 'solo' => '/^(6334[5-9][0-9]|6767[0-9]{2})\\d{10}(\\d{2,3})?$/',
183 'switch' => '/^(?:49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2})' .
184 '\\d{10}(\\d{2,3})?)|(?:564182\\d{10}(\\d{2,3})?)|(6(3(33[0-4]' .
185 '[0-9])|759[0-9]{2})\\d{10}(\\d{2,3})?)$/',
186 'visa' => '/^4\\d{12}(\\d{3})?$/',
187 'voyager' => '/^8699[0-9]{11}$/',
188 'fast' => '/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3' .
860cffb @Howard3 Closes #370 & #371
Howard3 authored
189 '(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/'
e7f3c31 @gwoo going lithium
gwoo authored
190 ),
191 'date' => array(
192 'dmy' => '%^(?:(?:31(\\/|-|\\.|\\x20)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)' .
193 '(\\/|-|\\.|\\x20)(?:0?[1,3-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?' .
194 '\\d{2})$|^(?:29(\\/|-|\\.|\\x20)0?2\\3(?:(?:(?:1[6-9]|[2-9]\\d)?' .
195 '(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])' .
196 '00))))$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.|\\x20)(?:(?:0?[1-9])|' .
197 '(?:1[0-2]))\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%',
198 'mdy' => '%^(?:(?:(?:0?[13578]|1[02])(\\/|-|\\.|\\x20)31)\\1|(?:(?:0?[13-9]|' .
199 '1[0-2])(\\/|-|\\.|\\x20)(?:29|30)\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d' .
200 '{2})$|^(?:0?2(\\/|-|\\.|\\x20)29\\3(?:(?:(?:1[6-9]|[2-9]\\d)?' .
201 '(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])' .
202 '00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\\/|-|\\.|\\x20)(?:0?[1-9]|1' .
203 '\\d|2[0-8])\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%',
204 'ymd' => '%^(?:(?:(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579]' .
205 '[26])|(?:(?:16|[2468][048]|[3579][26])00)))(\\/|-|\\.|\\x20)' .
206 '(?:0?2\\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\\d)?\\d{2})(\\/|-|\\.|' .
207 '\\x20)(?:(?:(?:0?[13578]|1[02])\\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])' .
208 '\\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\\2(?:0?[1-9]|1\\d|2[0-8]' .
209 '))))$%',
210 'dMy' => '/^((31(?!\\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\\b|t)t?|Nov)' .
211 '(ember)?)))|((30|29)(?!\\ Feb(ruary)?))|(29(?=\\ Feb(ruary)?\\ ' .
212 '(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468]' .
213 '[048]|[3579][26])00)))))|(0?[1-9])|1\\d|2[0-8])\\ (Jan(uary)?|' .
214 'Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|' .
215 'Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)\\ ((1[6-9]|[2-9]' .
216 '\\d)\\d{2})$/',
217 'Mdy' => '/^(?:(((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?' .
218 '|Dec(ember)?)\\ 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)' .
219 '|(ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?)\\ (0?[1-9]' .
220 '|([12]\\d)|30))|(Feb(ruary)?\\ (0?[1-9]|1\\d|2[0-8]|(29(?=,?\\ ' .
221 '((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468]' .
222 '[048]|[3579][26])00)))))))\\,?\\ ((1[6-9]|[2-9]\\d)\\d{2}))$/',
223 'My' => '%^(Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|' .
224 'Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)[ /]((1[6-9]' .
225 '|[2-9]\\d)\\d{2})$%',
226 'my' => '%^(((0[123456789]|10|11|12)([- /.])(([1][9][0-9][0-9])|([2][0-9]' .
227 '[0-9][0-9]))))$%'
228 ),
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
229 'ip' => function($value, $format = null, array $options = array()) {
230 $options += array('flags' => array());
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
231 return (boolean) filter_var($value, FILTER_VALIDATE_IP, $options);
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
232 },
e7f3c31 @gwoo going lithium
gwoo authored
233 'money' => array(
234 'right' => '/^(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?(?:\1\d{3})*|(?:\d+))' .
235 '((?!\1)[,.]\d{2})?(?<!\x{00a2})\p{Sc}?$/u',
236 'left' => '/^(?!\x{00a2})\p{Sc}?(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?' .
860cffb @Howard3 Closes #370 & #371
Howard3 authored
237 '(?:\1\d{3})*|(?:\d+))((?!\1)[,.]\d{2})?$/u'
e7f3c31 @gwoo going lithium
gwoo authored
238 ),
239 'notEmpty' => '/[^\s]+/m',
338196b @davidpersson Moving locale dependent validation rules to g11n resources.
davidpersson authored
240 'phone' => '/^\+?[0-9\(\)\-]{10,20}$/',
241 'postalCode' => '/(^|\A\b)[A-Z0-9\s\-]{5,}($|\b\z)/i',
c8d7278 @nateabele Implementing alternative regular expression delimiters for validation…
nateabele authored
242 'regex' => '/^(?:([^[:alpha:]\\\\{<\[\(])(.+)(?:\1))|(?:{(.+)})|(?:<(.+)>)|' .
243 '(?:\[(.+)\])|(?:\((.+)\))[gimsxu]*$/',
e7f3c31 @gwoo going lithium
gwoo authored
244 'time' => '%^((0?[1-9]|1[012])(:[0-5]\d){0,2}([AP]M|[ap]m))$|^([01]\d|2[0-3])' .
245 '(:[0-5]\d){0,2}$%',
c8d7278 @nateabele Implementing alternative regular expression delimiters for validation…
nateabele authored
246 'boolean' => function($value) {
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
247 $bool = is_bool($value);
0c12c78 @jperras Adding missing doc block for util\Validator::.
jperras authored
248 $filter = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
249 return ($bool || $filter !== null);
e7f3c31 @gwoo going lithium
gwoo authored
250 },
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored
251 'decimal' => function($value, $format = null, array $options = array()) {
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
252 if (isset($options['precision'])) {
253 $precision = strlen($value) - strrpos($value, '.') - 1;
e7f3c31 @gwoo going lithium
gwoo authored
254
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
255 if ($precision !== (int) $options['precision']) {
256 return false;
257 }
258 }
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
259 return (filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE) !== null);
e7f3c31 @gwoo going lithium
gwoo authored
260 },
261 'inList' => function($value, $format, $options) {
262 $options += array('list' => array());
263 return in_array($value, $options['list']);
264 },
265 'lengthBetween' => function($value, $format, $options) {
266 $length = strlen($value);
267 $options += array('min' => 1, 'max' => 255);
268 return ($length >= $options['min'] && $length <= $options['max']);
269 },
270 'luhn' => function($value) {
271 if (empty($value) || !is_string($value)) {
272 return false;
273 }
274 $sum = 0;
275 $length = strlen($value);
276
277 for ($position = 1 - ($length % 2); $position < $length; $position += 2) {
278 $sum += $value[$position];
279 }
280 for ($position = ($length % 2); $position < $length; $position += 2) {
281 $number = $value[$position] * 2;
282 $sum += ($number < 10) ? $number : $number - 9;
283 }
284 return ($sum % 10 == 0);
285 },
286 'numeric' => function($value) {
287 return is_numeric($value);
288 },
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
289 'inRange' => function($value, $format, $options) {
290 $defaults = array('upper' => null, 'lower' => null);
291 $options += $defaults;
292
293 if (!is_numeric($value)) {
294 return false;
295 }
296 switch (true) {
297 case (!is_null($options['upper']) && !is_null($options['lower'])):
298 return ($value > $options['lower'] && $value < $options['upper']);
299 case (!is_null($options['upper'])):
300 return ($value < $options['upper']);
301 case (!is_null($options['lower'])):
302 return ($value > $options['lower']);
303 }
304 return is_finite($value);
305 },
8c8279c @ddebernardy fix insufficiently robust uuid validator
ddebernardy authored
306 'uuid' => "/^{$alnum}{8}-{$alnum}{4}-{$alnum}{4}-{$alnum}{4}-{$alnum}{12}$/",
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
307 'email' => function($value) {
308 return filter_var($value, FILTER_VALIDATE_EMAIL);
309 },
310 'url' => function($value, $format = null, array $options = array()) {
311 $options += array('flags' => array());
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
312 return (boolean) filter_var($value, FILTER_VALIDATE_URL, $options);
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
313 }
e7f3c31 @gwoo going lithium
gwoo authored
314 );
315
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
316 $isEmpty = function($self, $params, $chain) {
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
317 extract($params);
318 return (empty($value) && $value != '0') ? false : $chain->next($self, $params, $chain);
e7f3c31 @gwoo going lithium
gwoo authored
319 };
320
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
321 static::$_methodFilters[$class]['alphaNumeric'] = array($isEmpty);
322 static::$_methodFilters[$class]['notEmpty'] = array($isEmpty);
e7f3c31 @gwoo going lithium
gwoo authored
323
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
324 static::$_methodFilters[$class]['creditCard'] = array(function($self, $params, $chain) {
325 extract($params);
e7f3c31 @gwoo going lithium
gwoo authored
326 $options += array('deep' => false);
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
327
328 if (strlen($value = str_replace(array('-', ' '), '', $value)) < 13) {
329 return false;
330 }
331 if (!$chain->next($self, compact('value') + $params, $chain)) {
332 return false;
333 }
e7f3c31 @gwoo going lithium
gwoo authored
334 return $options['deep'] ? Validator::isLuhn($value) : true;
335 });
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
336
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
337 static::$_methodFilters[$class]['email'] = array(function($self, $params, $chain) {
338 extract($params);
339 $defaults = array('deep' => false);
340 $options += $defaults;
e7f3c31 @gwoo going lithium
gwoo authored
341
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
342 if (!$chain->next($self, $params, $chain)) {
d87fbbe @gwoo changing deep email validator to use getmxrr
gwoo authored
343 return false;
e7f3c31 @gwoo going lithium
gwoo authored
344 }
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
345 if (!$options['deep']) {
346 return true;
347 }
348 list($prefix, $host) = explode('@', $params['value']);
349
350 if (getmxrr($host, $mxhosts)) {
351 return is_array($mxhosts);
352 }
353 return false;
354 });
e7f3c31 @gwoo going lithium
gwoo authored
355 }
356
357 /**
358 * Maps method calls to validation rule names. For example, a validation rule that would
359 * normally be called as `Validator::rule('email', 'foo@bar.com')` can also be called as
360 * `Validator::isEmail('foo@bar.com')`.
361 *
362 * @param string $method The name of the method called, i.e. `'isEmail'` or `'isCreditCard'`.
64f6039 @jperras Replacing various instances of array_key_exists with isset().
jperras authored
363 * @param array $args
e7f3c31 @gwoo going lithium
gwoo authored
364 * @return boolean
365 */
366 public static function __callStatic($method, $args = array()) {
367 if (!isset($args[0])) {
368 return false;
369 }
88a2ecc @Howard3 Validator: Fixed a condition where 'format' wasn't being properly app…
Howard3 authored
370 $args = array_filter($args) + array(0 => $args[0], 1 => 'any', 2 => array());
e7f3c31 @gwoo going lithium
gwoo authored
371 $rule = preg_replace("/^is([A-Z][A-Za-z0-9]+)$/", '$1', $method);
372 $rule[0] = strtolower($rule[0]);
373 return static::rule($rule, $args[0], $args[1], $args[2]);
374 }
375
376 /**
fbda39a @nateabele Updating `Validator` docs and replacing code snippets with unit test …
nateabele authored
377 * Checks a set of values against a specified rules list. This method may be used to validate
378 * any arbitrary array of data against a set of validation rules.
e7f3c31 @gwoo going lithium
gwoo authored
379 *
fbda39a @nateabele Updating `Validator` docs and replacing code snippets with unit test …
nateabele authored
380 * @param array $values An array of key/value pairs, where the values are to be checked.
381 * @param array $rules An array of rules to check the values in `$values` against. Each key in
382 * `$rules` should match a key contained in `$values`, and each value should be a
5089955 @nateabele Updating documentation for `\util\Validator`, adding tests to prove f…
nateabele authored
383 * validation rule in one of the allowable formats. For example, if you are
1ec455e @nateabele Updating example used in docblock of `\util\Validator::check()`. Fixe…
nateabele authored
384 * validating a data set containing a `'credit_card'` key, possible values for
5089955 @nateabele Updating documentation for `\util\Validator`, adding tests to prove f…
nateabele authored
385 * `$rules` would be as follows:
1ec455e @nateabele Updating example used in docblock of `\util\Validator::check()`. Fixe…
nateabele authored
386 * - `array('credit_card' => 'You must include a credit card number')`: This is the
387 * simplest form of validation rule, in which the value is simply a message to
388 * display if the rule fails. Using this format, all other validation settings
389 * inherit from the defaults, including the validation rule itself, which only
390 * checks to see that the corresponding key in `$values` is present and contains
391 * a value that is not empty. _Please note when globalizing validation messages:_
392 * When specifying messages, it may be preferable to use a code string (i.e.
393 * `'ERR_NO_TITLE'`) instead of the full text of the validation error. These code
394 * strings may then be translated by the appropriate tools in the templating
395 * layer.
396 * - `array('credit_card' => array('creditCard', 'message' => 'Invalid CC #'))`:
397 * In the second format, the validation rule (in this case `creditCard`) and
398 * associated configuration are specified as an array, where the rule to use is
399 * the first value in the array (no key), and additional settings are specified
400 * as other keys in the array. Please see the list below for more information on
401 * allowed keys.
5089955 @nateabele Updating documentation for `\util\Validator`, adding tests to prove f…
nateabele authored
402 * - The final format allows you to apply multiple validation rules to a single
403 * value, and it is specified as follows:
404 *
1ec455e @nateabele Updating example used in docblock of `\util\Validator::check()`. Fixe…
nateabele authored
405 * `array('credit_card' => array(
406 * array('notEmpty', 'message' => 'You must include credit card number'),
407 * array('creditCard', 'message' => 'Your credit card number must be valid')
5089955 @nateabele Updating documentation for `\util\Validator`, adding tests to prove f…
nateabele authored
408 * ));`
470d76e @jperras Fixing coding standards violations in `util\Validator`.
jperras authored
409 * @param array $options Validator-specific options.
5089955 @nateabele Updating documentation for `\util\Validator`, adding tests to prove f…
nateabele authored
410 *
411 * Each rule defined as an array can contain any of the following settings (in addition to the
412 * first value, which represents the rule to be used):
413 * - `'message'` _string_: The error message to be returned if the validation rule fails. See
414 * the note above regarding globalization of error messages.
415 * - `'required`' _boolean_: Represents whether the value is required to be present in
416 * `$values`. If `'required'` is set to `false`, the validation rule will be skipped if the
417 * corresponding key is not present. Defaults to `true`.
418 * - `'skipEmpty'` _boolean_: Similar to `'required'`, this setting (if `true`) will cause the
419 * validation rule to be skipped if the corresponding value is empty (an empty string or
420 * `null`). Defaults to `false`.
421 * - `'format'` _string_: If the validation rule has multiple format definitions (see the
422 * `add()` or `__init()` methods), the name of the format to be used can be specified here.
423 * Additionally, two special values can be used: either `'any'`, which means that all formats
424 * will be checked and the rule will pass if any format passes, or `'all'`, which requires
425 * all formats to pass in order for the rule check to succeed.
fbda39a @nateabele Updating `Validator` docs and replacing code snippets with unit test …
nateabele authored
426 * @return array Returns an array containing all validation failures for data in `$values`,
427 * where each key matches a key in `$values`, and each value is an array of that
428 * element's validation errors.
011811c @Ciaro Making Validator::check filterable.
Ciaro authored
429 * @filter
e7f3c31 @gwoo going lithium
gwoo authored
430 */
fbda39a @nateabele Updating `Validator` docs and replacing code snippets with unit test …
nateabele authored
431 public static function check(array $values, array $rules, array $options = array()) {
904be25 @nateabele Refactoring `Validator::check()`, implementing updated rules syntax. …
nateabele authored
432 $defaults = array(
433 'notEmpty',
434 'message' => null,
435 'required' => true,
436 'skipEmpty' => false,
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
437 'format' => 'any',
afe30c7 @vesln Adding support for "last" rule in `Validator::check` method
vesln authored
438 'on' => null,
439 'last' => false
904be25 @nateabele Refactoring `Validator::check()`, implementing updated rules syntax. …
nateabele authored
440 );
441
011811c @Ciaro Making Validator::check filterable.
Ciaro authored
442 $options += $defaults;
443 $params = compact('values', 'rules', 'options');
e86fd73 @nateabele Implementing validator events, to enable conditional validation. To u…
nateabele authored
444
011811c @Ciaro Making Validator::check filterable.
Ciaro authored
445 return static::_filter(__FUNCTION__, $params, function($self, $params) {
446 $values = $params['values'];
447 $rules = $params['rules'];
448 $options = $params['options'];
449
450 $errors = array();
451 $events = (array) (isset($options['events']) ? $options['events'] : null);
e1d1907 @Howard3 enabling validation on arrays
Howard3 authored
452 $values = array_merge($values, Set::flatten($values));
904be25 @nateabele Refactoring `Validator::check()`, implementing updated rules syntax. …
nateabele authored
453
011811c @Ciaro Making Validator::check filterable.
Ciaro authored
454 foreach ($rules as $field => $rules) {
455 $rules = is_string($rules) ? array('message' => $rules) : $rules;
456 $rules = is_array(current($rules)) ? $rules : array($rules);
457 $errors[$field] = array();
458 $options['field'] = $field;
459
460 foreach ($rules as $key => $rule) {
461 $rule += $options + compact('values');
462 list($name) = $rule;
463
464 if ($events && $rule['on'] && !array_intersect($events, (array) $rule['on'])) {
465 continue;
4fceb8b @gwoo refactoring Model::validates. adding Record::errors
gwoo authored
466 }
0ee6b81 @Howard3 'field' => null was triggering the 'required' cycle instead of the 's…
Howard3 authored
467 if (!array_key_exists($field, $values)) {
011811c @Ciaro Making Validator::check filterable.
Ciaro authored
468 if ($rule['required']) {
469 $errors[$field][] = $rule['message'] ?: $key;
470 }
471 if ($rule['last']) {
472 break;
473 }
474 continue;
475 }
476 if (empty($values[$field]) && $rule['skipEmpty']) {
477 continue;
8d52861 @vesln Fixing the case when last is used on empty value with required option
vesln authored
478 }
30ab4c1 @daschl QA: Miscc QA fixes, including trailing commas, param documentation an…
daschl authored
479
011811c @Ciaro Making Validator::check filterable.
Ciaro authored
480 if (!$self::rule($name, $values[$field], $rule['format'], $rule + $options)) {
481 $errors[$field][] = $rule['message'] ?: $key;
482
483 if ($rule['last']) {
484 break;
485 }
8d52861 @vesln Fixing the case when last is used on empty value with required option
vesln authored
486 }
4fceb8b @gwoo refactoring Model::validates. adding Record::errors
gwoo authored
487 }
488 }
011811c @Ciaro Making Validator::check filterable.
Ciaro authored
489 return array_filter($errors);
490 });
e7f3c31 @gwoo going lithium
gwoo authored
491 }
492
493 /**
494 * Adds to or replaces built-in validation rules specified in `Validator::$_rules`. Any new
495 * validation rules created are automatically callable as validation methods.
64f6039 @jperras Replacing various instances of array_key_exists with isset().
jperras authored
496 *
e7f3c31 @gwoo going lithium
gwoo authored
497 * For example:
498 * {{{
499 * Validator::add('zeroToNine', '/^[0-9]$/');
500 * $isValid = Validator::isZeroToNine("5"); // true
501 * $isValid = Validator::isZeroToNine("20"); // false
502 * }}}
64f6039 @jperras Replacing various instances of array_key_exists with isset().
jperras authored
503 *
e7f3c31 @gwoo going lithium
gwoo authored
504 * Alternatively, the first parameter may be an array of rules expressed as key/value pairs,
505 * as in the following:
506 * {{{
507 * Validator::add(array(
508 * 'zeroToNine' => '/^[0-9]$/',
509 * 'tenToNineteen' => '/^1[0-9]$/',
510 * ));
511 * }}}
64f6039 @jperras Replacing various instances of array_key_exists with isset().
jperras authored
512 *
bc7d5d7 @nateabele Fixing formatting and adding clarifying detail to documentation in `\…
nateabele authored
513 * In addition to regular expressions, validation rules can also be defined as full anonymous
514 * functions:
515 * {{{
516 * use app\models\Account;
517 *
518 * Validator::add('accountActive', function($value) {
519 * $value = is_int($value) ? Account::find($value) : $value;
520 * return (boolean) $value->is_active;
521 * });
522 *
523 * $testAccount = Account::create(array('is_active' => false));
524 * Validator::isAccountActive($testAccount); // returns false
525 * }}}
526 *
527 * These functions can take up to 3 parameters:
528 * - `$value` _mixed_: This is the actual value to be validated (as in the above example).
529 * - `$format` _string_: Often, validation rules come in multiple "formats", for example:
530 * postal codes, which vary by country or region. Defining multiple formats allows you to
470d76e @jperras Fixing coding standards violations in `util\Validator`.
jperras authored
531 * retian flexibility in how you validate data. In cases where a user's country of origin
532 * is known, the appropriate validation rule may be selected. In cases where it is not
533 * known, the value of `$format` may be `'any'`, which should pass if any format matches.
534 * In cases where validation rule formats are not mutually exclusive, the value may be
535 * `'all'`, in which case all must match.
536 * - `$options` _array_: This parameter allows a validation rule to implement custom
537 * options.
bc7d5d7 @nateabele Fixing formatting and adding clarifying detail to documentation in `\…
nateabele authored
538 *
539 * @see lithium\util\Validator::$_rules
e7f3c31 @gwoo going lithium
gwoo authored
540 * @param mixed $name The name of the validation rule (string), or an array of key/value pairs
541 * of names and rules.
542 * @param string $rule If $name is a string, this should be a string regular expression, or a
543 * closure that returns a boolean indicating success. Should be left blank if
544 * `$name` is an array.
545 * @param array $options The default options for validating this rule. An option which applies
546 * to all regular expression rules is `'contains'` which, if set to true, allows
547 * validated values to simply _contain_ a match to a rule, rather than exactly
548 * matching it in whole.
549 * @return void
550 */
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored
551 public static function add($name, $rule = null, array $options = array()) {
e7f3c31 @gwoo going lithium
gwoo authored
552 if (!is_array($name)) {
553 $name = array($name => $rule);
554 }
555 static::$_rules = Set::merge(static::$_rules, $name);
556
557 if (!empty($options)) {
558 $options = array_combine(array_keys($name), array_fill(0, count($name), $options));
559 static::$_options = Set::merge(static::$_options, $options);
560 }
561 }
562
563 /**
564 * Checks a single value against a single validation rule in one or more formats.
565 *
64f6039 @jperras Replacing various instances of array_key_exists with isset().
jperras authored
566 * @param string $rule
567 * @param mixed $value
568 * @param string $format
97c5e7c @Howard3 Doing a general QA and Docblock
Howard3 authored
569 * @param array $options
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
570 * @return boolean Returns `true` or `false` indicating whether the validation rule check
571 * succeeded or failed.
59cb735 @nateabele Ensuring `@filter` tags are present in docblocks that support filteri…
nateabele authored
572 * @filter
e7f3c31 @gwoo going lithium
gwoo authored
573 */
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored
574 public static function rule($rule, $value, $format = 'any', array $options = array()) {
e7f3c31 @gwoo going lithium
gwoo authored
575 if (!isset(static::$_rules[$rule])) {
5c0ba72 @davidpersson Appending period to error and exception messages where missing.
davidpersson authored
576 throw new InvalidArgumentException("Rule `{$rule}` is not a validation rule.");
e7f3c31 @gwoo going lithium
gwoo authored
577 }
578 $defaults = isset(static::$_options[$rule]) ? static::$_options[$rule] : array();
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
579 $options = (array) $options + $defaults + static::$_options['defaults'];
e7f3c31 @gwoo going lithium
gwoo authored
580
581 $ruleCheck = static::$_rules[$rule];
582 $ruleCheck = is_array($ruleCheck) ? $ruleCheck : array($ruleCheck);
583
584 if (!$options['contains'] && !empty($ruleCheck)) {
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
585 foreach ($ruleCheck as $key => $item) {
586 $ruleCheck[$key] = is_string($item) ? "/^{$item}$/" : $item;
587 }
e7f3c31 @gwoo going lithium
gwoo authored
588 }
589
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
590 $params = compact('value', 'format', 'options');
591 return static::_filter($rule, $params, static::_checkFormats($ruleCheck));
e7f3c31 @gwoo going lithium
gwoo authored
592 }
593
594 /**
904be25 @nateabele Refactoring `Validator::check()`, implementing updated rules syntax. …
nateabele authored
595 * Returns a list of available validation rules, or the configuration details of a single rule.
596 *
597 * @param string $name Optional name of a rule to get the details of. If not specified, an array
598 * of all available rule names is returned. Otherwise, returns the details of a
599 * single rule. This can be a regular expression string, a closure object, or an
600 * array of available rule formats made up of string regular expressions,
601 * closures, or both.
602 * @return mixed Returns either an single array of rule names, or the details of a single rule.
603 */
604 public static function rules($name = null) {
605 if (!$name) {
606 return array_keys(static::$_rules);
607 }
608 return isset(static::$_rules[$name]) ? static::$_rules[$name] : null;
609 }
610
611 /**
e7f3c31 @gwoo going lithium
gwoo authored
612 * Perform validation checks against a value using an array of all possible formats for a rule,
613 * and an array specifying which formats within the rule to use.
614 *
615 * @param array $rules All available rules.
536efae @svemir Update comments to specify when a method is returning a Closure
svemir authored
616 * @return closure Function returning boolean `true` if validation succeeded, `false` otherwise.
e7f3c31 @gwoo going lithium
gwoo authored
617 */
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
618 protected static function _checkFormats($rules) {
619 return function($self, $params, $chain) use ($rules) {
88a2ecc @Howard3 Validator: Fixed a condition where 'format' wasn't being properly app…
Howard3 authored
620 $value = $params['value'];
621 $format = $params['format'];
622 $options = $params['options'];
623
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
624 $defaults = array('all' => true);
625 $options += $defaults;
e7f3c31 @gwoo going lithium
gwoo authored
626
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
627 $formats = (array) $format;
88a2ecc @Howard3 Validator: Fixed a condition where 'format' wasn't being properly app…
Howard3 authored
628 $options['all'] = ($format == 'any');
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
629
88a2ecc @Howard3 Validator: Fixed a condition where 'format' wasn't being properly app…
Howard3 authored
630 foreach ($rules as $index => $check) {
631 if (!$options['all'] && !(in_array($index, $formats) || isset($formats[$index]))) {
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
632 continue;
633 }
e7f3c31 @gwoo going lithium
gwoo authored
634
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
635 $regexPassed = (is_string($check) && preg_match($check, $value));
860cffb @Howard3 Closes #370 & #371
Howard3 authored
636 $closurePassed = (is_object($check) && $check($value, $format, $options));
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
637
638 if (!$options['all'] && ($regexPassed || $closurePassed)) {
639 return true;
640 }
641 if ($options['all'] && (!$regexPassed && !$closurePassed)) {
642 return false;
643 }
e7f3c31 @gwoo going lithium
gwoo authored
644 }
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
645 return $options['all'];
646 };
e7f3c31 @gwoo going lithium
gwoo authored
647 }
648 }
649
650 ?>
Something went wrong with that request. Please try again.