Skip to content

Commit

Permalink
Rename "Max" to "LessThanOrEqual"
Browse files Browse the repository at this point in the history
Although the name is much longer, it's more explicit what it does. I
confess that after a while without using Validation, even I get confused
about that. Besides, I would like to create another rule with the same
name, but that will behave differently.

Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Feb 27, 2024
1 parent 7658187 commit 2f12b6c
Show file tree
Hide file tree
Showing 18 changed files with 121 additions and 120 deletions.
2 changes: 1 addition & 1 deletion docs/07-comparable-values.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions docs/08-list-of-rules-by-category.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/Between.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ See also:
- [GreaterThanOrEqual](GreaterThanOrEqual.md)
- [Length](Length.md)
- [LessThan](LessThan.md)
- [Max](Max.md)
- [LessThanOrEqual](LessThanOrEqual.md)
2 changes: 1 addition & 1 deletion docs/rules/GreaterThan.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ See also:

- [Between](Between.md)
- [GreaterThanOrEqual](GreaterThanOrEqual.md)
- [Max](Max.md)
- [LessThanOrEqual](LessThanOrEqual.md)
2 changes: 1 addition & 1 deletion docs/rules/GreaterThanOrEqual.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion docs/rules/LessThan.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ See also:

- [Between](Between.md)
- [GreaterThanOrEqual](GreaterThanOrEqual.md)
- [Max](Max.md)
- [LessThanOrEqual](LessThanOrEqual.md)
39 changes: 39 additions & 0 deletions docs/rules/LessThanOrEqual.md
Original file line number Diff line number Diff line change
@@ -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)
38 changes: 0 additions & 38 deletions docs/rules/Max.md

This file was deleted.

2 changes: 1 addition & 1 deletion docs/rules/MaxAge.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/MinAge.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion library/ChainedValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Between.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion library/StaticValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
19 changes: 19 additions & 0 deletions tests/integration/rules/lessThanOrEqual.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--FILE--
<?php

declare(strict_types=1);

require 'vendor/autoload.php';

use Respect\Validation\Validator as v;

exceptionMessage(static fn() => 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"
19 changes: 0 additions & 19 deletions tests/integration/rules/max.phpt

This file was deleted.

50 changes: 50 additions & 0 deletions tests/unit/Rules/LessThanOrEqualTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
* 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<array{LessThanOrEqual, mixed}> */
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<array{LessThanOrEqual, mixed}> */
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'],
];
}
}
50 changes: 0 additions & 50 deletions tests/unit/Rules/MaxTest.php

This file was deleted.

0 comments on commit 2f12b6c

Please sign in to comment.