mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 00:32:16 +02:00
Apply "Symfony.Functions.ScopeOrder"
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
9c0f8dcfcc
commit
272f18dcf5
|
@ -22,6 +22,7 @@
|
|||
"require-dev": {
|
||||
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
|
||||
"egulias/email-validator": "^2.0",
|
||||
"escapestudios/symfony2-coding-standard": "^3.8",
|
||||
"malukenho/docheader": "^0.1.4",
|
||||
"mikey179/vfsstream": "^1.6",
|
||||
"phpstan/phpstan": "^0.11",
|
||||
|
|
|
@ -54,13 +54,6 @@ abstract class AbstractAge extends AbstractRule
|
|||
$this->baseDate = (int) date('Ymd') - $this->age * 10000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should compare the current base date with the given one.
|
||||
*
|
||||
* The dates are represented as integers in the format "Ymd".
|
||||
*/
|
||||
abstract protected function compare(int $baseDate, int $givenDate): bool;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
@ -77,6 +70,13 @@ abstract class AbstractAge extends AbstractRule
|
|||
return $this->isValidWithFormat($this->format, (string) $input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Should compare the current base date with the given one.
|
||||
*
|
||||
* The dates are represented as integers in the format "Ymd".
|
||||
*/
|
||||
abstract protected function compare(int $baseDate, int $givenDate): bool;
|
||||
|
||||
private function isValidWithoutFormat(string $dateTime): bool
|
||||
{
|
||||
$timestamp = strtotime($dateTime);
|
||||
|
|
|
@ -37,8 +37,6 @@ abstract class AbstractFilterRule extends AbstractRule
|
|||
$this->additionalChars = implode($additionalChars);
|
||||
}
|
||||
|
||||
abstract protected function validateFilteredInput(string $input): bool;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
@ -58,6 +56,8 @@ abstract class AbstractFilterRule extends AbstractRule
|
|||
return $filteredInput === '' || $this->validateFilteredInput($filteredInput);
|
||||
}
|
||||
|
||||
abstract protected function validateFilteredInput(string $input): bool;
|
||||
|
||||
private function filter(string $input): string
|
||||
{
|
||||
return str_replace(str_split($this->additionalChars), '', $input);
|
||||
|
|
|
@ -25,11 +25,6 @@ abstract class AbstractSearcher extends AbstractRule
|
|||
{
|
||||
use CanValidateUndefined;
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
abstract protected function getDataSource(): array;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
@ -42,4 +37,9 @@ abstract class AbstractSearcher extends AbstractRule
|
|||
|
||||
return in_array($input, $dataSource, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
abstract protected function getDataSource(): array;
|
||||
}
|
||||
|
|
|
@ -93,6 +93,23 @@ final class Domain extends AbstractRule
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function check($input): void
|
||||
{
|
||||
try {
|
||||
$this->assert($input);
|
||||
} catch (NestedValidationException $exception) {
|
||||
/** @var ValidationException $childException */
|
||||
foreach ($exception as $childException) {
|
||||
throw $childException;
|
||||
}
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ValidationException[] $exceptions
|
||||
* @param mixed $input
|
||||
|
@ -111,23 +128,6 @@ final class Domain extends AbstractRule
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function check($input): void
|
||||
{
|
||||
try {
|
||||
$this->assert($input);
|
||||
} catch (NestedValidationException $exception) {
|
||||
/** @var ValidationException $childException */
|
||||
foreach ($exception as $childException) {
|
||||
throw $childException;
|
||||
}
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
|
||||
private function createGenericRule(): Validatable
|
||||
{
|
||||
return new AllOf(
|
||||
|
|
|
@ -49,6 +49,18 @@ final class In extends AbstractRule
|
|||
$this->compareIdentical = $compareIdentical;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if ($this->compareIdentical) {
|
||||
return $this->validateIdentical($input);
|
||||
}
|
||||
|
||||
return $this->validateEquals($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $input
|
||||
*/
|
||||
|
@ -84,16 +96,4 @@ final class In extends AbstractRule
|
|||
|
||||
return mb_strpos($this->haystack, $inputString, 0, mb_detect_encoding($inputString)) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if ($this->compareIdentical) {
|
||||
return $this->validateIdentical($input);
|
||||
}
|
||||
|
||||
return $this->validateEquals($input);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,6 +48,25 @@ final class KeyNested extends AbstractRelated
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getReferenceValue($input)
|
||||
{
|
||||
if (is_scalar($input)) {
|
||||
$message = sprintf('Cannot select the %s in the given data', $this->getReference());
|
||||
throw new ComponentException($message);
|
||||
}
|
||||
|
||||
$keys = $this->getReferencePieces();
|
||||
$value = $input;
|
||||
while (!is_null($key = array_shift($keys))) {
|
||||
$value = $this->getValue($value, $key);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
|
@ -127,23 +146,4 @@ final class KeyNested extends AbstractRelated
|
|||
$message = sprintf('Cannot select the property %s from the given data', $this->getReference());
|
||||
throw new ComponentException($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getReferenceValue($input)
|
||||
{
|
||||
if (is_scalar($input)) {
|
||||
$message = sprintf('Cannot select the %s in the given data', $this->getReference());
|
||||
throw new ComponentException($message);
|
||||
}
|
||||
|
||||
$keys = $this->getReferencePieces();
|
||||
$value = $input;
|
||||
while (!is_null($key = array_shift($keys))) {
|
||||
$value = $this->getValue($value, $key);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,6 +52,42 @@ final class KeySet extends AbstractWrapper
|
|||
parent::__construct(new AllOf(...$this->keyRules));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function assert($input): void
|
||||
{
|
||||
if (!$this->hasValidStructure($input)) {
|
||||
throw $this->reportError($input);
|
||||
}
|
||||
|
||||
parent::assert($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function check($input): void
|
||||
{
|
||||
if (!$this->hasValidStructure($input)) {
|
||||
throw $this->reportError($input);
|
||||
}
|
||||
|
||||
parent::check($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if (!$this->hasValidStructure($input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return parent::validate($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ComponentException
|
||||
*/
|
||||
|
@ -96,40 +132,4 @@ final class KeySet extends AbstractWrapper
|
|||
|
||||
return count($input) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function assert($input): void
|
||||
{
|
||||
if (!$this->hasValidStructure($input)) {
|
||||
throw $this->reportError($input);
|
||||
}
|
||||
|
||||
parent::assert($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function check($input): void
|
||||
{
|
||||
if (!$this->hasValidStructure($input)) {
|
||||
throw $this->reportError($input);
|
||||
}
|
||||
|
||||
parent::check($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if (!$this->hasValidStructure($input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return parent::validate($input);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,46 +51,6 @@ final class KeyValue extends AbstractRule
|
|||
$this->baseKey = $baseKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $input
|
||||
*/
|
||||
private function getRule($input): Validatable
|
||||
{
|
||||
if (!isset($input[$this->comparedKey])) {
|
||||
throw parent::reportError($this->comparedKey);
|
||||
}
|
||||
|
||||
if (!isset($input[$this->baseKey])) {
|
||||
throw parent::reportError($this->baseKey);
|
||||
}
|
||||
|
||||
try {
|
||||
$rule = Factory::getDefaultInstance()->rule($this->ruleName, [$input[$this->baseKey]]);
|
||||
$rule->setName($this->comparedKey);
|
||||
} catch (ComponentException $exception) {
|
||||
throw parent::reportError($input, ['component' => true]);
|
||||
}
|
||||
|
||||
return $rule;
|
||||
}
|
||||
|
||||
private function overwriteExceptionParams(ValidationException $exception): ValidationException
|
||||
{
|
||||
$params = [];
|
||||
foreach (array_keys($exception->getParams()) as $key) {
|
||||
if (in_array($key, ['template', 'translator'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$params[$key] = $this->baseKey;
|
||||
}
|
||||
$params['name'] = $this->comparedKey;
|
||||
|
||||
$exception->updateParams($params);
|
||||
|
||||
return $exception;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
@ -144,4 +104,44 @@ final class KeyValue extends AbstractRule
|
|||
return $this->overwriteExceptionParams($exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $input
|
||||
*/
|
||||
private function getRule($input): Validatable
|
||||
{
|
||||
if (!isset($input[$this->comparedKey])) {
|
||||
throw parent::reportError($this->comparedKey);
|
||||
}
|
||||
|
||||
if (!isset($input[$this->baseKey])) {
|
||||
throw parent::reportError($this->baseKey);
|
||||
}
|
||||
|
||||
try {
|
||||
$rule = Factory::getDefaultInstance()->rule($this->ruleName, [$input[$this->baseKey]]);
|
||||
$rule->setName($this->comparedKey);
|
||||
} catch (ComponentException $exception) {
|
||||
throw parent::reportError($input, ['component' => true]);
|
||||
}
|
||||
|
||||
return $rule;
|
||||
}
|
||||
|
||||
private function overwriteExceptionParams(ValidationException $exception): ValidationException
|
||||
{
|
||||
$params = [];
|
||||
foreach (array_keys($exception->getParams()) as $key) {
|
||||
if (in_array($key, ['template', 'translator'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$params[$key] = $this->baseKey;
|
||||
}
|
||||
$params['name'] = $this->comparedKey;
|
||||
|
||||
$exception->updateParams($params);
|
||||
|
||||
return $exception;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,16 +30,6 @@ use function sprintf;
|
|||
*/
|
||||
final class Phone extends AbstractRule
|
||||
{
|
||||
private function getPregFormat(): string
|
||||
{
|
||||
return sprintf(
|
||||
'/^\+?(%1$s)? ?(?(?=\()(\(%2$s\) ?%3$s)|([. -]?(%2$s[. -]*)?%3$s))$/',
|
||||
'\d{0,3}',
|
||||
'\d{1,3}',
|
||||
'((\d{3,5})[. -]?(\d{4})|(\d{2}[. -]?){4})'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
@ -51,4 +41,14 @@ final class Phone extends AbstractRule
|
|||
|
||||
return preg_match($this->getPregFormat(), (string) $input) > 0;
|
||||
}
|
||||
|
||||
private function getPregFormat(): string
|
||||
{
|
||||
return sprintf(
|
||||
'/^\+?(%1$s)? ?(?(?=\()(\(%2$s\) ?%3$s)|([. -]?(%2$s[. -]*)?%3$s))$/',
|
||||
'\d{0,3}',
|
||||
'\d{1,3}',
|
||||
'((\d{3,5})[. -]?(\d{4})|(\d{2}[. -]?){4})'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -61,6 +61,22 @@ final class Size extends AbstractRule
|
|||
$this->maxValue = $maxSize ? $this->toBytes($maxSize) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if ($input instanceof SplFileInfo) {
|
||||
return $this->isValidSize($input->getSize());
|
||||
}
|
||||
|
||||
if (is_string($input)) {
|
||||
return $this->isValidSize((int) filesize($input));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo Move it to a trait
|
||||
*
|
||||
|
@ -97,20 +113,4 @@ final class Size extends AbstractRule
|
|||
|
||||
return $size <= $this->maxValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if ($input instanceof SplFileInfo) {
|
||||
return $this->isValidSize($input->getSize());
|
||||
}
|
||||
|
||||
if (is_string($input)) {
|
||||
return $this->isValidSize((int) filesize($input));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,36 +53,6 @@ final class Zend extends AbstractRule
|
|||
$this->zendValidator = $this->zendValidator($validator, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $validator
|
||||
* @param mixed[] $params
|
||||
*
|
||||
* @throws ComponentException
|
||||
*/
|
||||
private function zendValidator($validator, array $params = []): ValidatorInterface
|
||||
{
|
||||
if ($validator instanceof ValidatorInterface) {
|
||||
return $validator;
|
||||
}
|
||||
|
||||
if (!is_string($validator)) {
|
||||
throw new ComponentException('The given argument is not a valid Zend Validator');
|
||||
}
|
||||
|
||||
$className = stripos($validator, 'Zend') === false ? 'Zend\\Validator\\'.$validator : '\\'.$validator;
|
||||
|
||||
try {
|
||||
$reflection = new ReflectionClass($className);
|
||||
if (!$reflection->isInstantiable()) {
|
||||
throw new ComponentException(sprintf('"%s" is not instantiable', $className));
|
||||
}
|
||||
|
||||
return $this->zendValidator($reflection->newInstanceArgs($params));
|
||||
} catch (Throwable $exception) {
|
||||
throw new ComponentException(sprintf('Could not create "%s"', $validator), 0, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
@ -134,4 +104,34 @@ final class Zend extends AbstractRule
|
|||
{
|
||||
return (clone $this->zendValidator)->isValid($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $validator
|
||||
* @param mixed[] $params
|
||||
*
|
||||
* @throws ComponentException
|
||||
*/
|
||||
private function zendValidator($validator, array $params = []): ValidatorInterface
|
||||
{
|
||||
if ($validator instanceof ValidatorInterface) {
|
||||
return $validator;
|
||||
}
|
||||
|
||||
if (!is_string($validator)) {
|
||||
throw new ComponentException('The given argument is not a valid Zend Validator');
|
||||
}
|
||||
|
||||
$className = stripos($validator, 'Zend') === false ? 'Zend\\Validator\\'.$validator : '\\'.$validator;
|
||||
|
||||
try {
|
||||
$reflection = new ReflectionClass($className);
|
||||
if (!$reflection->isInstantiable()) {
|
||||
throw new ComponentException(sprintf('"%s" is not instantiable', $className));
|
||||
}
|
||||
|
||||
return $this->zendValidator($reflection->newInstanceArgs($params));
|
||||
} catch (Throwable $exception) {
|
||||
throw new ComponentException(sprintf('Could not create "%s"', $validator), 0, $exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -160,4 +160,5 @@
|
|||
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines">
|
||||
<severity>5</severity>
|
||||
</rule>
|
||||
<rule ref="Symfony.Functions.ScopeOrder" />
|
||||
</ruleset>
|
||||
|
|
Loading…
Reference in a new issue