Create "GreaterThan" rule

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2018-05-31 13:50:58 +02:00
parent 9d0d750a18
commit 9b20d6dae2
No known key found for this signature in database
GPG key ID: 221E9281655813A6
7 changed files with 210 additions and 0 deletions

41
docs/GreaterThan.md Normal file
View file

@ -0,0 +1,41 @@
# GreaterThan
- `GreaterThan(mixed $compareTo)`
Validates whether the input is greater than a value.
```php
v::greaterThan(10)->validate(11); // true
v::greaterThan(10)->validate(9); // false
```
You can also validate:
```php
// Dates
v::dateTime()->greaterThan('2010-01-01')->validate('2020-01-01'); // true
v::dateTime()->greaterThan('2010-01-01')->validate('2000-01-01'); // false
// Date intervals
v::dateTime()->greaterThan('yesterday')->validate('now'); // true
v::dateTime()->greaterThan('18 years ago')->validate('5 days later'); // false
// Single character strings
v::dateTime()->greaterThan('A')->validate('B'); // true
v::dateTime()->greaterThan('c')->validate('a'); // 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)

View file

@ -44,6 +44,7 @@
- [Age](Age.md)
- [Between](Between.md)
- [Equals](Equals.md)
- [GreaterThan](GreaterThan.md)
- [Identical](Identical.md)
- [LessThan](LessThan.md)
- [Max](Max.md)
@ -262,6 +263,7 @@
- [FloatType](FloatType.md)
- [FloatVal](FloatVal.md)
- [Graph](Graph.md)
- [GreaterThan](GreaterThan.md)
- [HexRgbColor](HexRgbColor.md)
- [INSTALL](INSTALL.md)
- [Identical](Identical.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 GreaterThanException extends ValidationException
{
/**
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be greater than {{compareTo}}',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be greater than {{compareTo}}',
],
];
}

View 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 GreaterThan 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);
}
}

View file

@ -72,6 +72,7 @@ use Respect\Validation\Rules\Key;
* @method static Validator floatVal()
* @method static Validator floatType()
* @method static Validator graph(string $additionalChars = null)
* @method static Validator greaterThan($compareTo)
* @method static Validator hexRgbColor()
* @method static Validator identical($value)
* @method static Validator identityCard(string $countryCode)

View file

@ -0,0 +1,33 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\GreaterThanException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::greaterThan(12)->check(21);
} catch (GreaterThanException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::greaterThan('today'))->check('yesterday');
} catch (GreaterThanException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::greaterThan('1988-09-09')->assert('2018-09-09');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::greaterThan('b'))->assert('a');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--

View 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\GreaterThan
*
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class GreaterThanTest extends RuleTestCase
{
/**
* {@inheritdoc}
*/
public function providerForValidInput(): array
{
return [
[new GreaterThan(10), 11],
[new GreaterThan('2010-01-01'), '2020-01-01'],
[new GreaterThan('yesterday'), 'now'],
[new GreaterThan('A'), 'B'],
];
}
/**
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{
return [
[new GreaterThan(10), 9],
[new GreaterThan('2010-01-01'), '2000-01-01'],
[new GreaterThan('18 years ago'), '5 days later'],
[new GreaterThan('c'), 'a'],
];
}
}