mirror of
https://github.com/Respect/Validation.git
synced 2024-05-29 11:52:34 +02:00
Create "Equivalent" rule
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
4017e5aaa2
commit
2ab1f11c99
|
@ -33,4 +33,5 @@ See also:
|
|||
|
||||
- [StartsWith](StartsWith.md)
|
||||
- [EndsWith](EndsWith.md)
|
||||
- [Equivalent](Equivalent.md)
|
||||
- [In](In.md)
|
||||
|
|
|
@ -21,4 +21,5 @@ Version | Description
|
|||
See also:
|
||||
|
||||
- [Contains](Contains.md)
|
||||
- [Equivalent](Equivalent.md)
|
||||
- [Identical](Identical.md)
|
||||
|
|
29
docs/Equivalent.md
Normal file
29
docs/Equivalent.md
Normal 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)
|
|
@ -21,4 +21,5 @@ Version | Description
|
|||
See also:
|
||||
|
||||
- [Contains](Contains.md)
|
||||
- [Equivalent](Equivalent.md)
|
||||
- [Equals](Equals.md)
|
||||
|
|
|
@ -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)
|
||||
|
|
32
library/Exceptions/EquivalentException.php
Normal file
32
library/Exceptions/EquivalentException.php
Normal 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}}',
|
||||
],
|
||||
];
|
||||
}
|
61
library/Rules/Equivalent.php
Normal file
61
library/Rules/Equivalent.php
Normal 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);
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
|
|
37
tests/integration/rules/equivalent.phpt
Normal file
37
tests/integration/rules/equivalent.phpt
Normal 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`
|
54
tests/unit/Rules/EquivalentTest.php
Normal file
54
tests/unit/Rules/EquivalentTest.php
Normal 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]],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue