Create "Equivalent" rule

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2018-06-23 16:24:48 +02:00
parent 4017e5aaa2
commit 2ab1f11c99
No known key found for this signature in database
GPG key ID: 221E9281655813A6
10 changed files with 218 additions and 0 deletions

View file

@ -33,4 +33,5 @@ See also:
- [StartsWith](StartsWith.md)
- [EndsWith](EndsWith.md)
- [Equivalent](Equivalent.md)
- [In](In.md)

View file

@ -21,4 +21,5 @@ Version | Description
See also:
- [Contains](Contains.md)
- [Equivalent](Equivalent.md)
- [Identical](Identical.md)

29
docs/Equivalent.md Normal file
View file

@ -0,0 +1,29 @@
# Equivalent
- `Equivalent(mixed $compareTo)`
Validates if the input is equivalent to some value.
```php
v::equivalent(1)->validate(true); // true
v::equivalent('Something')->validate('someThing'); // true
v::equivalent(new ArrayObject([1, 2, 3, 4, 5]))->validate(new ArrayObject([1, 2, 3, 4, 5])); // true
```
This rule is very similar to [Equals](Equals.md) but it does not make case-sensitive
comparisons.
Message template for this validator includes `{{compareTo}}`.
## Changelog
Version | Description
--------|-------------
2.0.0 | Created
***
See also:
- [Contains](Contains.md)
- [Equals](Equals.md)
- [Identical](Identical.md)

View file

@ -21,4 +21,5 @@ Version | Description
See also:
- [Contains](Contains.md)
- [Equivalent](Equivalent.md)
- [Equals](Equals.md)

View file

@ -44,6 +44,7 @@
- [Age](Age.md)
- [Between](Between.md)
- [Equals](Equals.md)
- [Equivalent](Equivalent.md)
- [GreaterThan](GreaterThan.md)
- [Identical](Identical.md)
- [LessThan](LessThan.md)

View file

@ -0,0 +1,32 @@
<?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\Exceptions;
/**
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class EquivalentException extends ValidationException
{
/**
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be equivalent to {{compareTo}}',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be equivalent to {{compareTo}}',
],
];
}

View file

@ -0,0 +1,61 @@
<?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 function is_scalar;
use function mb_strtoupper;
/**
* Validates if the input is equivalent to some value.
*
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class Equivalent extends AbstractRule
{
/**
* @var mixed
*/
private $compareTo;
/**
* Initializes the rule.
*
* @param mixed $compareTo
*/
public function __construct($compareTo)
{
$this->compareTo = $compareTo;
}
/**
* {@inheritdoc}
*/
public function validate($input): bool
{
if (is_scalar($input)) {
return $this->isStringEquivalent((string) $input);
}
return $input == $this->compareTo;
}
private function isStringEquivalent(string $input): bool
{
if (!is_scalar($this->compareTo)) {
return false;
}
return mb_strtoupper((string) $input) === mb_strtoupper((string) $this->compareTo);
}
}

View file

@ -59,6 +59,7 @@ use Respect\Validation\Rules\Key;
* @method static Validator email()
* @method static Validator endsWith($endValue, bool $identical = false)
* @method static Validator equals($compareTo)
* @method static Validator equivalent($compareTo)
* @method static Validator even()
* @method static Validator executable()
* @method static Validator exists()

View file

@ -0,0 +1,37 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\EquivalentException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::equivalent(true)->check(false);
} catch (EquivalentException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::equivalent('Something'))->check('someThing');
} catch (EquivalentException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::equivalent(123)->assert('true');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::equivalent(true))->assert(1);
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
`FALSE` must be equivalent to `TRUE`
"someThing" must not be equivalent to "Something"
- "true" must be equivalent to 123
- 1 must not be equivalent to `TRUE`

View file

@ -0,0 +1,54 @@
<?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 ArrayObject;
use Respect\Validation\Test\RuleTestCase;
/**
* @group rule
*
* @covers \Respect\Validation\Rules\Equivalent
*
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class EquivalentTest extends RuleTestCase
{
/**
* {@inheritdoc}
*/
public function providerForValidInput(): array
{
return [
[new Equivalent(1), true],
[new Equivalent('Something'), 'something'],
[new Equivalent([1, 2, 3]), [1, 2, 3]],
[new Equivalent((object) ['foo' => 'bar']), (object) ['foo' => 'bar']],
[new Equivalent(new ArrayObject([1, 2, 3, 4, 5])), new ArrayObject([1, 2, 3, 4, 5])],
];
}
/**
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{
return [
[new Equivalent(1), false],
[new Equivalent('Something'), 'something else'],
[new Equivalent([1, 2, 3]), [1, 2, 3, 4]],
[new Equivalent((object) ['foo' => 'bar']), (object) ['foo' => 42]],
];
}
}