Skip to content

Commit

Permalink
Add compat with DV Interfaces 1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed Jan 22, 2021
1 parent cdb0301 commit 8a60a7b
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ for the [Maps](https://github.com/JeroenDeDauw/Maps) and
### 1.0.0 (2021-01-22)

* Added compatibility with `data-values/data-values` 3.x
* Added compatibility with `data-values/interfaces` 1.x

### 0.1.3 (2018-08-02)

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"require": {
"php": ">=7.2",
"data-values/data-values": "^3.0.0|^2.1.1|~1.0|~0.1",
"data-values/interfaces": "~0.2.0|~0.1.0"
"data-values/interfaces": "^1.0.0|~0.2.0|~0.1.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5",
Expand Down
5 changes: 3 additions & 2 deletions src/DimensionValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ValueValidators;

use Exception;
use ValueValidators\PackagePrivate\ValueValidatorBase;

/**
* ValueValidator that validates a dimension value.
Expand All @@ -12,7 +13,7 @@
* @license GPL-2.0-or-later
* @author Jeroen De Dauw < [email protected] >
*/
class DimensionValidator extends ValueValidatorObject {
class DimensionValidator extends ValueValidatorBase {

/**
* @since 0.1
Expand Down Expand Up @@ -103,7 +104,7 @@ public function setRange( $lowerBound, $upperBound ) {
}

/**
* @see ValueValidatorObject::doValidation
* @see ValueValidatorBase::doValidation
*
* @since 0.1
*
Expand Down
7 changes: 4 additions & 3 deletions src/ListValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ValueValidators;

use Exception;
use ValueValidators\PackagePrivate\ValueValidatorBase;

/**
* ValueValidator that validates a list of values.
Expand All @@ -13,10 +14,10 @@
* @author Jeroen De Dauw < [email protected] >
* @author Thiemo Kreuz
*/
class ListValidator extends ValueValidatorObject {
class ListValidator extends ValueValidatorBase {

/**
* @see ValueValidatorObject::doValidation
* @see ValueValidatorBase::doValidation
*
* @since 0.1
*
Expand Down Expand Up @@ -50,7 +51,7 @@ public function doValidation( $value ) {
}

/**
* @see ValueValidatorObject::enableWhitelistRestrictions
* @see ValueValidatorBase::enableWhitelistRestrictions
*
* @since 0.1
*
Expand Down
219 changes: 219 additions & 0 deletions src/PackagePrivate/ValueValidatorBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
<?php

namespace ValueValidators\PackagePrivate;

use ValueValidators\Error;
use ValueValidators\Result;
use ValueValidators\ValueValidator;

/**
* ValueValidator that holds base validation functions for any type of object.
*
* @since 0.1
*
* @license GPL-2.0+
* @author Jeroen De Dauw < [email protected] >
*/
abstract class ValueValidatorBase implements ValueValidator {

/**
* A list of allowed values. This means the parameters value(s) must be in the list
* during validation. False for no restriction.
*
* @since 0.1
*
* @var array|false
*/
protected $allowedValues = false;

/**
* A list of prohibited values. This means the parameters value(s) must
* not be in the list during validation. False for no restriction.
*
* @since 0.1
*
* @var array|false
*/
protected $prohibitedValues = false;

/**
* @since 0.1
*
* @var array
*/
protected $options = [];

/**
* @since 0.1
*
* @var Error[]
*/
protected $errors = [];

/**
* @see ValueValidator::validate
*
* @param mixed $value
*
* @return Result
*/
final public function validate( $value ) {
$this->errors = [];

if ( $this->enableWhitelistRestrictions() ) {
$this->valueIsAllowed( $value );
}

$this->doValidation( $value );

if ( $this->errors === [] ) {
return Result::newSuccess();
} else {
return Result::newError( $this->errors );
}
}

/**
* Checks the value against the allowed values and prohibited values lists in case they are set.
*
* @since 0.1
*
* @param mixed $value
*/
protected function valueIsAllowed( $value ) {
if ( $this->allowedValues !== false && !in_array( $value, $this->allowedValues, true ) ) {
$this->addErrorMessage( 'Value not in whitelist' );
}

if ( $this->prohibitedValues !== false && in_array( $value, $this->prohibitedValues, true ) ) {
$this->addErrorMessage( 'Value in blacklist' );
}
}

/**
* @see ValueValidator::validate
*
* @since 0.1
*
* @param mixed $value
*/
abstract public function doValidation( $value );

/**
* Sets the parameter definition values contained in the provided array.
* @see ParamDefinition::setArrayValues
*
* @param array $param
*/
public function setOptions( array $param ) {
if ( $this->enableWhitelistRestrictions() ) {
if ( array_key_exists( 'values', $param ) ) {
$this->allowedValues = $param['values'];
}

if ( array_key_exists( 'excluding', $param ) ) {
$this->prohibitedValues = $param['excluding'];
}
}

$this->options = $param;
}

/**
* Registers an error message.
*
* @since 0.1
*
* @param string $errorMessage
*/
protected function addErrorMessage( $errorMessage ) {
$this->addError( Error::newError( $errorMessage ) );
}

/**
* Registers an error.
*
* @since 0.1
*
* @param Error $error
*/
protected function addError( Error $error ) {
$this->errors[] = $error;
}

/**
* Registers a list of errors.
*
* @since 0.1
*
* @param Error[] $errors
*/
protected function addErrors( array $errors ) {
$this->errors = array_merge( $this->errors, $errors );
}

/**
* Runs the value through the provided ValueValidator and registers the errors.
* Options of $this can be mapped to those of the passed ValueValidator using
* the $optionMap parameter in which keys are source names and values are target
* names.
*
* @since 0.1
*
* @param mixed $value
* @param ValueValidator $validator
* @param string|null $property
* @param array $optionMap
*/
protected function runSubValidator(
$value,
ValueValidator $validator,
$property = null,
array $optionMap = []
) {
if ( $optionMap !== [] ) {
$options = [];

foreach ( $optionMap as $source => $target ) {
if ( array_key_exists( $source, $this->options ) ) {
$options[$target] = $this->options[$source];
}
}

$validator->setOptions( $options );
}

/**
* @var Error $error
*/
foreach ( $validator->validate( $value )->getErrors() as $error ) {
$this->addError( Error::newError( $error->getText(), $property ) );
}
}

/**
* If the "values" and "excluding" arguments should be held into account.
*
* @since 0.1
*
* @return bool
*/
protected function enableWhitelistRestrictions() {
return true;
}

/**
* Returns the allowed values.
*
* TODO: think about how to access set options in general and if we want to have
* whitelist and baclklist values in the validator objects to begin with.
*
* @since 0.1
*
* @return array|bool false
*/
public function getWhitelistedValues() {
return $this->allowedValues;
}

}
5 changes: 3 additions & 2 deletions src/RangeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ValueValidators;

use Exception;
use ValueValidators\PackagePrivate\ValueValidatorBase;

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

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

/**
* @see ValueValidatorObject::doValidation
* @see ValueValidatorBase::doValidation
*
* @since 0.1
*
Expand Down
5 changes: 3 additions & 2 deletions src/StringValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ValueValidators;

use Exception;
use ValueValidators\PackagePrivate\ValueValidatorBase;

/**
* ValueValidator that validates a string value.
Expand All @@ -12,10 +13,10 @@
* @license GPL-2.0-or-later
* @author Jeroen De Dauw < [email protected] >
*/
class StringValidator extends ValueValidatorObject {
class StringValidator extends ValueValidatorBase {

/**
* @see ValueValidatorObject::doValidation
* @see ValueValidatorBase::doValidation
*
* @since 0.1
*
Expand Down
5 changes: 3 additions & 2 deletions src/TitleValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ValueValidators;

use Title;
use ValueValidators\PackagePrivate\ValueValidatorBase;

/**
* ValueValidator that validates a Title object.
Expand All @@ -12,7 +13,7 @@
* @license GPL-2.0-or-later
* @author Jeroen De Dauw < [email protected] >
*/
class TitleValidator extends ValueValidatorObject {
class TitleValidator extends ValueValidatorBase {

/**
* @since 0.1
Expand All @@ -31,7 +32,7 @@ public function setHasToExist( $hasToExist ) {
}

/**
* @see ValueValidatorObject::doValidation
* @see ValueValidatorBase::doValidation
*
* @since 0.1
*
Expand Down

0 comments on commit 8a60a7b

Please sign in to comment.