10
10
* @author Craig Manley
11
11
* @copyright Copyright © 2016, Craig Manley (www.craigmanley.com)
12
12
* @license http://www.opensource.org/licenses/mit-license.php Licensed under MIT
13
- * @version $Id: Validation.php,v 1.2 2018/05/26 22:51:21 cmanley Exp $
13
+ * @version $Id: Validation.php,v 1.3 2018/05/26 22:55:49 cmanley Exp $
14
14
* @package Validate
15
15
*/
16
16
namespace Validate ;
31
31
*/
32
32
class Validation {
33
33
34
- // validations:
35
- protected $ allowed_values ; // array of scalars
36
- protected $ callbacks ; // associative array of key => callback pairs
34
+ # validations:
35
+ protected $ allowed_values ; # array of scalars
36
+ protected $ callbacks ; # associative array of key => callback pairs
37
37
protected $ callback ;
38
38
protected $ isa ;
39
39
protected $ mb_max_length ;
@@ -46,10 +46,10 @@ class Validation {
46
46
protected $ resource_type ;
47
47
protected $ types ;
48
48
49
- // options:
49
+ # options:
50
50
protected $ nocase ;
51
51
52
- // other:
52
+ # other:
53
53
protected $ last_failure ;
54
54
55
55
/**
@@ -85,7 +85,7 @@ class Validation {
85
85
public function __construct (array $ args = null ) {
86
86
if ($ args ) {
87
87
foreach ($ args as $ key => $ value ) {
88
- // Process validations:
88
+ # Process validations:
89
89
if ($ key == 'allowed_values ' ) {
90
90
if (!(is_array ($ value ) && count ($ value ))) {
91
91
throw new \InvalidArgumentException ("The \"$ key \" argument must be an array containing at least 1 value. " );
@@ -154,7 +154,7 @@ public function __construct(array $args = null) {
154
154
elseif ($ value == 'float ' ) {
155
155
$ value = 'double ' ;
156
156
}
157
- if (is_array ($ this ->types )) { // because 'types' was given
157
+ if (is_array ($ this ->types )) { # because 'types' was given
158
158
$ this ->types []= $ value ;
159
159
}
160
160
else {
@@ -171,16 +171,16 @@ public function __construct(array $args = null) {
171
171
}
172
172
/*
173
173
'boolean',
174
- 'integer', // Be careful with integers > 2147483647 (0x7FFFFFFF) or < -2147483648 (0x8000000) as these automatically become floats in PHP.
175
- 'double', // (for historical reasons "double" is returned in case of a float, and not simply "float")
174
+ 'integer', # Be careful with integers > 2147483647 (0x7FFFFFFF) or < -2147483648 (0x8000000) as these automatically become floats in PHP.
175
+ 'double', # (for historical reasons "double" is returned in case of a float, and not simply "float")
176
176
'string',
177
177
'array',
178
178
'object',
179
179
'resource',
180
180
'NULL',
181
181
'unknown type',
182
182
*/
183
- // Handle some common type aliases too:
183
+ # Handle some common type aliases too:
184
184
if ($ type == 'int ' ) {
185
185
$ type = 'integer ' ;
186
186
}
@@ -189,21 +189,21 @@ public function __construct(array $args = null) {
189
189
}
190
190
unset($ type );
191
191
}
192
- if (is_array ($ this ->$ key )) { // because 'type' was given
192
+ if (is_array ($ this ->$ key )) { # because 'type' was given
193
193
$ this ->$ key = array_merge ($ this ->$ key , $ value );
194
194
}
195
195
else {
196
196
$ this ->$ key = $ value ;
197
197
}
198
198
}
199
199
200
- // Process boolean options
200
+ # Process boolean options
201
201
elseif (in_array ($ key , array ('nocase ' ))) {
202
202
$ this ->$ key = (boolean ) $ value ;
203
203
}
204
204
205
205
elseif (substr ($ key ,0 ,1 ) === '_ ' ) {
206
- // Silently ignore options prefixed with underscore.
206
+ # Silently ignore options prefixed with underscore.
207
207
}
208
208
else {
209
209
throw new \InvalidArgumentException ("Unknown argument \"$ key \". " );
@@ -218,18 +218,18 @@ public function __construct(array $args = null) {
218
218
* All options passed into the constructor can be read using property accessors, e.g. print $validation->regex . "\n";
219
219
*/
220
220
public function __get ($ key ) {
221
- // TODO: perhaps replace this reflection code with some simple hash access code. See the comments below why.
221
+ # TODO: perhaps replace this reflection code with some simple hash access code. See the comments below why.
222
222
$ r = new \ReflectionObject ($ this );
223
223
$ p = null ;
224
224
try {
225
225
$ p = $ r ->getProperty ($ key );
226
226
}
227
227
catch (\ReflectionException $ e ) {
228
- // snuff unknown properties with exception message 'Property x does not exist'
228
+ # snuff unknown properties with exception message 'Property x does not exist'
229
229
}
230
230
if ($ p && ($ p ->isProtected () || $ p ->isPublic ()) && !$ p ->isStatic ()) {
231
- $ p ->setAccessible (true ); // Allow access to non-public members.
232
- return $ p ->getValue ($ this ); // This design breaks mirrors. Surely the reflection property should know what object was given to ReflectionObject.
231
+ $ p ->setAccessible (true ); # Allow access to non-public members.
232
+ return $ p ->getValue ($ this ); # This design breaks mirrors. Surely the reflection property should know what object was given to ReflectionObject.
233
233
}
234
234
throw new \BadMethodCallException ('Attempt to read undefined property ' . get_class ($ this ) . '-> ' . $ key );
235
235
}
0 commit comments