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