9
9
* </pre>
10
10
*
11
11
* @author Craig Manley
12
- * @copyright Copyright © 2013 , Craig Manley (www.craigmanley.com)
12
+ * @copyright Copyright © 2016 , Craig Manley (www.craigmanley.com)
13
13
* @license http://www.opensource.org/licenses/mit-license.php Licensed under MIT
14
- * @version $Id: Spec.class.php,v 1.5 2016/02/16 02:54:43 cmanley Exp $
14
+ * @version $Id: Spec.class.php,v 1.6 2016/02/17 22:25:33 cmanley Exp $
15
15
* @package Validate
16
16
*/
17
17
namespace Validate ;
25
25
26
26
27
27
/**
28
- * Validation class.
29
- * Encapsulates a validation object, as well as some extra specification options, and can validate single values.
28
+ * A Spec object encapsulates a Validation object as well as some extra attributes.
29
+ * See the constructor documentation for all the possible parameters.
30
+ * The Spec class is rarely used stand-alone since it is only able to validate a single value.
30
31
*
31
32
* SYNOPSIS:
32
33
*
@@ -90,10 +91,10 @@ class Spec {
90
91
*
91
92
* The following options are supported:
92
93
* <pre>
93
- * allow_empty : boolean, allow empty strings to be validated and pass optional check
94
+ * allow_empty : boolean, allow empty strings to be validated and pass ' optional' check
94
95
* before : callback that takes a reference to the value as argument so that it can mutate it before validation
95
96
* after : callback that takes a reference to the value as argument so that it can mutate it after validation
96
- * default : any non-null value (even closures!); using this causes null arguments to bypass validation and callbacks
97
+ * default : any non-null value (even closures!); null arguments to validate() are replaced with this (or it's result in if it's a closure)
97
98
* optional : boolean, if true, then null values are allowed
98
99
* description : optional description used in exception messages
99
100
* validation : Validation object
@@ -182,7 +183,7 @@ public function __get($key) {
182
183
183
184
184
185
/**
185
- * Return the allow_empty option as passed into the constructor.
186
+ * Returns the value of the ' allow_empty' option as passed into the constructor.
186
187
*
187
188
* @return boolean
188
189
*/
@@ -192,7 +193,7 @@ public function allow_empty() {
192
193
193
194
194
195
/**
195
- * Return the option as passed into the constructor.
196
+ * Returns the value of the 'default' option as passed into the constructor.
196
197
*
197
198
* @return string|null
198
199
*/
@@ -202,7 +203,7 @@ public function getDefault() {
202
203
203
204
204
205
/**
205
- * Return the option as passed into the constructor.
206
+ * Returns the value of the 'description' option as passed into the constructor.
206
207
*
207
208
* @return string|null
208
209
*/
@@ -232,7 +233,7 @@ public function after() {
232
233
233
234
234
235
/**
235
- * Return the optional option as passed in the constructor.
236
+ * Returns the value of the ' optional' option as passed into the constructor.
236
237
*
237
238
* @return boolean
238
239
*/
@@ -242,7 +243,7 @@ public function optional() {
242
243
243
244
244
245
/**
245
- * Return the nocase validation as passed in the constructor.
246
+ * Return the value of the ' validation' option as passed into or created by the constructor.
246
247
*
247
248
* @return Validation|null
248
249
*/
@@ -252,17 +253,19 @@ public function validation() {
252
253
253
254
254
255
/**
255
- * Return the name of the check the last validation failed on.
256
+ * Returns the name of the check that the last validation failed on.
256
257
*
257
- * @return string
258
+ * @return string|null
258
259
*/
259
260
public function getLastFailure () {
260
261
return $ this ->last_failure ;
261
262
}
262
263
263
264
264
265
/**
265
- * Validates the given argument.
266
+ * Validates the given argument reference.
267
+ * If 'before' or 'after' callback options were passed into the constructor,
268
+ * then these are applied to the argument in order to modify it in place, which is why it is passed by reference.
266
269
*
267
270
* @param mixed &$arg
268
271
* @return boolean
@@ -302,40 +305,14 @@ public function validate(&$arg) {
302
305
303
306
304
307
/**
305
- * Validates the given argument and throws a ValidationCheckException on failure.
308
+ * This simply wraps the validate() method in order to throw a ValidationCheckException on failure instead of returning a boolean .
306
309
*
307
310
* @param mixed &$arg
308
311
* @throws ValidationCheckException
309
312
*/
310
313
public function validate_ex (&$ arg ) {
311
- if (is_string ( $ arg ) && ! strlen ( $ arg ) && ! $ this ->allow_empty ) {
312
- $ arg = null ;
314
+ if (! $ this ->validate ( $ arg ) ) {
315
+ throw new ValidationCheckException ( $ this -> getLastFailure (), $ arg ) ;
313
316
}
314
- if (is_null ($ arg ) && !is_null ($ this ->default )) {
315
- $ arg = is_object ($ this ->default ) && ($ this ->default instanceof \Closure) ? call_user_func ($ this ->default ) : $ this ->default ;
316
- }
317
- else {
318
- if (is_null ($ arg )) {
319
- if (!$ this ->optional ) {
320
- $ this ->last_failure = 'mandatory ' ;
321
- throw new ValidationCheckException ('mandatory ' , $ arg );
322
- }
323
- }
324
- else {
325
- if ($ this ->before ) {
326
- call_user_func_array ($ this ->before , array (&$ arg ));
327
- }
328
- if ($ this ->validation ) {
329
- if (!$ this ->validation ->validate ($ arg )) {
330
- $ this ->last_failure = $ this ->validation ->getLastFailure ();
331
- throw new ValidationCheckException ($ this ->last_failure , $ arg );
332
- }
333
- }
334
- if ($ this ->after ) {
335
- call_user_func_array ($ this ->after , array (&$ arg ));
336
- }
337
- }
338
- }
339
- $ this ->last_failure = null ;
340
317
}
341
318
}
0 commit comments