Skip to content

Commit 8a60a7b

Browse files
committed
Add compat with DV Interfaces 1.x
1 parent cdb0301 commit 8a60a7b

File tree

8 files changed

+237
-12
lines changed

8 files changed

+237
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ for the [Maps](https://github.com/JeroenDeDauw/Maps) and
5454
### 1.0.0 (2021-01-22)
5555

5656
* Added compatibility with `data-values/data-values` 3.x
57+
* Added compatibility with `data-values/interfaces` 1.x
5758

5859
### 0.1.3 (2018-08-02)
5960

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"require": {
2929
"php": ">=7.2",
3030
"data-values/data-values": "^3.0.0|^2.1.1|~1.0|~0.1",
31-
"data-values/interfaces": "~0.2.0|~0.1.0"
31+
"data-values/interfaces": "^1.0.0|~0.2.0|~0.1.0"
3232
},
3333
"require-dev": {
3434
"phpunit/phpunit": "^8.5",

src/DimensionValidator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ValueValidators;
44

55
use Exception;
6+
use ValueValidators\PackagePrivate\ValueValidatorBase;
67

78
/**
89
* ValueValidator that validates a dimension value.
@@ -12,7 +13,7 @@
1213
* @license GPL-2.0-or-later
1314
* @author Jeroen De Dauw < [email protected] >
1415
*/
15-
class DimensionValidator extends ValueValidatorObject {
16+
class DimensionValidator extends ValueValidatorBase {
1617

1718
/**
1819
* @since 0.1
@@ -103,7 +104,7 @@ public function setRange( $lowerBound, $upperBound ) {
103104
}
104105

105106
/**
106-
* @see ValueValidatorObject::doValidation
107+
* @see ValueValidatorBase::doValidation
107108
*
108109
* @since 0.1
109110
*

src/ListValidator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ValueValidators;
44

55
use Exception;
6+
use ValueValidators\PackagePrivate\ValueValidatorBase;
67

78
/**
89
* ValueValidator that validates a list of values.
@@ -13,10 +14,10 @@
1314
* @author Jeroen De Dauw < [email protected] >
1415
* @author Thiemo Kreuz
1516
*/
16-
class ListValidator extends ValueValidatorObject {
17+
class ListValidator extends ValueValidatorBase {
1718

1819
/**
19-
* @see ValueValidatorObject::doValidation
20+
* @see ValueValidatorBase::doValidation
2021
*
2122
* @since 0.1
2223
*
@@ -50,7 +51,7 @@ public function doValidation( $value ) {
5051
}
5152

5253
/**
53-
* @see ValueValidatorObject::enableWhitelistRestrictions
54+
* @see ValueValidatorBase::enableWhitelistRestrictions
5455
*
5556
* @since 0.1
5657
*
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
<?php
2+
3+
namespace ValueValidators\PackagePrivate;
4+
5+
use ValueValidators\Error;
6+
use ValueValidators\Result;
7+
use ValueValidators\ValueValidator;
8+
9+
/**
10+
* ValueValidator that holds base validation functions for any type of object.
11+
*
12+
* @since 0.1
13+
*
14+
* @license GPL-2.0+
15+
* @author Jeroen De Dauw < [email protected] >
16+
*/
17+
abstract class ValueValidatorBase implements ValueValidator {
18+
19+
/**
20+
* A list of allowed values. This means the parameters value(s) must be in the list
21+
* during validation. False for no restriction.
22+
*
23+
* @since 0.1
24+
*
25+
* @var array|false
26+
*/
27+
protected $allowedValues = false;
28+
29+
/**
30+
* A list of prohibited values. This means the parameters value(s) must
31+
* not be in the list during validation. False for no restriction.
32+
*
33+
* @since 0.1
34+
*
35+
* @var array|false
36+
*/
37+
protected $prohibitedValues = false;
38+
39+
/**
40+
* @since 0.1
41+
*
42+
* @var array
43+
*/
44+
protected $options = [];
45+
46+
/**
47+
* @since 0.1
48+
*
49+
* @var Error[]
50+
*/
51+
protected $errors = [];
52+
53+
/**
54+
* @see ValueValidator::validate
55+
*
56+
* @param mixed $value
57+
*
58+
* @return Result
59+
*/
60+
final public function validate( $value ) {
61+
$this->errors = [];
62+
63+
if ( $this->enableWhitelistRestrictions() ) {
64+
$this->valueIsAllowed( $value );
65+
}
66+
67+
$this->doValidation( $value );
68+
69+
if ( $this->errors === [] ) {
70+
return Result::newSuccess();
71+
} else {
72+
return Result::newError( $this->errors );
73+
}
74+
}
75+
76+
/**
77+
* Checks the value against the allowed values and prohibited values lists in case they are set.
78+
*
79+
* @since 0.1
80+
*
81+
* @param mixed $value
82+
*/
83+
protected function valueIsAllowed( $value ) {
84+
if ( $this->allowedValues !== false && !in_array( $value, $this->allowedValues, true ) ) {
85+
$this->addErrorMessage( 'Value not in whitelist' );
86+
}
87+
88+
if ( $this->prohibitedValues !== false && in_array( $value, $this->prohibitedValues, true ) ) {
89+
$this->addErrorMessage( 'Value in blacklist' );
90+
}
91+
}
92+
93+
/**
94+
* @see ValueValidator::validate
95+
*
96+
* @since 0.1
97+
*
98+
* @param mixed $value
99+
*/
100+
abstract public function doValidation( $value );
101+
102+
/**
103+
* Sets the parameter definition values contained in the provided array.
104+
* @see ParamDefinition::setArrayValues
105+
*
106+
* @param array $param
107+
*/
108+
public function setOptions( array $param ) {
109+
if ( $this->enableWhitelistRestrictions() ) {
110+
if ( array_key_exists( 'values', $param ) ) {
111+
$this->allowedValues = $param['values'];
112+
}
113+
114+
if ( array_key_exists( 'excluding', $param ) ) {
115+
$this->prohibitedValues = $param['excluding'];
116+
}
117+
}
118+
119+
$this->options = $param;
120+
}
121+
122+
/**
123+
* Registers an error message.
124+
*
125+
* @since 0.1
126+
*
127+
* @param string $errorMessage
128+
*/
129+
protected function addErrorMessage( $errorMessage ) {
130+
$this->addError( Error::newError( $errorMessage ) );
131+
}
132+
133+
/**
134+
* Registers an error.
135+
*
136+
* @since 0.1
137+
*
138+
* @param Error $error
139+
*/
140+
protected function addError( Error $error ) {
141+
$this->errors[] = $error;
142+
}
143+
144+
/**
145+
* Registers a list of errors.
146+
*
147+
* @since 0.1
148+
*
149+
* @param Error[] $errors
150+
*/
151+
protected function addErrors( array $errors ) {
152+
$this->errors = array_merge( $this->errors, $errors );
153+
}
154+
155+
/**
156+
* Runs the value through the provided ValueValidator and registers the errors.
157+
* Options of $this can be mapped to those of the passed ValueValidator using
158+
* the $optionMap parameter in which keys are source names and values are target
159+
* names.
160+
*
161+
* @since 0.1
162+
*
163+
* @param mixed $value
164+
* @param ValueValidator $validator
165+
* @param string|null $property
166+
* @param array $optionMap
167+
*/
168+
protected function runSubValidator(
169+
$value,
170+
ValueValidator $validator,
171+
$property = null,
172+
array $optionMap = []
173+
) {
174+
if ( $optionMap !== [] ) {
175+
$options = [];
176+
177+
foreach ( $optionMap as $source => $target ) {
178+
if ( array_key_exists( $source, $this->options ) ) {
179+
$options[$target] = $this->options[$source];
180+
}
181+
}
182+
183+
$validator->setOptions( $options );
184+
}
185+
186+
/**
187+
* @var Error $error
188+
*/
189+
foreach ( $validator->validate( $value )->getErrors() as $error ) {
190+
$this->addError( Error::newError( $error->getText(), $property ) );
191+
}
192+
}
193+
194+
/**
195+
* If the "values" and "excluding" arguments should be held into account.
196+
*
197+
* @since 0.1
198+
*
199+
* @return bool
200+
*/
201+
protected function enableWhitelistRestrictions() {
202+
return true;
203+
}
204+
205+
/**
206+
* Returns the allowed values.
207+
*
208+
* TODO: think about how to access set options in general and if we want to have
209+
* whitelist and baclklist values in the validator objects to begin with.
210+
*
211+
* @since 0.1
212+
*
213+
* @return array|bool false
214+
*/
215+
public function getWhitelistedValues() {
216+
return $this->allowedValues;
217+
}
218+
219+
}

src/RangeValidator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ValueValidators;
44

55
use Exception;
6+
use ValueValidators\PackagePrivate\ValueValidatorBase;
67

78
/**
89
* ValueValidator that validates if a numeric value is within a certain range.
@@ -12,7 +13,7 @@
1213
* @license GPL-2.0-or-later
1314
* @author Jeroen De Dauw < [email protected] >
1415
*/
15-
class RangeValidator extends ValueValidatorObject {
16+
class RangeValidator extends ValueValidatorBase {
1617

1718
/**
1819
* Lower bound of the range (included). Either a number or false, for no lower limit.
@@ -82,7 +83,7 @@ public function setWithinRange( $point, $range ) {
8283
}
8384

8485
/**
85-
* @see ValueValidatorObject::doValidation
86+
* @see ValueValidatorBase::doValidation
8687
*
8788
* @since 0.1
8889
*

src/StringValidator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ValueValidators;
44

55
use Exception;
6+
use ValueValidators\PackagePrivate\ValueValidatorBase;
67

78
/**
89
* ValueValidator that validates a string value.
@@ -12,10 +13,10 @@
1213
* @license GPL-2.0-or-later
1314
* @author Jeroen De Dauw < [email protected] >
1415
*/
15-
class StringValidator extends ValueValidatorObject {
16+
class StringValidator extends ValueValidatorBase {
1617

1718
/**
18-
* @see ValueValidatorObject::doValidation
19+
* @see ValueValidatorBase::doValidation
1920
*
2021
* @since 0.1
2122
*

src/TitleValidator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ValueValidators;
44

55
use Title;
6+
use ValueValidators\PackagePrivate\ValueValidatorBase;
67

78
/**
89
* ValueValidator that validates a Title object.
@@ -12,7 +13,7 @@
1213
* @license GPL-2.0-or-later
1314
* @author Jeroen De Dauw < [email protected] >
1415
*/
15-
class TitleValidator extends ValueValidatorObject {
16+
class TitleValidator extends ValueValidatorBase {
1617

1718
/**
1819
* @since 0.1
@@ -31,7 +32,7 @@ public function setHasToExist( $hasToExist ) {
3132
}
3233

3334
/**
34-
* @see ValueValidatorObject::doValidation
35+
* @see ValueValidatorBase::doValidation
3536
*
3637
* @since 0.1
3738
*

0 commit comments

Comments
 (0)