Skip to content

Commit e8b45b2

Browse files
author
Craig Manley
committed
Breaking: Split exceptions.php into separate files and renamed exceptions.
Breaking: Rename Specs to SpecCollection and make it immutable. Better null handling. Spec: deprecate accessor methods; use read-only property accessors instead.
1 parent 568f63e commit e8b45b2

16 files changed

+462
-242
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ $error = null;
6060
try {
6161
save_student_score($_POST);
6262
}
63-
catch (Validate\ValidationException $e) {
63+
catch (Validate\Exception\ValidationException $e) {
6464
$error = $e->getMessage();
6565
}
6666
if ($error) {

TODO

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
Put exceptions in their own files and own namespace subdir.
2-
Rename Specs to SpecCollection and make it immutable.
3-
Perhaps rename or move Validation into a subdir of Spec so that it is more obvious that it is a component of Spec.
1+
Spec: perhaps replace allow_empty and optional with allow_empty_strings and allow_null
2+
Spec: remove accessor methods since the __get attribute accessor is enough.
3+
Perhaps rename Validation to Constraints or SpecConstraints.
4+
In progress: Put exceptions in their own files and own namespace subdir.
45
Create proper unit tests according to conventions.
5-
See other TODOs in classes.
66
Check and improve markdown documentation and see it if can be generated.
77
Improve examples.

eg/specs.php

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<?php
22
/**
3-
* Example class that demonstrates the use of the Specs class.
4-
* The Specs class is simply a collection of Spec objects.
5-
* Specs objects can be used as arrays since they implement the Countable, IteratorAggregate, and ArrayAccess interfaces.
3+
* Example class that demonstrates the use of the SpecCollection class.
4+
* The SpecCollection class is simply a collection of Spec objects.
5+
* SpecCollection objects can be used as arrays since they implement the Countable, IteratorAggregate, and ArrayAccess interfaces.
66
*/
7-
require_once(__DIR__ . '/../src/Specs.php');
7+
require_once(__DIR__ . '/../src/SpecCollection.php');
88

99

10-
# A Specs object can be created in 3 possible ways, all having the same effect.
10+
# A SpecCollection object can be created in 3 possible ways, all having the same effect.
1111
# The contructor is given an associative array of name => Spec pairs
1212

1313

14-
# This is the easy/lazy and my preferred way to create a Specs object with it's embedded Spec objects.
15-
$specs_easy = new Validate\Specs(array(
14+
# This is the easy/lazy and my preferred way to create a SpecCollection object with it's embedded Spec objects.
15+
$specs_easy = new Validate\SpecCollection(array(
1616
'firstname' => array(
1717
'description' => 'First name',
1818
'mb_max_length' => 10,
@@ -29,8 +29,8 @@
2929
));
3030

3131

32-
# This is the less lazy way to create a Specs object with it's embedded Spec objects.
33-
$specs_lazy = new Validate\Specs(array(
32+
# This is the less lazy way to create a SpecCollection object with it's embedded Spec objects.
33+
$specs_lazy = new Validate\SpecCollection(array(
3434
'firstname' => (new Validate\Spec(array(
3535
'description' => 'First name',
3636
'validation' => array(
@@ -51,8 +51,8 @@
5151
));
5252

5353

54-
# This is the proper and most verbose way to create a Specs object with it's embedded Spec objects.
55-
$specs_proper = new Validate\Specs(array(
54+
# This is the proper and most verbose way to create a SpecCollection object with it's embedded Spec objects.
55+
$specs_proper = new Validate\SpecCollection(array(
5656
'firstname' => (new Validate\Spec(array(
5757
'description' => 'First name',
5858
'validation' => (new Validate\Validation(array(
@@ -80,7 +80,7 @@
8080

8181
# Check if all the Spec objects are indeed identical:
8282
if (count(array_unique(array_map(function($specs) { return var_export($specs,true); }, array($specs_easy, $specs_lazy, $specs_proper) ))) != 1) {
83-
die("The Specs objects are not identical!\n");
83+
die("The SpecCollection objects are not identical!\n");
8484
}
8585

8686

@@ -142,6 +142,12 @@
142142

143143

144144
$verbose = true;
145+
146+
foreach ($specs as $k => &$spec) {
147+
$spec = new Validate\Spec(['type' => 'float']); # does nothing to $specs as the iterator works on a copy
148+
unset($spec);
149+
}
150+
145151
foreach ($tests as $name => $test) {
146152
$input = $test['input'];
147153
$verbose && print "Test: $name\n";

eg/validator_after.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
$record = $validator->validate($record);
6161
print 'After validation: ' . print_r($record,true);
6262
}
63-
catch (Validate\ValidationException $e) {
63+
catch (Validate\Exception\ValidationException $e) {
6464
print 'Error at record ' . $i . ': ' . $e->getMessage() . "\n";
6565
continue;
6666
}

eg/validator_before.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
$record = $validator->validate($record);
6161
print 'After validation: ' . print_r($record,true);
6262
}
63-
catch (Validate\ValidationException $e) {
63+
catch (Validate\Exception\ValidationException $e) {
6464
print 'Error at record ' . $i . ': ' . $e->getMessage() . "\n";
6565
continue;
6666
}

src/Exception/NamedValueException.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Contains the Validate\Exception\NamedValueException class.
4+
*
5+
* @author Craig Manley
6+
* @copyright Copyright © 2018, Craig Manley (www.craigmanley.com)
7+
* @license http://www.opensource.org/licenses/mit-license.php Licensed under MIT
8+
*/
9+
namespace Validate\Exception;
10+
11+
12+
/**
13+
* @ignore Require dependencies.
14+
*/
15+
require_once(__DIR__ . '/ValueException.php');
16+
17+
18+
/**
19+
* NamedValueException class.
20+
* Extends ValueException by also including the name of the parameter/key of the value that failed validation.
21+
*
22+
* @package cmanley
23+
*/
24+
class NamedValueException extends ValueException {
25+
protected $name;
26+
27+
/**
28+
* Constructor.
29+
*
30+
* @param string $name the parameter/key name
31+
* @param string $check name of check that failed
32+
* @param mixed $value the value that isn't valid
33+
* @param string $message optional custom message
34+
* @param array $options
35+
*/
36+
public function __construct($name, $check, $value, $message = null) {
37+
$this->name = $name;
38+
$this->check = $check;
39+
$this->value = $value;
40+
if (is_null($message)) {
41+
$message = 'Parameter "' . $name . '" failed validation "' . $check . '" for ' . gettype($this->value) . ' value';
42+
if (is_scalar($value)) {
43+
$message .= ' ' . $this->getStringPlaceholderValue();
44+
}
45+
}
46+
parent::__construct($check, $value, $message);
47+
}
48+
49+
50+
/**
51+
* Return the name of the key, if known, that caused the failure.
52+
*
53+
* @return string|null
54+
*/
55+
public function getName() {
56+
return $this->name;
57+
}
58+
}

src/Exception/ValidationException.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Contains the Validate\Exception\ValidationException class.
4+
*
5+
* @author Craig Manley
6+
* @copyright Copyright © 2018, Craig Manley (www.craigmanley.com)
7+
* @license http://www.opensource.org/licenses/mit-license.php Licensed under MIT
8+
*/
9+
namespace Validate\Exception;
10+
11+
12+
/**
13+
* Base class of all exceptions in this namespace.
14+
*/
15+
class ValidationException extends \Exception {}
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
<?php
22
/**
3-
* Contains the ValidationException based classes.
3+
* Contains the Validate\Exception\ValueException class.
44
*
55
* @author Craig Manley
6-
* @copyright Copyright © 2016, Craig Manley (www.craigmanley.com)
6+
* @copyright Copyright © 2018, Craig Manley (www.craigmanley.com)
77
* @license http://www.opensource.org/licenses/mit-license.php Licensed under MIT
88
*/
9-
namespace Validate;
9+
namespace Validate\Exception;
1010

1111

1212
/**
13-
* ValidationException class
14-
*
15-
* @package Validate
13+
* @ignore Require dependencies.
1614
*/
17-
class ValidationException extends \Exception {}
18-
19-
15+
require_once(__DIR__ . '/ValidationException.php');
2016

2117

2218
/**
23-
* ValidationCheckException class.
19+
* ValueException class. Encapsulates the check name, value, and message of the failed validation.
2420
*/
25-
class ValidationCheckException extends ValidationException {
21+
class ValueException extends ValidationException {
2622
protected $check;
2723
protected $value;
2824

25+
2926
/**
3027
* Constructor.
3128
*
@@ -38,14 +35,15 @@ public function __construct($check, $value, $message = null) {
3835
$this->check = $check;
3936
$this->value = $value;
4037
if (is_null($message)) {
41-
$message = 'Failed validation check "' . $check . '" for ' . gettype($this->value) . ' value';
38+
$message = 'Failed validation "' . $check . '" for ' . gettype($this->value) . ' value';
4239
if (is_scalar($value)) {
4340
$message .= ' ' . $this->getStringPlaceholderValue();
4441
}
4542
}
4643
parent::__construct($message);
4744
}
4845

46+
4947
/**
5048
* Return the name of the check that failed.
5149
*
@@ -55,6 +53,7 @@ public function getCheck() {
5553
return $this->check;
5654
}
5755

56+
5857
/**
5958
* Return a string representation of the scalar value that caused the failure, for use in error messages.
6059
* If the value was not a scalar, then null is returned.
@@ -76,6 +75,7 @@ public function getStringPlaceholderValue() {
7675
return null;
7776
}
7877

78+
7979
/**
8080
* Return the value that caused the failure.
8181
*
@@ -85,6 +85,7 @@ public function getValue() {
8585
return $this->value;
8686
}
8787

88+
8889
/**
8990
* Return a simplified string representation of the value that caused the failure.
9091
*
@@ -100,48 +101,3 @@ public function getValueSimple() {
100101
return gettype($this->value);
101102
}
102103
}
103-
104-
105-
106-
107-
108-
/**
109-
* ValidationNamedCheckException class.
110-
*
111-
* @package cmanley
112-
*/
113-
class ValidationNamedCheckException extends ValidationCheckException {
114-
protected $name;
115-
116-
/**
117-
* Constructor.
118-
*
119-
* @param string $name the parameter/key name
120-
* @param string $check name of check that failed
121-
* @param mixed $value the value that isn't valid
122-
* @param string $message optional custom message
123-
* @param array $options
124-
*/
125-
public function __construct($name, $check, $value, $message = null) {
126-
$this->name = $name;
127-
$this->check = $check;
128-
$this->value = $value;
129-
if (is_null($message)) {
130-
$message = 'Parameter "' . $name . '" failed validation check "' . $check . '" for ' . gettype($this->value) . ' value';
131-
if (is_scalar($value)) {
132-
$message .= ' ' . $this->getStringPlaceholderValue();
133-
}
134-
}
135-
parent::__construct($check, $value, $message);
136-
}
137-
138-
139-
/**
140-
* Return the name of the key, if known, that caused the failure.
141-
*
142-
* @return string|null
143-
*/
144-
public function getName() {
145-
return $this->name;
146-
}
147-
}

0 commit comments

Comments
 (0)