diff --git a/eg/spec.php b/eg/spec.php index f68014d..bf49c70 100644 --- a/eg/spec.php +++ b/eg/spec.php @@ -6,16 +6,16 @@ * The Spec class is rarely used stand-alone as it is only able to validate a single value. * * @author Craig Manley -* @version $Id: spec.php,v 1.3 2016/06/13 20:04:08 cmanley Exp $ +* @version $Id: spec.php,v 1.4 2018/05/26 22:55:49 cmanley Exp $ * @package Validate */ require_once(__DIR__ . '/../src/Spec.php'); -// A Spec object can be created in 3 possible ways, all having the same effect. +# A Spec object can be created in 3 possible ways, all having the same effect. $specs = array(); -// This is the proper way to create a Spec object with it's embedded Validation object. +# This is the proper way to create a Spec object with it's embedded Validation object. $specs []= new Validate\Spec(array( 'allow_empty' => false, 'description' => 'String with a lowercase "a"', @@ -30,7 +30,7 @@ )); -// This is the lazy way to create a Spec object. It'll automatically convert the 'validation' array into a Validation object internally. +# This is the lazy way to create a Spec object. It'll automatically convert the 'validation' array into a Validation object internally. $specs []= new Validate\Spec(array( 'allow_empty' => false, 'description' => 'String with a lowercase "a"', @@ -45,14 +45,14 @@ )); -// This is the very lazy way to create a Spec object. It'll automatically create a Validation object internally. +# This is the very lazy way to create a Spec object. It'll automatically create a Validation object internally. $specs []= new Validate\Spec(array( - // Spec options: + # Spec options: 'allow_empty' => false, 'description' => 'String with a lowercase "a"', 'optional' => false, - // Validation options: + # Validation options: 'mb_max_length' => 10, 'regex' => '/a/', 'callbacks' => array( @@ -61,7 +61,7 @@ )); -// Check if all the Spec objects are indeed identical: +# Check if all the Spec objects are indeed identical: if (count(array_unique(array_map(function($spec) { return var_export($spec,true); }, $specs))) != 1) { die("The Spec objects are not identical!\n"); } diff --git a/eg/specs.php b/eg/specs.php index 32d2ab1..706b90b 100644 --- a/eg/specs.php +++ b/eg/specs.php @@ -5,17 +5,17 @@ * Specs objects can be used as arrays since they implement the Countable, IteratorAggregate, and ArrayAccess interfaces. * * @author Craig Manley -* @version $Id: specs.php,v 1.3 2016/06/13 20:04:08 cmanley Exp $ +* @version $Id: specs.php,v 1.4 2018/05/26 22:55:49 cmanley Exp $ * @package Validate */ require_once(__DIR__ . '/../src/Specs.php'); -// A Specs object can be created in 3 possible ways, all having the same effect. -// The contructor is given an associative array of name => Spec pairs +# A Specs object can be created in 3 possible ways, all having the same effect. +# The contructor is given an associative array of name => Spec pairs -// This is the easy/lazy and my preferred way to create a Specs object with it's embedded Spec objects. +# This is the easy/lazy and my preferred way to create a Specs object with it's embedded Spec objects. $specs_easy = new Validate\Specs(array( 'firstname' => array( 'description' => 'First name', @@ -23,7 +23,7 @@ 'regex' => '/^[A-Z][a-z]+$/', 'type' => 'string', ), - 'surname' => 1, // shortcut for Spec with 'optional' => !value + 'surname' => 1, # shortcut for Spec with 'optional' => !value 'age' => array( 'optional' => true, 'mb_max_length' => 3, @@ -33,7 +33,7 @@ )); -// This is the less lazy way to create a Specs object with it's embedded Spec objects. +# This is the less lazy way to create a Specs object with it's embedded Spec objects. $specs_lazy = new Validate\Specs(array( 'firstname' => (new Validate\Spec(array( 'description' => 'First name', @@ -43,7 +43,7 @@ 'types' => array('string'), ), ))), - 'surname' => true, // shortcut for Spec with 'optional' => !value + 'surname' => true, # shortcut for Spec with 'optional' => !value 'age' => (new Validate\Spec(array( 'optional' => true, 'validation' => array( @@ -55,7 +55,7 @@ )); -// This is the proper and most verbose way to create a Specs object with it's embedded Spec objects. +# This is the proper and most verbose way to create a Specs object with it's embedded Spec objects. $specs_proper = new Validate\Specs(array( 'firstname' => (new Validate\Spec(array( 'description' => 'First name', @@ -82,7 +82,7 @@ -// Check if all the Spec objects are indeed identical: +# Check if all the Spec objects are indeed identical: if (count(array_unique(array_map(function($specs) { return var_export($specs,true); }, array($specs_easy, $specs_lazy, $specs_proper) ))) != 1) { die("The Specs objects are not identical!\n"); } diff --git a/eg/validator_after.php b/eg/validator_after.php index 39c2069..fb92095 100644 --- a/eg/validator_after.php +++ b/eg/validator_after.php @@ -4,7 +4,7 @@ * The 'after' function is executed after other spec validations have been performed. * * @author Craig Manley -* @version $Id: validator_after.php,v 1.3 2016/06/13 20:04:08 cmanley Exp $ +* @version $Id: validator_after.php,v 1.4 2018/05/26 22:55:49 cmanley Exp $ * @package Validate */ require_once(__DIR__ . '/../src/Validator.php'); @@ -19,8 +19,8 @@ ), 'birthdate' => array( 'type' => 'string', - 'regex' => '#^[0-3]\d/[01]\d/\d{4}$#', // expect dd/mm/yyyy - 'after' => function(&$value) { // want yyyy-mm-dd + 'regex' => '#^[0-3]\d/[01]\d/\d{4}$#', # expect dd/mm/yyyy + 'after' => function(&$value) { # want yyyy-mm-dd if (is_string($value) && preg_match('#^(\d{2})/(\d{2})/(\d{4})$#', $value, $matches)) { $value = $matches[3] . '-' . $matches[2] . '-' . $matches[1]; } diff --git a/eg/validator_before.php b/eg/validator_before.php index 44d55c6..3f88579 100644 --- a/eg/validator_before.php +++ b/eg/validator_before.php @@ -4,7 +4,7 @@ * The 'before' function is executed before other spec validations have been performed. * * @author Craig Manley -* @version $Id: validator_before.php,v 1.3 2016/06/13 20:04:08 cmanley Exp $ +* @version $Id: validator_before.php,v 1.4 2018/05/26 22:55:49 cmanley Exp $ * @package Validate */ require_once(__DIR__ . '/../src/Validator.php'); @@ -19,8 +19,8 @@ ), 'birthdate' => array( 'type' => 'string', - 'regex' => '/^\d{4}-[01]\d-[0-3]\d$/', // should be yyyy-mm-dd - 'before' => function(&$value) { // expect dd/mm/yyyy + 'regex' => '/^\d{4}-[01]\d-[0-3]\d$/', # should be yyyy-mm-dd + 'before' => function(&$value) { # expect dd/mm/yyyy if (is_string($value) && preg_match('#^([0-3]\d)/([01]\d)/(\d{4})$#', $value, $matches)) { $value = $matches[3] . '-' . $matches[2] . '-' . $matches[1]; } diff --git a/src/Specs.php b/src/Specs.php index 99142ed..7a62c20 100644 --- a/src/Specs.php +++ b/src/Specs.php @@ -10,7 +10,7 @@ * @author Craig Manley * @copyright Copyright © 2016, Craig Manley (www.craigmanley.com) * @license http://www.opensource.org/licenses/mit-license.php Licensed under MIT -* @version $Id: Specs.php,v 1.3 2018/05/26 22:51:21 cmanley Exp $ +* @version $Id: Specs.php,v 1.4 2018/05/26 22:55:49 cmanley Exp $ * @package cmanley */ namespace Validate; @@ -29,7 +29,7 @@ */ class Specs implements \Countable, \IteratorAggregate, \ArrayAccess { - private $pairs; // map of name => Spec pairs + private $pairs; # map of name => Spec pairs static private $boolean_spec_true; static private $boolean_spec_false; diff --git a/src/Validation.php b/src/Validation.php index 39b96a3..7e0727f 100644 --- a/src/Validation.php +++ b/src/Validation.php @@ -10,7 +10,7 @@ * @author Craig Manley * @copyright Copyright © 2016, Craig Manley (www.craigmanley.com) * @license http://www.opensource.org/licenses/mit-license.php Licensed under MIT -* @version $Id: Validation.php,v 1.2 2018/05/26 22:51:21 cmanley Exp $ +* @version $Id: Validation.php,v 1.3 2018/05/26 22:55:49 cmanley Exp $ * @package Validate */ namespace Validate; @@ -31,9 +31,9 @@ */ class Validation { - // validations: - protected $allowed_values; // array of scalars - protected $callbacks; // associative array of key => callback pairs + # validations: + protected $allowed_values; # array of scalars + protected $callbacks; # associative array of key => callback pairs protected $callback; protected $isa; protected $mb_max_length; @@ -46,10 +46,10 @@ class Validation { protected $resource_type; protected $types; - // options: + # options: protected $nocase; - // other: + # other: protected $last_failure; /** @@ -85,7 +85,7 @@ class Validation { public function __construct(array $args = null) { if ($args) { foreach ($args as $key => $value) { - // Process validations: + # Process validations: if ($key == 'allowed_values') { if (!(is_array($value) && count($value))) { 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) { elseif ($value == 'float') { $value = 'double'; } - if (is_array($this->types)) { // because 'types' was given + if (is_array($this->types)) { # because 'types' was given $this->types []= $value; } else { @@ -171,8 +171,8 @@ public function __construct(array $args = null) { } /* 'boolean', - 'integer', // Be careful with integers > 2147483647 (0x7FFFFFFF) or < -2147483648 (0x8000000) as these automatically become floats in PHP. - 'double', // (for historical reasons "double" is returned in case of a float, and not simply "float") + 'integer', # Be careful with integers > 2147483647 (0x7FFFFFFF) or < -2147483648 (0x8000000) as these automatically become floats in PHP. + 'double', # (for historical reasons "double" is returned in case of a float, and not simply "float") 'string', 'array', 'object', @@ -180,7 +180,7 @@ public function __construct(array $args = null) { 'NULL', 'unknown type', */ - // Handle some common type aliases too: + # Handle some common type aliases too: if ($type == 'int') { $type = 'integer'; } @@ -189,7 +189,7 @@ public function __construct(array $args = null) { } unset($type); } - if (is_array($this->$key)) { // because 'type' was given + if (is_array($this->$key)) { # because 'type' was given $this->$key = array_merge($this->$key, $value); } else { @@ -197,13 +197,13 @@ public function __construct(array $args = null) { } } - // Process boolean options + # Process boolean options elseif (in_array($key, array('nocase'))) { $this->$key = (boolean) $value; } elseif (substr($key,0,1) === '_') { - // Silently ignore options prefixed with underscore. + # Silently ignore options prefixed with underscore. } else { throw new \InvalidArgumentException("Unknown argument \"$key\"."); @@ -218,18 +218,18 @@ public function __construct(array $args = null) { * All options passed into the constructor can be read using property accessors, e.g. print $validation->regex . "\n"; */ public function __get($key) { - // TODO: perhaps replace this reflection code with some simple hash access code. See the comments below why. + # TODO: perhaps replace this reflection code with some simple hash access code. See the comments below why. $r = new \ReflectionObject($this); $p = null; try { $p = $r->getProperty($key); } catch (\ReflectionException $e) { - // snuff unknown properties with exception message 'Property x does not exist' + # snuff unknown properties with exception message 'Property x does not exist' } if ($p && ($p->isProtected() || $p->isPublic()) && !$p->isStatic()) { - $p->setAccessible(true); // Allow access to non-public members. - return $p->getValue($this); // This design breaks mirrors. Surely the reflection property should know what object was given to ReflectionObject. + $p->setAccessible(true); # Allow access to non-public members. + return $p->getValue($this); # This design breaks mirrors. Surely the reflection property should know what object was given to ReflectionObject. } throw new \BadMethodCallException('Attempt to read undefined property ' . get_class($this) . '->' . $key); } diff --git a/src/Validator.php b/src/Validator.php index 7709198..475a409 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -11,7 +11,7 @@ * @author Craig Manley * @copyright Copyright © 2016, Craig Manley (www.craigmanley.com) * @license http://www.opensource.org/licenses/mit-license.php Licensed under MIT -* @version $Id: Validator.php,v 1.3 2018/05/26 22:51:21 cmanley Exp $ +* @version $Id: Validator.php,v 1.4 2018/05/26 22:55:49 cmanley Exp $ * @package Validate */ namespace Validate; @@ -42,8 +42,8 @@ * ), * 'birthdate' => array( * 'type' => 'string', -* 'regex' => '#^[0-3]\d/[01]\d/\d{4}$#', // expect dd/mm/yyyy -* 'after' => function(&$value) { // want yyyy-mm-dd +* 'regex' => '#^[0-3]\d/[01]\d/\d{4}$#', # expect dd/mm/yyyy +* 'after' => function(&$value) { # want yyyy-mm-dd * if (is_string($value) && preg_match('#^(\d{2})/(\d{2})/(\d{4})$#', $value, $matches)) { * $value = $matches[3] . '-' . $matches[2] . '-' . $matches[1]; * } @@ -94,7 +94,7 @@ class Validator { protected $allow_extra = null; - protected $empty_delete = true; // odd one out - normally defaults are false + protected $empty_delete = true; # odd one out - normally defaults are false protected $empty_null = null; protected $prefix = ''; protected $remove_extra = false; @@ -137,12 +137,12 @@ public function __construct(array $args = null) { } $this->$k = $v; } - // Process boolean options + # Process boolean options elseif (in_array($k, array('allow_extra', 'empty_delete', 'empty_null', 'remove_extra'))) { $this->$k = (boolean) $v; } elseif (substr($key,0,1) === '_') { - // Silently ignore options prefixed with underscore. + # Silently ignore options prefixed with underscore. } else { throw new \InvalidArgumentException("Unhandled option '$k'"); @@ -157,17 +157,17 @@ public function __construct(array $args = null) { * All options passed into the constructor can be read using property accessors, e.g. print $spec->optional . "\n"; */ public function __get($key) { - // TODO: perhaps replace this reflection code with some simple hash access code. See the comments below why. + # TODO: perhaps replace this reflection code with some simple hash access code. See the comments below why. $r = new \ReflectionObject($this); $p = null; try { $p = $r->getProperty($key); } catch (\ReflectionException $e) { - // snuff unknown properties with exception message 'Property x does not exist' + # snuff unknown properties with exception message 'Property x does not exist' } if ($p && ($p->isProtected() || $p->isPublic()) && !$p->isStatic()) { - $p->setAccessible(true); // Allow access to non-public members. + $p->setAccessible(true); # Allow access to non-public members. return $p->getValue($this); } throw new \BadMethodCallException('Attempt to read undefined property ' . get_class($this) . '->' . $key); @@ -193,11 +193,11 @@ public function specs() { * @throws ValidationNamedCheckException */ public function validate(array $args) { - // Make sure keys in $args exist that have default values + # Make sure keys in $args exist that have default values $specs = $this->specs(); if ($specs) { foreach ($specs as $k => $spec) { - //if (!array_key_exists($k, $args) && !is_null($specs[$k]->getDefault())) { + #if (!array_key_exists($k, $args) && !is_null($specs[$k]->getDefault())) { if (!array_key_exists($k, $args)) { $args[$k] = null; } @@ -206,7 +206,7 @@ public function validate(array $args) { if ($this->empty_delete) { foreach ($args as $k => $v) { if (is_null($v) || (is_string($v) && !strlen($v))) { - if (!($specs && $specs->offsetExists($k) && !is_null($specs[$k]->getDefault()))) { // don't delete arguments that have default values + if (!($specs && $specs->offsetExists($k) && !is_null($specs[$k]->getDefault()))) { # don't delete arguments that have default values unset($args[$k]); } } @@ -221,15 +221,15 @@ public function validate(array $args) { } } - // If no spec exists, then return unvalidated arguments. + # If no spec exists, then return unvalidated arguments. if (!$specs) { return $args; } - // Validate args + # Validate args foreach ($args as $k => &$v) { if ($specs->offsetExists($k)) { - // nop + # nop } elseif ($this->remove_extra) { unset($args[$k]); @@ -244,12 +244,12 @@ public function validate(array $args) { if (array_key_exists($k, $args)) { $v =& $args[$k]; } - if (!$spec->validate($v)) { // also applies defaults or before/after mutators to $v reference + if (!$spec->validate($v)) { # also applies defaults or before/after mutators to $v reference throw new ValidationNamedCheckException($this->prefix . $k, $spec->getLastFailure(), $v); } unset($v); } - return $args; // same as input if no befores were applied + return $args; # same as input if no befores were applied } @@ -265,22 +265,22 @@ public function validate(array $args) { public function validate_pos(array $args) { $specs = $this->specs(); if ($specs) { - $specs = array_values($this->specs()->toArray()); // make sure that specs is a sequential numerically indexed array. + $specs = array_values($this->specs()->toArray()); # make sure that specs is a sequential numerically indexed array. } - $args = array_values($args); // this make sure that args is a sequential numerically indexed array. + $args = array_values($args); # this make sure that args is a sequential numerically indexed array. foreach ($args as $k => &$v) { if ($this->empty_null && is_string($v) && !strlen($v)) { $v = null; } - $spec = $specs && (is_array($specs) ? array_key_exists($k, $specs) : $specs->offsetExists($k)) ? $specs[$k] : null; // array_key_exists does not work with ArrayAccess objects yet. Perhaps in the future it will. + $spec = $specs && (is_array($specs) ? array_key_exists($k, $specs) : $specs->offsetExists($k)) ? $specs[$k] : null; # array_key_exists does not work with ArrayAccess objects yet. Perhaps in the future it will. if (!$spec) { - // note: remove_extra doesn't apply to positional arrays + # note: remove_extra doesn't apply to positional arrays if (!$this->allow_extra) { throw new ValidationException('Unexpected parameter at index ' . $this->prefix . $k); } continue; } - if (!$spec->validate($v)) { // also applies before/after mutators to $v reference + if (!$spec->validate($v)) { # also applies before/after mutators to $v reference throw new ValidationNamedCheckException($this->prefix . $k, $spec->getLastFailure(), $v); } unset($v); diff --git a/t/Specs.t.php b/t/Specs.t.php index b1002cf..0f5c570 100644 --- a/t/Specs.t.php +++ b/t/Specs.t.php @@ -25,18 +25,18 @@ public function testClassExists() { public function testMethodsExist() { $class = static::CLASS_NAME; $methods = array( - // public + # public '__construct', 'toArray', 'keys', - // for Countable interface + # for Countable interface 'count', - // for IteratorAggregate interface + # for IteratorAggregate interface 'getIterator', - // for ArrayAccess interface + # for ArrayAccess interface 'offsetSet', 'offsetExists', 'offsetUnset', @@ -49,7 +49,7 @@ public function testMethodsExist() { public function testInterfaces() { $class = static::CLASS_NAME; - $expect = array('ArrayAccess', 'Countable', 'IteratorAggregate', 'Traversable'); // Traversable is part of IteratorAggregate + $expect = array('ArrayAccess', 'Countable', 'IteratorAggregate', 'Traversable'); # Traversable is part of IteratorAggregate $got = class_implements($class, false); sort($got); $this->assertEquals($expect, $got, "$class implements the interfaces " . join(', ', $expect)); @@ -66,7 +66,7 @@ public function testCreate() { 'regex' => '/^[A-Z][a-z]+$/', 'type' => 'string', ), - 'surname' => 1, // shortcut for Spec with 'optional' => !value + 'surname' => 1, # shortcut for Spec with 'optional' => !value 'age' => array( 'optional' => true, 'mb_max_length' => 3, @@ -138,7 +138,7 @@ public function testValidate() { 'regex' => '/^[A-Z][a-z]+$/', 'type' => 'string', ), - 'surname' => 1, // shortcut for Spec with 'optional' => !value + 'surname' => 1, # shortcut for Spec with 'optional' => !value 'age' => array( 'optional' => true, 'mb_max_length' => 3, diff --git a/t/Validation.t.php b/t/Validation.t.php index 9e63309..192abe8 100644 --- a/t/Validation.t.php +++ b/t/Validation.t.php @@ -25,7 +25,7 @@ public function testClassExists() { public function testMethodsExist() { $class = static::CLASS_NAME; $methods = array( - // public + # public '__construct', '__get', 'getLastFailure', @@ -42,11 +42,11 @@ public function testCreate() { $o = new $class(); $this->assertTrue(is_object($o), 'Create empty object.'); $o = new $class(array( - // Some of these validations contradict each other, but it's merely a test + # Some of these validations contradict each other, but it's merely a test 'allowed_values' => array('one@two.com', 'two@three.com'), - 'callbacks' => array( // associative array of key => callback pairs + 'callbacks' => array( # associative array of key => callback pairs 'syntax' => function($x) { return filter_var($x, FILTER_VALIDATE_EMAIL); }, - 'mx' => function($x) { return true; }, // dummy check + 'mx' => function($x) { return true; }, # dummy check ), 'callback' => function($x) { return filter_var($x, FILTER_VALIDATE_EMAIL); }, 'isa' => 'StdClass', diff --git a/t/Validator.t.php b/t/Validator.t.php index 8d8f089..f8ef06a 100644 --- a/t/Validator.t.php +++ b/t/Validator.t.php @@ -25,7 +25,7 @@ public function testClassExists() { public function testMethodsExist() { $class = static::CLASS_NAME; $methods = array( - // public + # public '__construct', '__get', 'specs', @@ -50,8 +50,8 @@ public function testCreate() { ), 'birthdate' => array( 'type' => 'string', - 'regex' => '/^\d{4}-[01]\d-[0-3]\d$/', // should be yyyy-mm-dd - 'before' => function(&$value) { // expect dd/mm/yyyy + 'regex' => '/^\d{4}-[01]\d-[0-3]\d$/', # should be yyyy-mm-dd + 'before' => function(&$value) { # expect dd/mm/yyyy if (is_string($value) && preg_match('#^([0-3]\d)/([01]\d)/(\d{4})$#', $value, $matches)) { $value = $matches[3] . '-' . $matches[2] . '-' . $matches[1]; } @@ -69,7 +69,7 @@ public function testCreate() { 'allow_extra' => false, 'empty_delete' => false, 'empty_null' => true, - //'prefix' + #'prefix' 'remove_extra' => true, 'specs' => $specs, )); @@ -91,8 +91,8 @@ public function testValidate() { ), 'birthdate' => array( 'type' => 'string', - 'regex' => '/^\d{4}-[01]\d-[0-3]\d$/', // should be yyyy-mm-dd - 'before' => function(&$value) { // expect dd/mm/yyyy + 'regex' => '/^\d{4}-[01]\d-[0-3]\d$/', # should be yyyy-mm-dd + 'before' => function(&$value) { # expect dd/mm/yyyy if (is_string($value) && preg_match('#^([0-3]\d)/([01]\d)/(\d{4})$#', $value, $matches)) { $value = $matches[3] . '-' . $matches[2] . '-' . $matches[1]; } @@ -109,7 +109,7 @@ public function testValidate() { 'allow_extra' => false, 'empty_delete' => false, 'empty_null' => true, - //'prefix' + #'prefix' 'remove_extra' => true, 'specs' => $specs, )); @@ -144,7 +144,7 @@ public function testValidate() { 'input' => array( 'name' => 'Mike', 'birthdate' => '01/01/2000', - 'score' => 'high', // not allowed + 'score' => 'high', # not allowed ), 'expect' => null, 'expect_exception' => 'Parameter "score" validation check "types" failed for string value "high"',