From 9d0d750a181937b3364e23347506e4ae9d2a0bb9 Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Thu, 31 May 2018 13:18:54 +0200 Subject: [PATCH] Create "LessThan" rule Signed-off-by: Henrique Moody --- docs/LessThan.md | 41 +++++++++++++++++++ docs/VALIDATORS.md | 2 + library/Exceptions/LessThanException.php | 32 +++++++++++++++ library/Rules/LessThan.php | 49 ++++++++++++++++++++++ library/Validator.php | 1 + tests/integration/rules/lessThan.phpt | 37 +++++++++++++++++ tests/unit/Rules/LessThanTest.php | 52 ++++++++++++++++++++++++ 7 files changed, 214 insertions(+) create mode 100644 docs/LessThan.md create mode 100644 library/Exceptions/LessThanException.php create mode 100644 library/Rules/LessThan.php create mode 100644 tests/integration/rules/lessThan.phpt create mode 100644 tests/unit/Rules/LessThanTest.php diff --git a/docs/LessThan.md b/docs/LessThan.md new file mode 100644 index 00000000..b9affc36 --- /dev/null +++ b/docs/LessThan.md @@ -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) diff --git a/docs/VALIDATORS.md b/docs/VALIDATORS.md index d08ab530..0f770970 100644 --- a/docs/VALIDATORS.md +++ b/docs/VALIDATORS.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) diff --git a/library/Exceptions/LessThanException.php b/library/Exceptions/LessThanException.php new file mode 100644 index 00000000..44d9b1f2 --- /dev/null +++ b/library/Exceptions/LessThanException.php @@ -0,0 +1,32 @@ + + * + * 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 + */ +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}}', + ], + ]; +} diff --git a/library/Rules/LessThan.php b/library/Rules/LessThan.php new file mode 100644 index 00000000..7962b764 --- /dev/null +++ b/library/Rules/LessThan.php @@ -0,0 +1,49 @@ + + * + * 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 + */ +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); + } +} diff --git a/library/Validator.php b/library/Validator.php index 7cb1c94e..b9faa4cc 100644 --- a/library/Validator.php +++ b/library/Validator.php @@ -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) diff --git a/tests/integration/rules/lessThan.phpt b/tests/integration/rules/lessThan.phpt new file mode 100644 index 00000000..8b0675b5 --- /dev/null +++ b/tests/integration/rules/lessThan.phpt @@ -0,0 +1,37 @@ +--FILE-- +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" diff --git a/tests/unit/Rules/LessThanTest.php b/tests/unit/Rules/LessThanTest.php new file mode 100644 index 00000000..afde1f8a --- /dev/null +++ b/tests/unit/Rules/LessThanTest.php @@ -0,0 +1,52 @@ + + * + * 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 + */ +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'], + ]; + } +}