Skip to content

Commit 3291c3b

Browse files
author
Craig Manley
committed
Documentation updates
1 parent 53813ec commit 3291c3b

8 files changed

+40
-59
lines changed

.cvsignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
.git
2+
docs
3+
md

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
CVS
2+
docs
3+
md

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2013 Craig Manley (craigmanley.com)
3+
Copyright (c) 2016 Craig Manley (craigmanley.com)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of
66
this software and associated documentation files (the "Software"), to deal in

Validate/Spec.class.php

+20-43
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* </pre>
1010
*
1111
* @author Craig Manley
12-
* @copyright Copyright © 2013, Craig Manley (www.craigmanley.com)
12+
* @copyright Copyright © 2016, Craig Manley (www.craigmanley.com)
1313
* @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 $
1515
* @package Validate
1616
*/
1717
namespace Validate;
@@ -25,8 +25,9 @@
2525

2626

2727
/**
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.
3031
*
3132
* SYNOPSIS:
3233
*
@@ -90,10 +91,10 @@ class Spec {
9091
*
9192
* The following options are supported:
9293
* <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
9495
* before : callback that takes a reference to the value as argument so that it can mutate it before validation
9596
* 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)
9798
* optional : boolean, if true, then null values are allowed
9899
* description : optional description used in exception messages
99100
* validation : Validation object
@@ -182,7 +183,7 @@ public function __get($key) {
182183

183184

184185
/**
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.
186187
*
187188
* @return boolean
188189
*/
@@ -192,7 +193,7 @@ public function allow_empty() {
192193

193194

194195
/**
195-
* Return the option as passed into the constructor.
196+
* Returns the value of the 'default' option as passed into the constructor.
196197
*
197198
* @return string|null
198199
*/
@@ -202,7 +203,7 @@ public function getDefault() {
202203

203204

204205
/**
205-
* Return the option as passed into the constructor.
206+
* Returns the value of the 'description' option as passed into the constructor.
206207
*
207208
* @return string|null
208209
*/
@@ -232,7 +233,7 @@ public function after() {
232233

233234

234235
/**
235-
* Return the optional option as passed in the constructor.
236+
* Returns the value of the 'optional' option as passed into the constructor.
236237
*
237238
* @return boolean
238239
*/
@@ -242,7 +243,7 @@ public function optional() {
242243

243244

244245
/**
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.
246247
*
247248
* @return Validation|null
248249
*/
@@ -252,17 +253,19 @@ public function validation() {
252253

253254

254255
/**
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.
256257
*
257-
* @return string
258+
* @return string|null
258259
*/
259260
public function getLastFailure() {
260261
return $this->last_failure;
261262
}
262263

263264

264265
/**
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.
266269
*
267270
* @param mixed &$arg
268271
* @return boolean
@@ -302,40 +305,14 @@ public function validate(&$arg) {
302305

303306

304307
/**
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.
306309
*
307310
* @param mixed &$arg
308311
* @throws ValidationCheckException
309312
*/
310313
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);
313316
}
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;
340317
}
341318
}

Validate/Specs.class.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
* </pre>
99
*
1010
* @author Craig Manley
11-
* @copyright Copyright © 2013, Craig Manley (www.craigmanley.com)
11+
* @copyright Copyright © 2016, Craig Manley (www.craigmanley.com)
1212
* @license http://www.opensource.org/licenses/mit-license.php Licensed under MIT
13-
* @version $Id: Specs.class.php,v 1.3 2016/02/16 02:55:38 cmanley Exp $
13+
* @version $Id: Specs.class.php,v 1.4 2016/02/17 22:25:33 cmanley Exp $
1414
* @package cmanley
1515
*/
1616
namespace Validate;

Validate/Validation.class.php

+7-8
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
* </pre>
99
*
1010
* @author Craig Manley
11-
* @copyright Copyright © 2013, Craig Manley (www.craigmanley.com)
11+
* @copyright Copyright © 2016, Craig Manley (www.craigmanley.com)
1212
* @license http://www.opensource.org/licenses/mit-license.php Licensed under MIT
13-
* @version $Id: Validation.class.php,v 1.5 2016/02/16 02:55:38 cmanley Exp $
13+
* @version $Id: Validation.class.php,v 1.6 2016/02/17 22:25:33 cmanley Exp $
1414
* @package Validate
1515
*/
1616
namespace Validate;
@@ -24,9 +24,8 @@
2424

2525

2626
/**
27-
* Validation class.
28-
* Encapsulates validations and validates.
29-
* This class is designed to also be used stand-alone.
27+
* The Validation class encapsulates checks for validating single non-null values.
28+
* This class may be used stand-alone, but it is typically used as a parameter for the Spec constructor.
3029
*
3130
* @package cmanley
3231
*/
@@ -58,16 +57,16 @@ class Validation {
5857
*
5958
* Below are the supported validations, in the order that they are applied during validation.
6059
* <pre>
60+
* type: allowed type as returned by gettype(), including 'scalar', 'int' (alias of 'integer'), 'float' (alias of 'double')
61+
* types: array of allowed types (see type)
62+
* resource_type: only used if 'resource' is in 'types' array
6163
* max_length: max string length, for scalar types
6264
* min_length: min string length, for scalar types
6365
* mb_max_length: max multibyte string length, for scalar types
6466
* mb_min_length: min multibyte string length, for scalar types
6567
* max_value: for numeric types
6668
* min_value: for numeric types
6769
* isa: allowed object type
68-
* resource_type: only used if 'resource' is in 'types' array
69-
* type: allowed type as returned by gettype(), including 'scalar', 'int' (alias of 'integer'), 'float' (alias of 'double')
70-
* types: array of allowed types (see type)
7170
* regex: validation regex string, e.g. '/^.{1,50}$/s'
7271
* callback: boolean closure function that receives the value as argument
7372
* callbacks: associative array of boolean closure functions that receive the value as argument

Validate/Validator.class.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* </pre>
1010
*
1111
* @author Craig Manley
12-
* @copyright Copyright © 2013, Craig Manley (www.craigmanley.com)
12+
* @copyright Copyright © 2016, Craig Manley (www.craigmanley.com)
1313
* @license http://www.opensource.org/licenses/mit-license.php Licensed under MIT
14-
* @version $Id: Validator.class.php,v 1.4 2016/02/16 02:55:38 cmanley Exp $
14+
* @version $Id: Validator.class.php,v 1.5 2016/02/17 22:25:33 cmanley Exp $
1515
* @package Validate
1616
*/
1717
namespace Validate;
@@ -26,7 +26,8 @@
2626

2727

2828
/**
29-
* Encapsulates a field definition.
29+
* Validator objects use their internal Specs to validate and possibly modify (associative) arrays.
30+
* Typical arrays that often require validation are those from form submissions, reading CSV records, and function array-type arguments.
3031
*
3132
* SYNOPSIS
3233
*

Validate/exceptions.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* Contains the ValidationException based classes.
44
*
55
* @author Craig Manley
6-
* @copyright Copyright © 2013, Craig Manley (www.craigmanley.com)
6+
* @copyright Copyright © 2016, Craig Manley (www.craigmanley.com)
77
* @license http://www.opensource.org/licenses/mit-license.php Licensed under MIT
8-
* @version $Id: exceptions.php,v 1.2 2013/12/14 00:17:42 cmanley Exp $
8+
* @version $Id: exceptions.php,v 1.3 2016/02/17 22:25:33 cmanley Exp $
99
* @package Validate
1010
*/
1111
namespace Validate;

0 commit comments

Comments
 (0)