Skip to content

Commit 83dbac2

Browse files
authoredJan 22, 2021
Merge pull request #23 from DataValues/rel100
1.0.0 release
2 parents 2339613 + 193075a commit 83dbac2

18 files changed

+248
-25
lines changed
 

‎.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ build/
99
vendor/
1010

1111
composer.phar
12-
composer.lock
12+
composer.lock
13+
14+
.phpunit.result.cache

‎.phpcs.xml

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
<?xml version="1.0"?>
22
<ruleset>
3-
<rule ref="./vendor/wikibase/wikibase-codesniffer/Wikibase">
4-
<exclude name="Generic.Arrays.DisallowLongArraySyntax" />
5-
</rule>
3+
<file>src/</file>
4+
<file>tests/</file>
65

76
<rule ref="Generic.Files.LineLength">
8-
<properties>
9-
<property name="lineLimit" value="106" />
10-
</properties>
7+
<exclude name="Generic.Files.LineLength.TooLong" />
8+
<exclude name="Generic.Files.LineLength.MaxExceeded" />
119
</rule>
12-
13-
<file>.</file>
1410
</ruleset>

‎README.md

+5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ for the [Maps](https://github.com/JeroenDeDauw/Maps) and
5151

5252
## Release notes
5353

54+
### 1.0.0 (2021-01-22)
55+
56+
* Added compatibility with `data-values/data-values` 3.x
57+
* Added compatibility with `data-values/interfaces` 1.x
58+
5459
### 0.1.3 (2018-08-02)
5560

5661
* Made component installable with DataValues 2.x

‎composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
},
2828
"require": {
2929
"php": ">=7.2",
30-
"data-values/data-values": "^2.1.1|~1.0|~0.1",
31-
"data-values/interfaces": "~0.2.0|~0.1.0"
30+
"data-values/data-values": "^3.0.0|^2.1.1|~1.0|~0.1",
31+
"data-values/interfaces": "^1.0.0|~0.2.0|~0.1.0"
3232
},
3333
"require-dev": {
3434
"phpunit/phpunit": "^8.5",
35-
"wikibase/wikibase-codesniffer": "1.2.0"
35+
"mediawiki/mediawiki-codesniffer": "^34"
3636
},
3737
"autoload": {
3838
"psr-4": {

‎phpunit.xml.dist

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
stopOnFailure="false"
1111
stopOnIncomplete="false"
1212
stopOnSkipped="false"
13-
strict="true"
1413
verbose="true">
1514
<testsuites>
1615
<testsuite name="DataValuesValidators">

‎src/DimensionValidator.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace ValueValidators;
46

57
use Exception;
8+
use ValueValidators\PackagePrivate\ValueValidatorBase;
69

710
/**
811
* ValueValidator that validates a dimension value.
@@ -12,7 +15,7 @@
1215
* @license GPL-2.0-or-later
1316
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
1417
*/
15-
class DimensionValidator extends ValueValidatorObject {
18+
class DimensionValidator extends ValueValidatorBase {
1619

1720
/**
1821
* @since 0.1
@@ -103,7 +106,7 @@ public function setRange( $lowerBound, $upperBound ) {
103106
}
104107

105108
/**
106-
* @see ValueValidatorObject::doValidation
109+
* @see ValueValidatorBase::doValidation
107110
*
108111
* @since 0.1
109112
*

‎src/ListValidator.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace ValueValidators;
46

57
use Exception;
8+
use ValueValidators\PackagePrivate\ValueValidatorBase;
69

710
/**
811
* ValueValidator that validates a list of values.
@@ -13,10 +16,10 @@
1316
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
1417
* @author Thiemo Kreuz
1518
*/
16-
class ListValidator extends ValueValidatorObject {
19+
class ListValidator extends ValueValidatorBase {
1720

1821
/**
19-
* @see ValueValidatorObject::doValidation
22+
* @see ValueValidatorBase::doValidation
2023
*
2124
* @since 0.1
2225
*
@@ -50,7 +53,7 @@ public function doValidation( $value ) {
5053
}
5154

5255
/**
53-
* @see ValueValidatorObject::enableWhitelistRestrictions
56+
* @see ValueValidatorBase::enableWhitelistRestrictions
5457
*
5558
* @since 0.1
5659
*

‎src/NullValidator.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace ValueValidators;
46

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

‎src/RangeValidator.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace ValueValidators;
46

57
use Exception;
8+
use ValueValidators\PackagePrivate\ValueValidatorBase;
69

710
/**
811
* ValueValidator that validates if a numeric value is within a certain range.
@@ -12,7 +15,7 @@
1215
* @license GPL-2.0-or-later
1316
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
1417
*/
15-
class RangeValidator extends ValueValidatorObject {
18+
class RangeValidator extends ValueValidatorBase {
1619

1720
/**
1821
* Lower bound of the range (included). Either a number or false, for no lower limit.
@@ -82,7 +85,7 @@ public function setWithinRange( $point, $range ) {
8285
}
8386

8487
/**
85-
* @see ValueValidatorObject::doValidation
88+
* @see ValueValidatorBase::doValidation
8689
*
8790
* @since 0.1
8891
*

‎src/StringValidator.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace ValueValidators;
46

57
use Exception;
8+
use ValueValidators\PackagePrivate\ValueValidatorBase;
69

710
/**
811
* ValueValidator that validates a string value.
@@ -12,10 +15,10 @@
1215
* @license GPL-2.0-or-later
1316
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
1417
*/
15-
class StringValidator extends ValueValidatorObject {
18+
class StringValidator extends ValueValidatorBase {
1619

1720
/**
18-
* @see ValueValidatorObject::doValidation
21+
* @see ValueValidatorBase::doValidation
1922
*
2023
* @since 0.1
2124
*

‎src/TitleValidator.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace ValueValidators;
46

57
use Title;
8+
use ValueValidators\PackagePrivate\ValueValidatorBase;
69

710
/**
811
* ValueValidator that validates a Title object.
@@ -12,7 +15,7 @@
1215
* @license GPL-2.0-or-later
1316
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
1417
*/
15-
class TitleValidator extends ValueValidatorObject {
18+
class TitleValidator extends ValueValidatorBase {
1619

1720
/**
1821
* @since 0.1
@@ -31,7 +34,7 @@ public function setHasToExist( $hasToExist ) {
3134
}
3235

3336
/**
34-
* @see ValueValidatorObject::doValidation
37+
* @see ValueValidatorBase::doValidation
3538
*
3639
* @since 0.1
3740
*

‎tests/ValueValidators/DimensionValidatorTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace ValueValidators\Tests;
46

57
use PHPUnit\Framework\TestCase;

‎tests/ValueValidators/ListValidatorTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace ValueValidators\Tests;
46

57
use PHPUnit\Framework\TestCase;

‎tests/ValueValidators/RangeValidatorTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace ValueValidators\Tests;
46

57
use PHPUnit\Framework\TestCase;

‎tests/ValueValidators/StringValidatorTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace ValueValidators\Tests;
46

57
use PHPUnit\Framework\TestCase;

‎tests/ValueValidators/TitleValidatorTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
namespace ValueValidators\Tests;
46

57
use PHPUnit\Framework\TestCase;

‎tests/bootstrap.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php
22

3+
declare( strict_types = 1 );
4+
35
if ( PHP_SAPI !== 'cli' ) {
46
die( 'Not an entry point' );
57
}
68

79
error_reporting( E_ALL | E_STRICT );
8-
ini_set( 'display_errors', 1 );
10+
ini_set( 'display_errors', '1' );
911

1012
if ( !is_readable( __DIR__ . '/../vendor/autoload.php' ) ) {
1113
die( 'You need to install this package with Composer before you can run the tests' );

0 commit comments

Comments
 (0)
Please sign in to comment.