Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 663 lines (622 sloc) 30.2 kB
e7f3c31 @gwoo going lithium
gwoo authored
1 <?php
2 /**
3 * Lithium: the most rad php framework
4 *
14de7bf @gwoo Happy 2012!
gwoo authored
5 * @copyright Copyright 2012, 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.
8a9859f @davidpersson Adding note about UTF-8 encoded strings to `Validator`.
davidpersson authored
131 *
132 * ### UTF-8 encoded input strings
133 *
134 * All rules operating on strings have been created with the possibility of
135 * UTF-8 encoded input in mind. A default PHP binary and an enabled Lithium
136 * g11n bootstrap will make these rules work correctly in any case. Should you
137 * ever experience odd behavior following paragraph with implementation
138 * details might help you to track to the cause.
139 *
140 * The rules `alphaNumeric` and `money` rely on additional functionality of
141 * PCRE to validate UTF-8 encoded strings. As no PCRE feature detection is
142 * done, having this feature enabled in PCRE isn't optional. Please ensure
143 * you've got PCRE compiled with UTF-8 support.
e4bc392 Adding Validator docs.
psychic authored
144 */
e7f3c31 @gwoo going lithium
gwoo authored
145 class Validator extends \lithium\core\StaticObject {
146
147 /**
148 * An array of validation rules. May contain a single regular expression, an array of regular
149 * expressions (where the array keys define various possible 'formats' of the same rule), or a
150 * closure which accepts a value to be validated, and an array of options, and returns a
151 * boolean value, indicating whether the validation succeeded or failed.
152 *
153 * @var array
154 * @see lithium\util\Validator::add()
155 * @see lithium\util\Validator::rule()
156 */
157 protected static $_rules = array();
158
0c12c78 @jperras Adding missing doc block for util\Validator::.
jperras authored
159 /**
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
160 * Default options used when defining a new validator rule. Each key contains method-specific
161 * options that should always be applied, or options that should be applied to all rules in the
162 * `'defaults'` key.
0c12c78 @jperras Adding missing doc block for util\Validator::.
jperras authored
163 *
164 * @see lithium\util\Validator::add()
165 * @see lithium\util\Validator::rule()
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
166 * @var array
0c12c78 @jperras Adding missing doc block for util\Validator::.
jperras authored
167 */
e7f3c31 @gwoo going lithium
gwoo authored
168 protected static $_options = array(
169 'defaults' => array('contains' => true)
170 );
171
172 /**
173 * Initializes the list of default validation rules.
174 *
175 * @return void
176 */
177 public static function __init() {
178 $alnum = '[A-Fa-f0-9]';
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
179 $class = get_called_class();
180 static::$_methodFilters[$class] = array();
e7f3c31 @gwoo going lithium
gwoo authored
181
182 static::$_rules = array(
183 'alphaNumeric' => '/^[\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]+$/mu',
184 'blank' => '/[^\\s]/',
185 'creditCard' => array(
186 'amex' => '/^3[4|7]\\d{13}$/',
187 'bankcard' => '/^56(10\\d\\d|022[1-5])\\d{10}$/',
188 'diners' => '/^(?:3(0[0-5]|[68]\\d)\\d{11})|(?:5[1-5]\\d{14})$/',
189 'disc' => '/^(?:6011|650\\d)\\d{12}$/',
190 'electron' => '/^(?:417500|4917\\d{2}|4913\\d{2})\\d{10}$/',
191 'enroute' => '/^2(?:014|149)\\d{11}$/',
192 'jcb' => '/^(3\\d{4}|2100|1800)\\d{11}$/',
193 'maestro' => '/^(?:5020|6\\d{3})\\d{12}$/',
194 'mc' => '/^5[1-5]\\d{14}$/',
195 'solo' => '/^(6334[5-9][0-9]|6767[0-9]{2})\\d{10}(\\d{2,3})?$/',
196 'switch' => '/^(?:49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2})' .
197 '\\d{10}(\\d{2,3})?)|(?:564182\\d{10}(\\d{2,3})?)|(6(3(33[0-4]' .
198 '[0-9])|759[0-9]{2})\\d{10}(\\d{2,3})?)$/',
199 'visa' => '/^4\\d{12}(\\d{3})?$/',
200 'voyager' => '/^8699[0-9]{11}$/',
201 '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
202 '(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/'
e7f3c31 @gwoo going lithium
gwoo authored
203 ),
204 'date' => array(
205 'dmy' => '%^(?:(?:31(\\/|-|\\.|\\x20)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)' .
206 '(\\/|-|\\.|\\x20)(?:0?[1,3-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?' .
207 '\\d{2})$|^(?:29(\\/|-|\\.|\\x20)0?2\\3(?:(?:(?:1[6-9]|[2-9]\\d)?' .
208 '(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])' .
209 '00))))$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.|\\x20)(?:(?:0?[1-9])|' .
210 '(?:1[0-2]))\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%',
211 'mdy' => '%^(?:(?:(?:0?[13578]|1[02])(\\/|-|\\.|\\x20)31)\\1|(?:(?:0?[13-9]|' .
212 '1[0-2])(\\/|-|\\.|\\x20)(?:29|30)\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d' .
213 '{2})$|^(?:0?2(\\/|-|\\.|\\x20)29\\3(?:(?:(?:1[6-9]|[2-9]\\d)?' .
214 '(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])' .
215 '00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\\/|-|\\.|\\x20)(?:0?[1-9]|1' .
216 '\\d|2[0-8])\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%',
217 'ymd' => '%^(?:(?:(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579]' .
218 '[26])|(?:(?:16|[2468][048]|[3579][26])00)))(\\/|-|\\.|\\x20)' .
219 '(?:0?2\\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\\d)?\\d{2})(\\/|-|\\.|' .
220 '\\x20)(?:(?:(?:0?[13578]|1[02])\\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])' .
221 '\\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\\2(?:0?[1-9]|1\\d|2[0-8]' .
222 '))))$%',
223 'dMy' => '/^((31(?!\\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\\b|t)t?|Nov)' .
224 '(ember)?)))|((30|29)(?!\\ Feb(ruary)?))|(29(?=\\ Feb(ruary)?\\ ' .
225 '(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468]' .
226 '[048]|[3579][26])00)))))|(0?[1-9])|1\\d|2[0-8])\\ (Jan(uary)?|' .
227 'Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|' .
228 'Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)\\ ((1[6-9]|[2-9]' .
229 '\\d)\\d{2})$/',
230 'Mdy' => '/^(?:(((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?' .
231 '|Dec(ember)?)\\ 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)' .
232 '|(ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?)\\ (0?[1-9]' .
233 '|([12]\\d)|30))|(Feb(ruary)?\\ (0?[1-9]|1\\d|2[0-8]|(29(?=,?\\ ' .
234 '((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468]' .
235 '[048]|[3579][26])00)))))))\\,?\\ ((1[6-9]|[2-9]\\d)\\d{2}))$/',
236 'My' => '%^(Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|' .
237 'Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)[ /]((1[6-9]' .
238 '|[2-9]\\d)\\d{2})$%',
239 'my' => '%^(((0[123456789]|10|11|12)([- /.])(([1][9][0-9][0-9])|([2][0-9]' .
240 '[0-9][0-9]))))$%'
241 ),
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
242 'ip' => function($value, $format = null, array $options = array()) {
243 $options += array('flags' => array());
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
244 return (boolean) filter_var($value, FILTER_VALIDATE_IP, $options);
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
245 },
e7f3c31 @gwoo going lithium
gwoo authored
246 'money' => array(
247 'right' => '/^(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?(?:\1\d{3})*|(?:\d+))' .
248 '((?!\1)[,.]\d{2})?(?<!\x{00a2})\p{Sc}?$/u',
249 'left' => '/^(?!\x{00a2})\p{Sc}?(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?' .
860cffb @Howard3 Closes #370 & #371
Howard3 authored
250 '(?:\1\d{3})*|(?:\d+))((?!\1)[,.]\d{2})?$/u'
e7f3c31 @gwoo going lithium
gwoo authored
251 ),
252 'notEmpty' => '/[^\s]+/m',
338196b @davidpersson Moving locale dependent validation rules to g11n resources.
davidpersson authored
253 'phone' => '/^\+?[0-9\(\)\-]{10,20}$/',
254 'postalCode' => '/(^|\A\b)[A-Z0-9\s\-]{5,}($|\b\z)/i',
c8d7278 @nateabele Implementing alternative regular expression delimiters for validation…
nateabele authored
255 'regex' => '/^(?:([^[:alpha:]\\\\{<\[\(])(.+)(?:\1))|(?:{(.+)})|(?:<(.+)>)|' .
256 '(?:\[(.+)\])|(?:\((.+)\))[gimsxu]*$/',
e7f3c31 @gwoo going lithium
gwoo authored
257 'time' => '%^((0?[1-9]|1[012])(:[0-5]\d){0,2}([AP]M|[ap]m))$|^([01]\d|2[0-3])' .
258 '(:[0-5]\d){0,2}$%',
c8d7278 @nateabele Implementing alternative regular expression delimiters for validation…
nateabele authored
259 'boolean' => function($value) {
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
260 $bool = is_bool($value);
0c12c78 @jperras Adding missing doc block for util\Validator::.
jperras authored
261 $filter = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
1bb901c @joebeeson Adding empty() check in boolean validator to allow empty values. Upda…
joebeeson authored
262 return ($bool || $filter !== null || empty($value));
e7f3c31 @gwoo going lithium
gwoo authored
263 },
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored
264 'decimal' => function($value, $format = null, array $options = array()) {
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
265 if (isset($options['precision'])) {
266 $precision = strlen($value) - strrpos($value, '.') - 1;
e7f3c31 @gwoo going lithium
gwoo authored
267
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
268 if ($precision !== (int) $options['precision']) {
269 return false;
270 }
271 }
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
272 return (filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE) !== null);
e7f3c31 @gwoo going lithium
gwoo authored
273 },
274 'inList' => function($value, $format, $options) {
275 $options += array('list' => array());
276 return in_array($value, $options['list']);
277 },
278 'lengthBetween' => function($value, $format, $options) {
279 $length = strlen($value);
280 $options += array('min' => 1, 'max' => 255);
281 return ($length >= $options['min'] && $length <= $options['max']);
282 },
283 'luhn' => function($value) {
284 if (empty($value) || !is_string($value)) {
285 return false;
286 }
287 $sum = 0;
288 $length = strlen($value);
289
290 for ($position = 1 - ($length % 2); $position < $length; $position += 2) {
291 $sum += $value[$position];
292 }
293 for ($position = ($length % 2); $position < $length; $position += 2) {
294 $number = $value[$position] * 2;
295 $sum += ($number < 10) ? $number : $number - 9;
296 }
297 return ($sum % 10 == 0);
298 },
299 'numeric' => function($value) {
300 return is_numeric($value);
301 },
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
302 'inRange' => function($value, $format, $options) {
303 $defaults = array('upper' => null, 'lower' => null);
304 $options += $defaults;
305
306 if (!is_numeric($value)) {
307 return false;
308 }
309 switch (true) {
310 case (!is_null($options['upper']) && !is_null($options['lower'])):
311 return ($value > $options['lower'] && $value < $options['upper']);
312 case (!is_null($options['upper'])):
313 return ($value < $options['upper']);
314 case (!is_null($options['lower'])):
315 return ($value > $options['lower']);
316 }
317 return is_finite($value);
318 },
8c8279c @ddebernardy fix insufficiently robust uuid validator
ddebernardy authored
319 'uuid' => "/^{$alnum}{8}-{$alnum}{4}-{$alnum}{4}-{$alnum}{4}-{$alnum}{12}$/",
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
320 'email' => function($value) {
321 return filter_var($value, FILTER_VALIDATE_EMAIL);
322 },
323 'url' => function($value, $format = null, array $options = array()) {
324 $options += array('flags' => array());
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
325 return (boolean) filter_var($value, FILTER_VALIDATE_URL, $options);
5fba2fb @jperras Modifying util\Validator to use filter_var() where possible.
jperras authored
326 }
e7f3c31 @gwoo going lithium
gwoo authored
327 );
328
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
329 $isEmpty = function($self, $params, $chain) {
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
330 extract($params);
331 return (empty($value) && $value != '0') ? false : $chain->next($self, $params, $chain);
e7f3c31 @gwoo going lithium
gwoo authored
332 };
333
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
334 static::$_methodFilters[$class]['alphaNumeric'] = array($isEmpty);
335 static::$_methodFilters[$class]['notEmpty'] = array($isEmpty);
e7f3c31 @gwoo going lithium
gwoo authored
336
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
337 static::$_methodFilters[$class]['creditCard'] = array(function($self, $params, $chain) {
338 extract($params);
e7f3c31 @gwoo going lithium
gwoo authored
339 $options += array('deep' => false);
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
340
341 if (strlen($value = str_replace(array('-', ' '), '', $value)) < 13) {
342 return false;
343 }
344 if (!$chain->next($self, compact('value') + $params, $chain)) {
345 return false;
346 }
e7f3c31 @gwoo going lithium
gwoo authored
347 return $options['deep'] ? Validator::isLuhn($value) : true;
348 });
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
349
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
350 static::$_methodFilters[$class]['email'] = array(function($self, $params, $chain) {
351 extract($params);
352 $defaults = array('deep' => false);
353 $options += $defaults;
e7f3c31 @gwoo going lithium
gwoo authored
354
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
355 if (!$chain->next($self, $params, $chain)) {
d87fbbe @gwoo changing deep email validator to use getmxrr
gwoo authored
356 return false;
e7f3c31 @gwoo going lithium
gwoo authored
357 }
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
358 if (!$options['deep']) {
359 return true;
360 }
361 list($prefix, $host) = explode('@', $params['value']);
362
363 if (getmxrr($host, $mxhosts)) {
364 return is_array($mxhosts);
365 }
366 return false;
367 });
e7f3c31 @gwoo going lithium
gwoo authored
368 }
369
370 /**
371 * Maps method calls to validation rule names. For example, a validation rule that would
372 * normally be called as `Validator::rule('email', 'foo@bar.com')` can also be called as
373 * `Validator::isEmail('foo@bar.com')`.
374 *
375 * @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
376 * @param array $args
e7f3c31 @gwoo going lithium
gwoo authored
377 * @return boolean
378 */
379 public static function __callStatic($method, $args = array()) {
380 if (!isset($args[0])) {
381 return false;
382 }
88a2ecc @Howard3 Validator: Fixed a condition where 'format' wasn't being properly app…
Howard3 authored
383 $args = array_filter($args) + array(0 => $args[0], 1 => 'any', 2 => array());
e7f3c31 @gwoo going lithium
gwoo authored
384 $rule = preg_replace("/^is([A-Z][A-Za-z0-9]+)$/", '$1', $method);
385 $rule[0] = strtolower($rule[0]);
386 return static::rule($rule, $args[0], $args[1], $args[2]);
387 }
388
389 /**
fbda39a @nateabele Updating `Validator` docs and replacing code snippets with unit test …
nateabele authored
390 * Checks a set of values against a specified rules list. This method may be used to validate
391 * any arbitrary array of data against a set of validation rules.
e7f3c31 @gwoo going lithium
gwoo authored
392 *
fbda39a @nateabele Updating `Validator` docs and replacing code snippets with unit test …
nateabele authored
393 * @param array $values An array of key/value pairs, where the values are to be checked.
394 * @param array $rules An array of rules to check the values in `$values` against. Each key in
395 * `$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
396 * 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
397 * 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
398 * `$rules` would be as follows:
1ec455e @nateabele Updating example used in docblock of `\util\Validator::check()`. Fixe…
nateabele authored
399 * - `array('credit_card' => 'You must include a credit card number')`: This is the
400 * simplest form of validation rule, in which the value is simply a message to
401 * display if the rule fails. Using this format, all other validation settings
402 * inherit from the defaults, including the validation rule itself, which only
403 * checks to see that the corresponding key in `$values` is present and contains
404 * a value that is not empty. _Please note when globalizing validation messages:_
405 * When specifying messages, it may be preferable to use a code string (i.e.
406 * `'ERR_NO_TITLE'`) instead of the full text of the validation error. These code
407 * strings may then be translated by the appropriate tools in the templating
408 * layer.
409 * - `array('credit_card' => array('creditCard', 'message' => 'Invalid CC #'))`:
410 * In the second format, the validation rule (in this case `creditCard`) and
411 * associated configuration are specified as an array, where the rule to use is
412 * the first value in the array (no key), and additional settings are specified
413 * as other keys in the array. Please see the list below for more information on
414 * allowed keys.
5089955 @nateabele Updating documentation for `\util\Validator`, adding tests to prove f…
nateabele authored
415 * - The final format allows you to apply multiple validation rules to a single
416 * value, and it is specified as follows:
417 *
1ec455e @nateabele Updating example used in docblock of `\util\Validator::check()`. Fixe…
nateabele authored
418 * `array('credit_card' => array(
419 * array('notEmpty', 'message' => 'You must include credit card number'),
420 * array('creditCard', 'message' => 'Your credit card number must be valid')
5089955 @nateabele Updating documentation for `\util\Validator`, adding tests to prove f…
nateabele authored
421 * ));`
470d76e @jperras Fixing coding standards violations in `util\Validator`.
jperras authored
422 * @param array $options Validator-specific options.
5089955 @nateabele Updating documentation for `\util\Validator`, adding tests to prove f…
nateabele authored
423 *
424 * Each rule defined as an array can contain any of the following settings (in addition to the
425 * first value, which represents the rule to be used):
426 * - `'message'` _string_: The error message to be returned if the validation rule fails. See
427 * the note above regarding globalization of error messages.
428 * - `'required`' _boolean_: Represents whether the value is required to be present in
429 * `$values`. If `'required'` is set to `false`, the validation rule will be skipped if the
430 * corresponding key is not present. Defaults to `true`.
431 * - `'skipEmpty'` _boolean_: Similar to `'required'`, this setting (if `true`) will cause the
432 * validation rule to be skipped if the corresponding value is empty (an empty string or
433 * `null`). Defaults to `false`.
434 * - `'format'` _string_: If the validation rule has multiple format definitions (see the
435 * `add()` or `__init()` methods), the name of the format to be used can be specified here.
436 * Additionally, two special values can be used: either `'any'`, which means that all formats
437 * will be checked and the rule will pass if any format passes, or `'all'`, which requires
438 * 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
439 * @return array Returns an array containing all validation failures for data in `$values`,
440 * where each key matches a key in `$values`, and each value is an array of that
441 * element's validation errors.
011811c @Ciaro Making Validator::check filterable.
Ciaro authored
442 * @filter
e7f3c31 @gwoo going lithium
gwoo authored
443 */
fbda39a @nateabele Updating `Validator` docs and replacing code snippets with unit test …
nateabele authored
444 public static function check(array $values, array $rules, array $options = array()) {
904be25 @nateabele Refactoring `Validator::check()`, implementing updated rules syntax. …
nateabele authored
445 $defaults = array(
446 'notEmpty',
447 'message' => null,
448 'required' => true,
449 'skipEmpty' => false,
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
450 'format' => 'any',
afe30c7 @vesln Adding support for "last" rule in `Validator::check` method
vesln authored
451 'on' => null,
452 'last' => false
904be25 @nateabele Refactoring `Validator::check()`, implementing updated rules syntax. …
nateabele authored
453 );
454
011811c @Ciaro Making Validator::check filterable.
Ciaro authored
455 $options += $defaults;
456 $params = compact('values', 'rules', 'options');
e86fd73 @nateabele Implementing validator events, to enable conditional validation. To u…
nateabele authored
457
011811c @Ciaro Making Validator::check filterable.
Ciaro authored
458 return static::_filter(__FUNCTION__, $params, function($self, $params) {
459 $values = $params['values'];
460 $rules = $params['rules'];
461 $options = $params['options'];
827e14f @daschl QA: formatting and docblock changes.
daschl authored
462
011811c @Ciaro Making Validator::check filterable.
Ciaro authored
463 $errors = array();
464 $events = (array) (isset($options['events']) ? $options['events'] : null);
465 $values = Set::flatten($values);
904be25 @nateabele Refactoring `Validator::check()`, implementing updated rules syntax. …
nateabele authored
466
011811c @Ciaro Making Validator::check filterable.
Ciaro authored
467 foreach ($rules as $field => $rules) {
468 $rules = is_string($rules) ? array('message' => $rules) : $rules;
469 $rules = is_array(current($rules)) ? $rules : array($rules);
470 $errors[$field] = array();
471 $options['field'] = $field;
472
473 foreach ($rules as $key => $rule) {
474 $rule += $options + compact('values');
475 list($name) = $rule;
476
477 if ($events && $rule['on'] && !array_intersect($events, (array) $rule['on'])) {
478 continue;
4fceb8b @gwoo refactoring Model::validates. adding Record::errors
gwoo authored
479 }
c4c08c5 @mehlah Fix a regression on Validator::check and skipEmpty option
mehlah authored
480 if (!array_key_exists($field, $values)) {
011811c @Ciaro Making Validator::check filterable.
Ciaro authored
481 if ($rule['required']) {
482 $errors[$field][] = $rule['message'] ?: $key;
483 }
484 if ($rule['last']) {
485 break;
486 }
487 continue;
488 }
489 if (empty($values[$field]) && $rule['skipEmpty']) {
490 continue;
8d52861 @vesln Fixing the case when last is used on empty value with required option
vesln authored
491 }
30ab4c1 @daschl QA: Miscc QA fixes, including trailing commas, param documentation an…
daschl authored
492
011811c @Ciaro Making Validator::check filterable.
Ciaro authored
493 if (!$self::rule($name, $values[$field], $rule['format'], $rule + $options)) {
494 $errors[$field][] = $rule['message'] ?: $key;
827e14f @daschl QA: formatting and docblock changes.
daschl authored
495
011811c @Ciaro Making Validator::check filterable.
Ciaro authored
496 if ($rule['last']) {
497 break;
498 }
8d52861 @vesln Fixing the case when last is used on empty value with required option
vesln authored
499 }
4fceb8b @gwoo refactoring Model::validates. adding Record::errors
gwoo authored
500 }
501 }
011811c @Ciaro Making Validator::check filterable.
Ciaro authored
502 return array_filter($errors);
503 });
e7f3c31 @gwoo going lithium
gwoo authored
504 }
505
506 /**
507 * Adds to or replaces built-in validation rules specified in `Validator::$_rules`. Any new
508 * validation rules created are automatically callable as validation methods.
64f6039 @jperras Replacing various instances of array_key_exists with isset().
jperras authored
509 *
e7f3c31 @gwoo going lithium
gwoo authored
510 * For example:
511 * {{{
512 * Validator::add('zeroToNine', '/^[0-9]$/');
513 * $isValid = Validator::isZeroToNine("5"); // true
514 * $isValid = Validator::isZeroToNine("20"); // false
515 * }}}
64f6039 @jperras Replacing various instances of array_key_exists with isset().
jperras authored
516 *
e7f3c31 @gwoo going lithium
gwoo authored
517 * Alternatively, the first parameter may be an array of rules expressed as key/value pairs,
518 * as in the following:
519 * {{{
520 * Validator::add(array(
521 * 'zeroToNine' => '/^[0-9]$/',
522 * 'tenToNineteen' => '/^1[0-9]$/',
523 * ));
524 * }}}
64f6039 @jperras Replacing various instances of array_key_exists with isset().
jperras authored
525 *
bc7d5d7 @nateabele Fixing formatting and adding clarifying detail to documentation in `\…
nateabele authored
526 * In addition to regular expressions, validation rules can also be defined as full anonymous
527 * functions:
528 * {{{
529 * use app\models\Account;
530 *
531 * Validator::add('accountActive', function($value) {
532 * $value = is_int($value) ? Account::find($value) : $value;
533 * return (boolean) $value->is_active;
534 * });
535 *
536 * $testAccount = Account::create(array('is_active' => false));
537 * Validator::isAccountActive($testAccount); // returns false
538 * }}}
539 *
540 * These functions can take up to 3 parameters:
541 * - `$value` _mixed_: This is the actual value to be validated (as in the above example).
542 * - `$format` _string_: Often, validation rules come in multiple "formats", for example:
543 * 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
544 * retian flexibility in how you validate data. In cases where a user's country of origin
545 * is known, the appropriate validation rule may be selected. In cases where it is not
546 * known, the value of `$format` may be `'any'`, which should pass if any format matches.
547 * In cases where validation rule formats are not mutually exclusive, the value may be
548 * `'all'`, in which case all must match.
549 * - `$options` _array_: This parameter allows a validation rule to implement custom
550 * options.
bc7d5d7 @nateabele Fixing formatting and adding clarifying detail to documentation in `\…
nateabele authored
551 *
552 * @see lithium\util\Validator::$_rules
e7f3c31 @gwoo going lithium
gwoo authored
553 * @param mixed $name The name of the validation rule (string), or an array of key/value pairs
554 * of names and rules.
555 * @param string $rule If $name is a string, this should be a string regular expression, or a
556 * closure that returns a boolean indicating success. Should be left blank if
557 * `$name` is an array.
558 * @param array $options The default options for validating this rule. An option which applies
559 * to all regular expression rules is `'contains'` which, if set to true, allows
560 * validated values to simply _contain_ a match to a rule, rather than exactly
561 * matching it in whole.
562 * @return void
563 */
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored
564 public static function add($name, $rule = null, array $options = array()) {
e7f3c31 @gwoo going lithium
gwoo authored
565 if (!is_array($name)) {
566 $name = array($name => $rule);
567 }
568 static::$_rules = Set::merge(static::$_rules, $name);
569
570 if (!empty($options)) {
571 $options = array_combine(array_keys($name), array_fill(0, count($name), $options));
572 static::$_options = Set::merge(static::$_options, $options);
573 }
574 }
575
576 /**
577 * Checks a single value against a single validation rule in one or more formats.
578 *
64f6039 @jperras Replacing various instances of array_key_exists with isset().
jperras authored
579 * @param string $rule
580 * @param mixed $value
581 * @param string $format
97c5e7c @Howard3 Doing a general QA and Docblock
Howard3 authored
582 * @param array $options
b6a2d05 @nateabele Cleaning up `\util\Validator`, and adding documentation on various va…
nateabele authored
583 * @return boolean Returns `true` or `false` indicating whether the validation rule check
584 * succeeded or failed.
59cb735 @nateabele Ensuring `@filter` tags are present in docblocks that support filteri…
nateabele authored
585 * @filter
e7f3c31 @gwoo going lithium
gwoo authored
586 */
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored
587 public static function rule($rule, $value, $format = 'any', array $options = array()) {
e7f3c31 @gwoo going lithium
gwoo authored
588 if (!isset(static::$_rules[$rule])) {
5c0ba72 @davidpersson Appending period to error and exception messages where missing.
davidpersson authored
589 throw new InvalidArgumentException("Rule `{$rule}` is not a validation rule.");
e7f3c31 @gwoo going lithium
gwoo authored
590 }
591 $defaults = isset(static::$_options[$rule]) ? static::$_options[$rule] : array();
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
592 $options = (array) $options + $defaults + static::$_options['defaults'];
e7f3c31 @gwoo going lithium
gwoo authored
593
594 $ruleCheck = static::$_rules[$rule];
595 $ruleCheck = is_array($ruleCheck) ? $ruleCheck : array($ruleCheck);
596
597 if (!$options['contains'] && !empty($ruleCheck)) {
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
598 foreach ($ruleCheck as $key => $item) {
599 $ruleCheck[$key] = is_string($item) ? "/^{$item}$/" : $item;
600 }
e7f3c31 @gwoo going lithium
gwoo authored
601 }
602
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
603 $params = compact('value', 'format', 'options');
604 return static::_filter($rule, $params, static::_checkFormats($ruleCheck));
e7f3c31 @gwoo going lithium
gwoo authored
605 }
606
607 /**
904be25 @nateabele Refactoring `Validator::check()`, implementing updated rules syntax. …
nateabele authored
608 * Returns a list of available validation rules, or the configuration details of a single rule.
609 *
610 * @param string $name Optional name of a rule to get the details of. If not specified, an array
611 * of all available rule names is returned. Otherwise, returns the details of a
612 * single rule. This can be a regular expression string, a closure object, or an
613 * array of available rule formats made up of string regular expressions,
614 * closures, or both.
615 * @return mixed Returns either an single array of rule names, or the details of a single rule.
616 */
617 public static function rules($name = null) {
618 if (!$name) {
619 return array_keys(static::$_rules);
620 }
621 return isset(static::$_rules[$name]) ? static::$_rules[$name] : null;
622 }
623
624 /**
e7f3c31 @gwoo going lithium
gwoo authored
625 * Perform validation checks against a value using an array of all possible formats for a rule,
626 * and an array specifying which formats within the rule to use.
627 *
628 * @param array $rules All available rules.
536efae @svemir Update comments to specify when a method is returning a Closure
svemir authored
629 * @return closure Function returning boolean `true` if validation succeeded, `false` otherwise.
e7f3c31 @gwoo going lithium
gwoo authored
630 */
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
631 protected static function _checkFormats($rules) {
632 return function($self, $params, $chain) use ($rules) {
88a2ecc @Howard3 Validator: Fixed a condition where 'format' wasn't being properly app…
Howard3 authored
633 $value = $params['value'];
634 $format = $params['format'];
635 $options = $params['options'];
636
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
637 $defaults = array('all' => true);
638 $options += $defaults;
e7f3c31 @gwoo going lithium
gwoo authored
639
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
640 $formats = (array) $format;
88a2ecc @Howard3 Validator: Fixed a condition where 'format' wasn't being properly app…
Howard3 authored
641 $options['all'] = ($format == 'any');
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
642
88a2ecc @Howard3 Validator: Fixed a condition where 'format' wasn't being properly app…
Howard3 authored
643 foreach ($rules as $index => $check) {
644 if (!$options['all'] && !(in_array($index, $formats) || isset($formats[$index]))) {
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
645 continue;
646 }
e7f3c31 @gwoo going lithium
gwoo authored
647
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
648 $regexPassed = (is_string($check) && preg_match($check, $value));
860cffb @Howard3 Closes #370 & #371
Howard3 authored
649 $closurePassed = (is_object($check) && $check($value, $format, $options));
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
650
651 if (!$options['all'] && ($regexPassed || $closurePassed)) {
652 return true;
653 }
654 if ($options['all'] && (!$regexPassed && !$closurePassed)) {
655 return false;
656 }
e7f3c31 @gwoo going lithium
gwoo authored
657 }
f7a9ba6 @nateabele Removing unused validators in `util\Validator`, refactoring `Validato…
nateabele authored
658 return $options['all'];
659 };
e7f3c31 @gwoo going lithium
gwoo authored
660 }
661 }
662
663 ?>
Something went wrong with that request. Please try again.