-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cdb0301
commit 8a60a7b
Showing
8 changed files
with
237 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
namespace ValueValidators; | ||
|
||
use Exception; | ||
use ValueValidators\PackagePrivate\ValueValidatorBase; | ||
|
||
/** | ||
* ValueValidator that validates a dimension value. | ||
|
@@ -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 | ||
|
@@ -103,7 +104,7 @@ public function setRange( $lowerBound, $upperBound ) { | |
} | ||
|
||
/** | ||
* @see ValueValidatorObject::doValidation | ||
* @see ValueValidatorBase::doValidation | ||
* | ||
* @since 0.1 | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
namespace ValueValidators; | ||
|
||
use Exception; | ||
use ValueValidators\PackagePrivate\ValueValidatorBase; | ||
|
||
/** | ||
* ValueValidator that validates a list of values. | ||
|
@@ -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 | ||
* | ||
|
@@ -50,7 +51,7 @@ public function doValidation( $value ) { | |
} | ||
|
||
/** | ||
* @see ValueValidatorObject::enableWhitelistRestrictions | ||
* @see ValueValidatorBase::enableWhitelistRestrictions | ||
* | ||
* @since 0.1 | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
namespace ValueValidators; | ||
|
||
use Exception; | ||
use ValueValidators\PackagePrivate\ValueValidatorBase; | ||
|
||
/** | ||
* ValueValidator that validates if a numeric value is within a certain range. | ||
|
@@ -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. | ||
|
@@ -82,7 +83,7 @@ public function setWithinRange( $point, $range ) { | |
} | ||
|
||
/** | ||
* @see ValueValidatorObject::doValidation | ||
* @see ValueValidatorBase::doValidation | ||
* | ||
* @since 0.1 | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
namespace ValueValidators; | ||
|
||
use Exception; | ||
use ValueValidators\PackagePrivate\ValueValidatorBase; | ||
|
||
/** | ||
* ValueValidator that validates a string value. | ||
|
@@ -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 | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
namespace ValueValidators; | ||
|
||
use Title; | ||
use ValueValidators\PackagePrivate\ValueValidatorBase; | ||
|
||
/** | ||
* ValueValidator that validates a Title object. | ||
|
@@ -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 | ||
|
@@ -31,7 +32,7 @@ public function setHasToExist( $hasToExist ) { | |
} | ||
|
||
/** | ||
* @see ValueValidatorObject::doValidation | ||
* @see ValueValidatorBase::doValidation | ||
* | ||
* @since 0.1 | ||
* | ||
|