From 0cdd8c45464e75faefee349f945a1f495c9a8d7f Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Mon, 9 Jul 2018 09:24:08 +0200 Subject: [PATCH] Fix issues after merging 1.1 These two branches are very different, therefore merging is becoming very hard. I decided to not put these changes together with 5750952 because it seems easy to track these changes with a specific commit. While working on this merge I realized that would make more sense to create "AbstractComparison" to handle the rules that compare values. Signed-off-by: Henrique Moody --- .../Exceptions/NestedValidationException.php | 37 ++++------- library/Helpers/ComparisonHelper.php | 13 ++++ library/Rules/AbstractComparison.php | 66 +++++++++++++++++++ library/Rules/AbstractComposite.php | 8 ++- library/Rules/GreaterThan.php | 25 +------ library/Rules/LessThan.php | 25 +------ library/Rules/Max.php | 25 +------ library/Rules/Min.php | 25 +------ library/Validator.php | 8 +-- tests/integration/issue-1033.phpt | 8 +-- tests/integration/issue-179.phpt | 6 +- tests/integration/issue-425.phpt | 6 +- tests/integration/issue-619.phpt | 2 +- tests/integration/issue-805.phpt | 2 +- tests/unit/Rules/GreaterThanTest.php | 3 + tests/unit/Rules/LessThanTest.php | 3 + tests/unit/Rules/MaxTest.php | 1 + tests/unit/Rules/MinTest.php | 1 + 18 files changed, 131 insertions(+), 133 deletions(-) create mode 100644 library/Rules/AbstractComparison.php diff --git a/library/Exceptions/NestedValidationException.php b/library/Exceptions/NestedValidationException.php index 7adaa104..c9e242e5 100644 --- a/library/Exceptions/NestedValidationException.php +++ b/library/Exceptions/NestedValidationException.php @@ -16,8 +16,11 @@ namespace Respect\Validation\Exceptions; use IteratorAggregate; use RecursiveIteratorIterator; use SplObjectStorage; +use const PHP_EOL; use function count; +use function implode; use function is_array; +use function str_repeat; class NestedValidationException extends ValidationException implements IteratorAggregate { @@ -59,20 +62,7 @@ class NestedValidationException extends ValidationException implements IteratorA * * @return bool */ - private function isOmissible(Exception $exception) - { - if (!$exception instanceof self) { - return false; - } - - $relatedExceptions = $exception->getRelated(); - $relatedExceptions->rewind(); - $childException = $relatedExceptions->current(); - - return 1 === $relatedExceptions->count() && !$childException instanceof NonOmissibleException; - } - - private function isSkippable(ValidationException $exception) + private function isOmissible(Exception $exception): bool { if (!$exception instanceof self) { return false; @@ -82,19 +72,17 @@ class NestedValidationException extends ValidationException implements IteratorA return false; } - if (!$exception->hasCustomTemplate()) { + $exception->getRelated()->rewind(); + $childException = $exception->getRelated()->current(); + if ($childException->getMessage() === $exception->getMessage()) { return true; } - return $this->hasChildTemplate($exception); - } + if ($exception->hasCustomTemplate()) { + return $childException->hasCustomTemplate(); + } - private function hasChildTemplate(self $exception) - { - $exception->getRelated()->rewind(); - $childException = $exception->getRelated()->current(); - - return $childException->getMessage() === $exception->getMessage(); + return !$childException instanceof NonOmissibleException; } /** @@ -105,7 +93,6 @@ class NestedValidationException extends ValidationException implements IteratorA $childrenExceptions = new SplObjectStorage(); $recursiveIteratorIterator = $this->getRecursiveIterator(); - $exceptionIterator = $recursiveIteratorIterator->getInnerIterator(); $lastDepth = 0; $lastDepthOriginal = 0; @@ -181,7 +168,7 @@ class NestedValidationException extends ValidationException implements IteratorA $messages = []; $leveler = 1; - if (!$this->isSkippable($this)) { + if (!$this->isOmissible($this)) { $leveler = 0; $messages[] = sprintf('- %s', $this->getMessage()); } diff --git a/library/Helpers/ComparisonHelper.php b/library/Helpers/ComparisonHelper.php index ec528c6c..f5234990 100644 --- a/library/Helpers/ComparisonHelper.php +++ b/library/Helpers/ComparisonHelper.php @@ -55,4 +55,17 @@ trait ComparisonHelper return $value; } } + + /** + * Returns whether the values can be compared or not. + * + * @param mixed $left + * @param mixed $right + * + * @return bool + */ + private function isAbleToCompareValues($left, $right) + { + return is_scalar($left) === is_scalar($right); + } } diff --git a/library/Rules/AbstractComparison.php b/library/Rules/AbstractComparison.php new file mode 100644 index 00000000..5634bce8 --- /dev/null +++ b/library/Rules/AbstractComparison.php @@ -0,0 +1,66 @@ + + * + * For the full copyright and license information, please view the "LICENSE.md" + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Respect\Validation\Rules; + +use Respect\Validation\Helpers\ComparisonHelper; + +/** + * Abstract class to help on creating rules that compare value. + * + * @author Henrique Moody + */ +abstract class AbstractComparison extends AbstractRule +{ + use ComparisonHelper; + + /** + * @var mixed + */ + private $compareTo; + + /** + * Initializes the rule by setting the value to be compared to the input. + * + * @param mixed $maxValue + */ + public function __construct($maxValue) + { + $this->compareTo = $maxValue; + } + + /** + * {@inheritdoc} + */ + public function validate($input): bool + { + $left = $this->toComparable($input); + $right = $this->toComparable($this->compareTo); + + if (!$this->isAbleToCompareValues($left, $right)) { + return false; + } + + return $this->compare($left, $right); + } + + /** + * Compare both values and return whether the comparison is valid or not. + * + * @param mixed $left + * @param mixed $right + * + * @return bool + */ + abstract protected function compare($left, $right): bool; +} diff --git a/library/Rules/AbstractComposite.php b/library/Rules/AbstractComposite.php index 2498e653..8f4c3516 100644 --- a/library/Rules/AbstractComposite.php +++ b/library/Rules/AbstractComposite.php @@ -103,6 +103,8 @@ abstract class AbstractComposite extends AbstractRule try { $rule->assert($input); } catch (ValidationException $exception) { + $this->updateExceptionTemplate($exception); + return $exception; } @@ -123,20 +125,20 @@ abstract class AbstractComposite extends AbstractRule return null !== $rule->getName(); } - private function setExceptionTemplate(ValidationException $exception) + private function updateExceptionTemplate(ValidationException $exception): void { if (null === $this->template || $exception->hasCustomTemplate()) { return; } - $exception->setTemplate($this->template); + $exception->updateTemplate($this->template); if (!$exception instanceof NestedValidationException) { return; } foreach ($exception->getRelated() as $relatedException) { - $this->setExceptionTemplate($relatedException); + $this->updateExceptionTemplate($relatedException); } } } diff --git a/library/Rules/GreaterThan.php b/library/Rules/GreaterThan.php index 12f95eb7..d8b71d4d 100644 --- a/library/Rules/GreaterThan.php +++ b/library/Rules/GreaterThan.php @@ -13,37 +13,18 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -use Respect\Validation\Helpers\ComparisonHelper; - /** * Validates whether the input is less than a value. * * @author Henrique Moody */ -final class GreaterThan extends AbstractRule +final class GreaterThan extends AbstractComparison { - use ComparisonHelper; - - /** - * @var mixed - */ - private $compareTo; - - /** - * Initializes the rule by setting the value to be compared to the input. - * - * @param mixed $compareTo - */ - public function __construct($compareTo) - { - $this->compareTo = $compareTo; - } - /** * {@inheritdoc} */ - public function validate($input): bool + protected function compare($left, $right): bool { - return $this->toComparable($input) > $this->toComparable($this->compareTo); + return $left > $right; } } diff --git a/library/Rules/LessThan.php b/library/Rules/LessThan.php index 7962b764..6582fdbb 100644 --- a/library/Rules/LessThan.php +++ b/library/Rules/LessThan.php @@ -13,37 +13,18 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -use Respect\Validation\Helpers\ComparisonHelper; - /** * Validates whether the input is less than a value. * * @author Henrique Moody */ -final class LessThan extends AbstractRule +final class LessThan extends AbstractComparison { - use ComparisonHelper; - - /** - * @var mixed - */ - private $compareTo; - - /** - * Initializes the rule by setting the value to be compared to the input. - * - * @param mixed $compareTo - */ - public function __construct($compareTo) - { - $this->compareTo = $compareTo; - } - /** * {@inheritdoc} */ - public function validate($input): bool + protected function compare($left, $right): bool { - return $this->toComparable($input) < $this->toComparable($this->compareTo); + return $left < $right; } } diff --git a/library/Rules/Max.php b/library/Rules/Max.php index 0511ee0a..8a2d7c28 100644 --- a/library/Rules/Max.php +++ b/library/Rules/Max.php @@ -13,38 +13,19 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -use Respect\Validation\Helpers\ComparisonHelper; - /** * Validates whether the input is less than or equal to a value. * * @author Alexandre Gomes Gaigalas * @author Henrique Moody */ -final class Max extends AbstractRule +final class Max extends AbstractComparison { - use ComparisonHelper; - - /** - * @var mixed - */ - private $compareTo; - - /** - * Initializes the rule by setting the value to be compared to the input. - * - * @param mixed $maxValue - */ - public function __construct($maxValue) - { - $this->compareTo = $maxValue; - } - /** * {@inheritdoc} */ - public function validate($input): bool + protected function compare($left, $right): bool { - return $this->toComparable($input) <= $this->toComparable($this->compareTo); + return $left <= $right; } } diff --git a/library/Rules/Min.php b/library/Rules/Min.php index cdfc848d..9f5c1689 100644 --- a/library/Rules/Min.php +++ b/library/Rules/Min.php @@ -13,38 +13,19 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -use Respect\Validation\Helpers\ComparisonHelper; - /** * Validates whether the input is greater than or equal to a value. * * @author Alexandre Gomes Gaigalas * @author Henrique Moody */ -final class Min extends AbstractRule +final class Min extends AbstractComparison { - use ComparisonHelper; - - /** - * @var mixed - */ - private $compareTo; - - /** - * Initializes the rule by setting the value to be compared to the input. - * - * @param mixed $compareTo - */ - public function __construct($compareTo) - { - $this->compareTo = $compareTo; - } - /** * {@inheritdoc} */ - public function validate($input): bool + protected function compare($left, $right): bool { - return $this->toComparable($input) >= $this->toComparable($this->compareTo); + return $left >= $right; } } diff --git a/library/Validator.php b/library/Validator.php index 15ad971a..3b6338f8 100644 --- a/library/Validator.php +++ b/library/Validator.php @@ -189,13 +189,7 @@ class Validator extends AllOf */ public static function __callStatic($ruleName, $arguments) { - if ('allOf' === $ruleName) { - return static::buildRule($ruleName, $arguments); - } - - $validator = new static(); - - return $validator->__call($ruleName, $arguments); + return (new static())->__call($ruleName, $arguments); } /** diff --git a/tests/integration/issue-1033.phpt b/tests/integration/issue-1033.phpt index 8a4ef742..f756220e 100644 --- a/tests/integration/issue-1033.phpt +++ b/tests/integration/issue-1033.phpt @@ -21,7 +21,7 @@ try { } ?> --EXPECTF-- -- Each item in { "A", "B", "B" } must be valid - - "A" must be equals 1 - - "B" must be equals 1 - - "B" must be equals 1 +- Each item in `{ "A", "B", "B" }` must be valid + - "A" must equal 1 + - "B" must equal 1 + - "B" must equal 1 diff --git a/tests/integration/issue-179.phpt b/tests/integration/issue-179.phpt index 11c0b896..bff7cc6f 100644 --- a/tests/integration/issue-179.phpt +++ b/tests/integration/issue-179.phpt @@ -7,7 +7,7 @@ use Respect\Validation\Exceptions\AllOfException; use Respect\Validation\Validator as v; $config = [ - 'host' => 'my_host', + 'host' => 1, 'password' => 'my_password', 'schema' => 'my_schema', ]; @@ -26,4 +26,6 @@ try { } ?> --EXPECTF-- -- Key user must be present +- These rules must pass for Settings + - host must be of type string + - Key user must be present diff --git a/tests/integration/issue-425.phpt b/tests/integration/issue-425.phpt index 1c79cf41..c506bcdc 100644 --- a/tests/integration/issue-425.phpt +++ b/tests/integration/issue-425.phpt @@ -25,5 +25,7 @@ try { } ?> --EXPECTF-- -- Key reference must be present -- Key age must be present +- These rules must pass for `{ "age": 1 }` + - Key reference must be present +- These rules must pass for `{ "reference": "QSF1234" }` + - Key age must be present diff --git a/tests/integration/issue-619.phpt b/tests/integration/issue-619.phpt index da55f4ae..8992789d 100644 --- a/tests/integration/issue-619.phpt +++ b/tests/integration/issue-619.phpt @@ -9,7 +9,7 @@ use Respect\Validation\Rules\Instance; try { (new Instance('stdClass'))->setTemplate('invalid object')->assert('test'); } catch (ValidationException $exception) { - print_r($exception->getMainMessage()); + print_r($exception->getMessage()); } ?> --EXPECTF-- diff --git a/tests/integration/issue-805.phpt b/tests/integration/issue-805.phpt index c1332cf6..86560a4c 100644 --- a/tests/integration/issue-805.phpt +++ b/tests/integration/issue-805.phpt @@ -15,5 +15,5 @@ try { --EXPECTF-- Array ( - [0] => WRONG EMAIL!!!!!! + [email] => WRONG EMAIL!!!!!! ) diff --git a/tests/unit/Rules/GreaterThanTest.php b/tests/unit/Rules/GreaterThanTest.php index 31f9654e..01614d31 100644 --- a/tests/unit/Rules/GreaterThanTest.php +++ b/tests/unit/Rules/GreaterThanTest.php @@ -19,6 +19,7 @@ use Respect\Validation\Test\Stubs\CountableStub; /** * @group rule * + * @covers \Respect\Validation\Rules\AbstractComparison * @covers \Respect\Validation\Rules\GreaterThan * * @author Henrique Moody @@ -50,6 +51,8 @@ final class GreaterThanTest extends RuleTestCase [new GreaterThan('18 years ago'), '5 days later'], [new GreaterThan('c'), 'a'], [new GreaterThan(new CountableStub(3)), 3], + [new GreaterThan(1900), '2018-01-25'], + [new GreaterThan(10.5), '2018-01-25'], ]; } } diff --git a/tests/unit/Rules/LessThanTest.php b/tests/unit/Rules/LessThanTest.php index 9f63a0cb..e7f489e6 100644 --- a/tests/unit/Rules/LessThanTest.php +++ b/tests/unit/Rules/LessThanTest.php @@ -19,6 +19,7 @@ use Respect\Validation\Test\Stubs\CountableStub; /** * @group rule * + * @covers \Respect\Validation\Rules\AbstractComparison * @covers \Respect\Validation\Rules\LessThan * * @author Henrique Moody @@ -50,6 +51,8 @@ final class LessThanTest extends RuleTestCase [new LessThan('yesterday'), 'tomorrow'], [new LessThan('a'), 'z'], [new LessThan(new CountableStub(5)), 6], + [new LessThan(1900), '2018-01-25'], + [new LessThan(10.5), '2018-01-25'], ]; } } diff --git a/tests/unit/Rules/MaxTest.php b/tests/unit/Rules/MaxTest.php index f2cbe1b0..28beb2c9 100644 --- a/tests/unit/Rules/MaxTest.php +++ b/tests/unit/Rules/MaxTest.php @@ -21,6 +21,7 @@ use Respect\Validation\Test\Stubs\CountableStub; /** * @group rule * + * @covers \Respect\Validation\Rules\AbstractComparison * @covers \Respect\Validation\Rules\Max * * @author Alexandre Gomes Gaigalas diff --git a/tests/unit/Rules/MinTest.php b/tests/unit/Rules/MinTest.php index a2c0c1f3..d2250d1e 100644 --- a/tests/unit/Rules/MinTest.php +++ b/tests/unit/Rules/MinTest.php @@ -21,6 +21,7 @@ use Respect\Validation\Test\Stubs\CountableStub; /** * @group rule * + * @covers \Respect\Validation\Rules\AbstractComparison * @covers \Respect\Validation\Rules\Min * * @author Alexandre Gomes Gaigalas