diff --git a/docs/07-comparable-values.md b/docs/07-comparable-values.md index b572898fe..5fb6d4c93 100644 --- a/docs/07-comparable-values.md +++ b/docs/07-comparable-values.md @@ -22,7 +22,7 @@ v::dateTime() ->between(new DateTime('yesterday'), new DateTime('tomorrow')) ->validate(new DateTime('now')); // true -v::numericVal()->max(10)->validate(5); // true +v::numericVal()->lessThanOrEqual(10)->validate(5); // true v::stringVal()->between('a', 'f')->validate('d'); // true diff --git a/docs/08-list-of-rules-by-category.md b/docs/08-list-of-rules-by-category.md index 0b8227932..d1798d7f7 100644 --- a/docs/08-list-of-rules-by-category.md +++ b/docs/08-list-of-rules-by-category.md @@ -50,7 +50,7 @@ - [Identical](rules/Identical.md) - [In](rules/In.md) - [LessThan](rules/LessThan.md) -- [Max](rules/Max.md) +- [LessThanOrEqual](rules/LessThanOrEqual.md) ## Composite @@ -346,10 +346,10 @@ - [LeapYear](rules/LeapYear.md) - [Length](rules/Length.md) - [LessThan](rules/LessThan.md) +- [LessThanOrEqual](rules/LessThanOrEqual.md) - [Lowercase](rules/Lowercase.md) - [Luhn](rules/Luhn.md) - [MacAddress](rules/MacAddress.md) -- [Max](rules/Max.md) - [MaxAge](rules/MaxAge.md) - [Mimetype](rules/Mimetype.md) - [MinAge](rules/MinAge.md) diff --git a/docs/rules/Between.md b/docs/rules/Between.md index 51a548356..176d5298b 100644 --- a/docs/rules/Between.md +++ b/docs/rules/Between.md @@ -35,4 +35,4 @@ See also: - [GreaterThanOrEqual](GreaterThanOrEqual.md) - [Length](Length.md) - [LessThan](LessThan.md) -- [Max](Max.md) +- [LessThanOrEqual](LessThanOrEqual.md) diff --git a/docs/rules/GreaterThan.md b/docs/rules/GreaterThan.md index 1d345ae1d..6bb209985 100644 --- a/docs/rules/GreaterThan.md +++ b/docs/rules/GreaterThan.md @@ -29,4 +29,4 @@ See also: - [Between](Between.md) - [GreaterThanOrEqual](GreaterThanOrEqual.md) -- [Max](Max.md) +- [LessThanOrEqual](LessThanOrEqual.md) diff --git a/docs/rules/GreaterThanOrEqual.md b/docs/rules/GreaterThanOrEqual.md index 6b8fba3db..1563736b6 100644 --- a/docs/rules/GreaterThanOrEqual.md +++ b/docs/rules/GreaterThanOrEqual.md @@ -35,6 +35,6 @@ See also: - [GreaterThan](GreaterThan.md) - [Length](Length.md) - [LessThan](LessThan.md) -- [Max](Max.md) +- [LessThanOrEqual](LessThanOrEqual.md) - [MaxAge](MaxAge.md) - [MinAge](MinAge.md) diff --git a/docs/rules/LessThan.md b/docs/rules/LessThan.md index 54ccfc039..c10f59a16 100644 --- a/docs/rules/LessThan.md +++ b/docs/rules/LessThan.md @@ -29,4 +29,4 @@ See also: - [Between](Between.md) - [GreaterThanOrEqual](GreaterThanOrEqual.md) -- [Max](Max.md) +- [LessThanOrEqual](LessThanOrEqual.md) diff --git a/docs/rules/LessThanOrEqual.md b/docs/rules/LessThanOrEqual.md new file mode 100644 index 000000000..046fb0d10 --- /dev/null +++ b/docs/rules/LessThanOrEqual.md @@ -0,0 +1,39 @@ +# LessThanOrEqual + +- `LessThanOrEqual(mixed $compareTo)` + +Validates whether the input is less than or equal to a value. + +```php +v::lessThanOrEqual(10)->validate(9); // true +v::lessThanOrEqual(10)->validate(10); // true +v::lessThanOrEqual(10)->validate(11); // false +``` + +Validation makes comparison easier, check out our supported +[comparable values](../07-comparable-values.md). + +Message template for this validator includes `{{compareTo}}`. + +## Categorization + +- Comparisons + +## Changelog + +| Version | Description | +|--------:|-----------------------------------------| +| 3.0.0 | Renamed from "Max" to "LessThanOrEqual" | +| 2.0.0 | Became always inclusive | +| 1.0.0 | Became inclusive by default | +| 0.3.9 | Created | + +*** +See also: + +- [Between](Between.md) +- [GreaterThan](GreaterThan.md) +- [GreaterThanOrEqual](GreaterThanOrEqual.md) +- [LessThan](LessThan.md) +- [MaxAge](MaxAge.md) +- [MinAge](MinAge.md) diff --git a/docs/rules/Max.md b/docs/rules/Max.md deleted file mode 100644 index ddef3a838..000000000 --- a/docs/rules/Max.md +++ /dev/null @@ -1,38 +0,0 @@ -# Max - -- `Max(mixed $compareTo)` - -Validates whether the input is less than or equal to a value. - -```php -v::max(10)->validate(9); // true -v::max(10)->validate(10); // true -v::max(10)->validate(11); // false -``` - -Validation makes comparison easier, check out our supported -[comparable values](../07-comparable-values.md). - -Message template for this validator includes `{{compareTo}}`. - -## Categorization - -- Comparisons - -## Changelog - -Version | Description ---------|------------- - 2.0.0 | Became always inclusive - 1.0.0 | Became inclusive by default - 0.3.9 | Created - -*** -See also: - -- [Between](Between.md) -- [GreaterThan](GreaterThan.md) -- [GreaterThanOrEqual](GreaterThanOrEqual.md) -- [LessThan](LessThan.md) -- [MaxAge](MaxAge.md) -- [MinAge](MinAge.md) diff --git a/docs/rules/MaxAge.md b/docs/rules/MaxAge.md index 63822bc70..4f88d9e57 100644 --- a/docs/rules/MaxAge.md +++ b/docs/rules/MaxAge.md @@ -33,7 +33,7 @@ See also: - [Date](Date.md) - [GreaterThanOrEqual](GreaterThanOrEqual.md) -- [Max](Max.md) +- [LessThanOrEqual](LessThanOrEqual.md) - [MinAge](MinAge.md) [date()]: http://php.net/date diff --git a/docs/rules/MinAge.md b/docs/rules/MinAge.md index 62e71152e..d2c4bfcee 100644 --- a/docs/rules/MinAge.md +++ b/docs/rules/MinAge.md @@ -34,7 +34,7 @@ See also: - [Date](Date.md) - [DateTime](DateTime.md) - [GreaterThanOrEqual](GreaterThanOrEqual.md) -- [Max](Max.md) +- [LessThanOrEqual](LessThanOrEqual.md) - [MaxAge](MaxAge.md) [date()]: http://php.net/date diff --git a/library/ChainedValidator.php b/library/ChainedValidator.php index 38b1d84ff..38d00e13d 100644 --- a/library/ChainedValidator.php +++ b/library/ChainedValidator.php @@ -194,7 +194,7 @@ public function luhn(): ChainedValidator; public function macAddress(): ChainedValidator; - public function max(mixed $compareTo): ChainedValidator; + public function lessThanOrEqual(mixed $compareTo): ChainedValidator; public function maxAge(int $age, ?string $format = null): ChainedValidator; diff --git a/library/Rules/Between.php b/library/Rules/Between.php index 4558beda6..402909b96 100644 --- a/library/Rules/Between.php +++ b/library/Rules/Between.php @@ -30,7 +30,7 @@ public function __construct(mixed $minValue, mixed $maxValue) parent::__construct( new AllOf( new GreaterThanOrEqual($minValue), - new Max($maxValue) + new LessThanOrEqual($maxValue) ), [ 'minValue' => $minValue, diff --git a/library/Rules/Max.php b/library/Rules/LessThanOrEqual.php similarity index 90% rename from library/Rules/Max.php rename to library/Rules/LessThanOrEqual.php index 976f998a3..859209ed8 100644 --- a/library/Rules/Max.php +++ b/library/Rules/LessThanOrEqual.php @@ -15,7 +15,7 @@ '{{name}} must be less than or equal to {{compareTo}}', '{{name}} must not be less than or equal to {{compareTo}}', )] -final class Max extends Comparison +final class LessThanOrEqual extends Comparison { protected function compare(mixed $left, mixed $right): bool { diff --git a/library/StaticValidator.php b/library/StaticValidator.php index e19fde05f..ed0ae851e 100644 --- a/library/StaticValidator.php +++ b/library/StaticValidator.php @@ -196,7 +196,7 @@ public static function luhn(): ChainedValidator; public static function macAddress(): ChainedValidator; - public static function max(mixed $compareTo): ChainedValidator; + public static function lessThanOrEqual(mixed $compareTo): ChainedValidator; public static function maxAge(int $age, ?string $format = null): ChainedValidator; diff --git a/tests/integration/rules/lessThanOrEqual.phpt b/tests/integration/rules/lessThanOrEqual.phpt new file mode 100644 index 000000000..9f4adf27b --- /dev/null +++ b/tests/integration/rules/lessThanOrEqual.phpt @@ -0,0 +1,19 @@ +--FILE-- + v::lessThanOrEqual(10)->check(11)); +exceptionMessage(static fn() => v::not(v::lessThanOrEqual(10))->check(5)); +exceptionFullMessage(static fn() => v::lessThanOrEqual('today')->assert('tomorrow')); +exceptionFullMessage(static fn() => v::not(v::lessThanOrEqual('b'))->assert('a')); +?> +--EXPECT-- +11 must be less than or equal to 10 +5 must not be less than or equal to 10 +- "tomorrow" must be less than or equal to "today" +- "a" must not be less than or equal to "b" diff --git a/tests/integration/rules/max.phpt b/tests/integration/rules/max.phpt deleted file mode 100644 index b15c63278..000000000 --- a/tests/integration/rules/max.phpt +++ /dev/null @@ -1,19 +0,0 @@ ---FILE-- - v::max(10)->check(11)); -exceptionMessage(static fn() => v::not(v::max(10))->check(5)); -exceptionFullMessage(static fn() => v::max('today')->assert('tomorrow')); -exceptionFullMessage(static fn() => v::not(v::max('b'))->assert('a')); -?> ---EXPECT-- -11 must be less than or equal to 10 -5 must not be less than or equal to 10 -- "tomorrow" must be less than or equal to "today" -- "a" must not be less than or equal to "b" diff --git a/tests/unit/Rules/LessThanOrEqualTest.php b/tests/unit/Rules/LessThanOrEqualTest.php new file mode 100644 index 000000000..a46014927 --- /dev/null +++ b/tests/unit/Rules/LessThanOrEqualTest.php @@ -0,0 +1,50 @@ + + * SPDX-License-Identifier: MIT + */ + +declare(strict_types=1); + +namespace Respect\Validation\Rules; + +use DateTime; +use DateTimeImmutable; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Group; +use Respect\Validation\Test\RuleTestCase; +use Respect\Validation\Test\Stubs\CountableStub; + +#[Group('rule')] +#[CoversClass(LessThanOrEqual::class)] +final class LessThanOrEqualTest extends RuleTestCase +{ + /** @return iterable */ + public static function providerForValidInput(): iterable + { + return [ + [new LessThanOrEqual(10), 9], + [new LessThanOrEqual(10), 10], + [new LessThanOrEqual('2010-01-01'), '2000-01-01'], + [new LessThanOrEqual(new DateTime('today')), new DateTimeImmutable('yesterday')], + [new LessThanOrEqual('18 years ago'), '1988-09-09'], + [new LessThanOrEqual('z'), 'a'], + [new LessThanOrEqual(new CountableStub(3)), 2], + ]; + } + + /** @return iterable */ + public static function providerForInvalidInput(): iterable + { + return [ + [new LessThanOrEqual(10), 11], + [new LessThanOrEqual(new DateTimeImmutable('today')), new DateTime('tomorrow')], + [new LessThanOrEqual('now'), '+1 minute'], + [new LessThanOrEqual('B'), 'C'], + [new LessThanOrEqual(new CountableStub(3)), 4], + [new LessThanOrEqual(1900), '2018-01-25'], + [new LessThanOrEqual(10.5), '2018-01-25'], + ]; + } +} diff --git a/tests/unit/Rules/MaxTest.php b/tests/unit/Rules/MaxTest.php deleted file mode 100644 index ec9ae8f2c..000000000 --- a/tests/unit/Rules/MaxTest.php +++ /dev/null @@ -1,50 +0,0 @@ - - * SPDX-License-Identifier: MIT - */ - -declare(strict_types=1); - -namespace Respect\Validation\Rules; - -use DateTime; -use DateTimeImmutable; -use PHPUnit\Framework\Attributes\CoversClass; -use PHPUnit\Framework\Attributes\Group; -use Respect\Validation\Test\RuleTestCase; -use Respect\Validation\Test\Stubs\CountableStub; - -#[Group('rule')] -#[CoversClass(Max::class)] -final class MaxTest extends RuleTestCase -{ - /** @return iterable */ - public static function providerForValidInput(): iterable - { - return [ - [new Max(10), 9], - [new Max(10), 10], - [new Max('2010-01-01'), '2000-01-01'], - [new Max(new DateTime('today')), new DateTimeImmutable('yesterday')], - [new Max('18 years ago'), '1988-09-09'], - [new Max('z'), 'a'], - [new Max(new CountableStub(3)), 2], - ]; - } - - /** @return iterable */ - public static function providerForInvalidInput(): iterable - { - return [ - [new Max(10), 11], - [new Max(new DateTimeImmutable('today')), new DateTime('tomorrow')], - [new Max('now'), '+1 minute'], - [new Max('B'), 'C'], - [new Max(new CountableStub(3)), 4], - [new Max(1900), '2018-01-25'], - [new Max(10.5), '2018-01-25'], - ]; - } -}