mirror of
https://github.com/Respect/Validation.git
synced 2024-06-29 10:40:24 +02:00
Create "LessThan" rule
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
0a031649a8
commit
9d0d750a18
41
docs/LessThan.md
Normal file
41
docs/LessThan.md
Normal file
|
@ -0,0 +1,41 @@
|
|||
# LessThan
|
||||
|
||||
- `LessThan(mixed $compareTo)`
|
||||
|
||||
Validates whether the input is less than a value.
|
||||
|
||||
```php
|
||||
v::lessThan(10)->validate(9); // true
|
||||
v::lessThan(10)->validate(10); // false
|
||||
```
|
||||
|
||||
You can also validate:
|
||||
|
||||
```php
|
||||
// Dates
|
||||
v::dateTime()->lessThan('2010-01-01')->validate('2000-01-01'); // true
|
||||
v::dateTime()->lessThan('2010-01-01')->validate('2020-01-01'); // false
|
||||
|
||||
// Date intervals
|
||||
v::dateTime()->lessThan('today')->validate('3 days ago'); // true
|
||||
v::dateTime()->lessThan('yesterday')->validate('tomorrow'); // false
|
||||
|
||||
// Single character strings
|
||||
v::dateTime()->lessThan('b')->validate('a'); // true
|
||||
v::dateTime()->lessThan('a')->validate('z'); // false
|
||||
```
|
||||
|
||||
Message template for this validator includes `{{compareTo}}`.
|
||||
|
||||
## Changelog
|
||||
|
||||
Version | Description
|
||||
--------|-------------
|
||||
2.0.0 | Created
|
||||
|
||||
***
|
||||
See also:
|
||||
|
||||
- [Between](Between.md)
|
||||
- [Max](Max.md)
|
||||
- [Min](Min.md)
|
|
@ -45,6 +45,7 @@
|
|||
- [Between](Between.md)
|
||||
- [Equals](Equals.md)
|
||||
- [Identical](Identical.md)
|
||||
- [LessThan](LessThan.md)
|
||||
- [Max](Max.md)
|
||||
- [Min](Min.md)
|
||||
|
||||
|
@ -283,6 +284,7 @@
|
|||
- [LeapDate](LeapDate.md)
|
||||
- [LeapYear](LeapYear.md)
|
||||
- [Length](Length.md)
|
||||
- [LessThan](LessThan.md)
|
||||
- [Lowercase](Lowercase.md)
|
||||
- [Luhn](Luhn.md)
|
||||
- [MacAddress](MacAddress.md)
|
||||
|
|
32
library/Exceptions/LessThanException.php
Normal file
32
library/Exceptions/LessThanException.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 LessThanException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be less than {{compareTo}}',
|
||||
],
|
||||
self::MODE_NEGATIVE => [
|
||||
self::STANDARD => '{{name}} must not be less than {{compareTo}}',
|
||||
],
|
||||
];
|
||||
}
|
49
library/Rules/LessThan.php
Normal file
49
library/Rules/LessThan.php
Normal file
|
@ -0,0 +1,49 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Validates whether the input is less than a value.
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class LessThan extends AbstractRule
|
||||
{
|
||||
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
|
||||
{
|
||||
return $this->toComparable($input) < $this->toComparable($this->compareTo);
|
||||
}
|
||||
}
|
|
@ -94,6 +94,7 @@ use Respect\Validation\Rules\Key;
|
|||
* @method static Validator leapYear()
|
||||
* @method static Validator length(int $min = null, int $max = null, bool $inclusive = true)
|
||||
* @method static Validator lowercase()
|
||||
* @method static Validator lessThan($compareTo)
|
||||
* @method static Validator luhn()
|
||||
* @method static Validator macAddress()
|
||||
* @method static Validator max($maxValue, bool $inclusive = true)
|
||||
|
|
37
tests/integration/rules/lessThan.phpt
Normal file
37
tests/integration/rules/lessThan.phpt
Normal file
|
@ -0,0 +1,37 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\LessThanException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::lessThan(12)->check(21);
|
||||
} catch (LessThanException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::lessThan('today'))->check('yesterday');
|
||||
} catch (LessThanException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::lessThan('1988-09-09')->assert('2018-09-09');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::lessThan('b'))->assert('a');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
21 must be less than 12
|
||||
"yesterday" must not be less than "today"
|
||||
- "2018-09-09" must be less than "1988-09-09"
|
||||
- "a" must not be less than "b"
|
52
tests/unit/Rules/LessThanTest.php
Normal file
52
tests/unit/Rules/LessThanTest.php
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?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\Test\RuleTestCase;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\LessThan
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class LessThanTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
return [
|
||||
[new LessThan(10), 9],
|
||||
[new LessThan('2010-01-01'), '2000-01-01'],
|
||||
[new LessThan('today'), '3 days ago'],
|
||||
[new LessThan('b'), 'a'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
return [
|
||||
[new LessThan(10), 10],
|
||||
[new LessThan('2010-01-01'), '2020-01-01'],
|
||||
[new LessThan('yesterday'), 'tomorrow'],
|
||||
[new LessThan('a'), 'z'],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue