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 <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2018-07-09 09:24:08 +02:00
parent 57509520a4
commit 0cdd8c4546
No known key found for this signature in database
GPG key ID: 221E9281655813A6
18 changed files with 131 additions and 133 deletions

View file

@ -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());
}

View file

@ -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);
}
}

View file

@ -0,0 +1,66 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* 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 <henriquemoody@gmail.com>
*/
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;
}

View file

@ -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);
}
}
}

View file

@ -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 <henriquemoody@gmail.com>
*/
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;
}
}

View file

@ -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 <henriquemoody@gmail.com>
*/
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;
}
}

View file

@ -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 <alexandre@gaigalas.net>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
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;
}
}

View file

@ -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 <alexandre@gaigalas.net>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
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;
}
}

View file

@ -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);
}
/**

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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--

View file

@ -15,5 +15,5 @@ try {
--EXPECTF--
Array
(
[0] => WRONG EMAIL!!!!!!
[email] => WRONG EMAIL!!!!!!
)

View file

@ -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 <henriquemoody@gmail.com>
@ -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'],
];
}
}

View file

@ -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 <henriquemoody@gmail.com>
@ -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'],
];
}
}

View file

@ -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 <alexandre@gaigalas.net>

View file

@ -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 <alexandre@gaigalas.net>