Skip to content

Commit

Permalink
Replace old // comment prefixes with #
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Manley committed May 26, 2018
1 parent e99c094 commit 8f6039e
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 84 deletions.
16 changes: 8 additions & 8 deletions eg/spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"',
Expand All @@ -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"',
Expand All @@ -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(
Expand All @@ -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");
}
Expand Down
18 changes: 9 additions & 9 deletions eg/specs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
* 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',
'mb_max_length' => 10,
'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,
Expand All @@ -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',
Expand All @@ -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(
Expand All @@ -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',
Expand All @@ -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");
}
Expand Down
6 changes: 3 additions & 3 deletions eg/validator_after.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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];
}
Expand Down
6 changes: 3 additions & 3 deletions eg/validator_before.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Specs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
36 changes: 18 additions & 18 deletions src/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -46,10 +46,10 @@ class Validation {
protected $resource_type;
protected $types;

// options:
# options:
protected $nocase;

// other:
# other:
protected $last_failure;

/**
Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -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 {
Expand All @@ -171,16 +171,16 @@ 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',
'resource',
'NULL',
'unknown type',
*/
// Handle some common type aliases too:
# Handle some common type aliases too:
if ($type == 'int') {
$type = 'integer';
}
Expand All @@ -189,21 +189,21 @@ 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 {
$this->$key = $value;
}
}

// 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\".");
Expand All @@ -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);
}
Expand Down
Loading

0 comments on commit 8f6039e

Please sign in to comment.