-
Notifications
You must be signed in to change notification settings - Fork 771
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
With this rule, we introduce a new type of rule, which is only possible due to the changes in the validation engine. Signed-off-by: Henrique Moody <[email protected]>
- Loading branch information
1 parent
2f12b6c
commit cc96ee9
Showing
15 changed files
with
352 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Min | ||
|
||
- `Min(Validatable $rule)` | ||
|
||
Validates the minimum value of the input against a given rule. | ||
|
||
```php | ||
v::min(v::equals(10))->validate([10, 20, 30]); // true | ||
|
||
v::min(v::between('a', 'c'))->validate(['b', 'd', 'f']); // true | ||
|
||
v::min(v::greaterThan(new DateTime('yesterday'))) | ||
->validate([new DateTime('today'), new DateTime('tomorrow')]); // true | ||
|
||
v::min(v::lessThan(3))->validate([4, 8, 12]); // false | ||
``` | ||
|
||
## Note | ||
|
||
This rule uses PHP's [min][] function to compare the input against the given rule. The PHP manual states that: | ||
|
||
> Values of different types will be compared using the [standard comparison rules][]. For instance, a non-numeric | ||
> `string` will be compared to an `int` as though it were `0`, but multiple non-numeric `string` values will be compared | ||
> alphanumerically. The actual value returned will be of the original type with no conversion applied. | ||
## Categorization | ||
|
||
- Aggregations | ||
- Comparisons | ||
|
||
## Changelog | ||
|
||
| Version | Description | | ||
|--------:|-----------------------------| | ||
| 3.0.0 | Became an aggregation | | ||
| 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) | ||
- [LessThanOrEqual](LessThanOrEqual.md) | ||
|
||
[min]: https://www.php.net/min | ||
[standard comparison rules]: https://www.php.net/operators.comparison |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Validation\Rules; | ||
|
||
use Respect\Validation\Message\Template; | ||
use Respect\Validation\Result; | ||
|
||
use function count; | ||
use function is_array; | ||
use function is_iterable; | ||
use function iterator_to_array; | ||
use function min; | ||
|
||
#[Template('As the minimum from {{name}},', 'As the minimum from {{name}},')] | ||
#[Template('The minimum from', 'The minimum from', self::TEMPLATE_NAMED)] | ||
#[Template('{{name}} must have at least 1 item', '{{name}} must not have at least 1 item', self::TEMPLATE_EMPTY)] | ||
#[Template( | ||
'{{name}} must be an array or iterable to validate its minimum value', | ||
'{{name}} must not be an array or iterable to validate its minimum value', | ||
self::TEMPLATE_TYPE, | ||
)] | ||
final class Min extends Wrapper | ||
{ | ||
public const TEMPLATE_NAMED = '__named__'; | ||
public const TEMPLATE_EMPTY = '__empty__'; | ||
public const TEMPLATE_TYPE = '__min__'; | ||
|
||
public function evaluate(mixed $input): Result | ||
{ | ||
if (!is_iterable($input)) { | ||
return Result::failed($input, $this); | ||
} | ||
|
||
$array = $this->toArray($input); | ||
if (count($array) === 0) { | ||
return Result::failed($input, $this); | ||
} | ||
|
||
$result = $this->rule->evaluate(min($array)); | ||
$template = $this->getName() === null ? self::TEMPLATE_STANDARD : self::TEMPLATE_NAMED; | ||
|
||
return (new Result($result->isValid, $input, $this, [], $template,))->withNextSibling($result); | ||
} | ||
|
||
/** | ||
* @param iterable<mixed> $input | ||
* @return array<mixed> | ||
*/ | ||
private function toArray(iterable $input): array | ||
{ | ||
if (is_array($input)) { | ||
return $input; | ||
} | ||
|
||
return iterator_to_array($input); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
use Respect\Validation\Validator as v; | ||
|
||
run([ | ||
'Default' => [v::min(v::equals(1)), [2, 3]], | ||
'Negative' => [v::not(v::min(v::equals(1))), [1, 2, 3]], | ||
'With template' => [v::min(v::equals(1)), [2, 3], 'That did not go as planned'], | ||
'With name' => [v::min(v::equals(1))->setName('Options'), [2, 3]], | ||
]); | ||
?> | ||
--EXPECT-- | ||
Default | ||
⎺⎺⎺⎺⎺⎺⎺ | ||
As the minimum from `[2, 3]`, 2 must equal 1 | ||
- As the minimum from `[2, 3]`, 2 must equal 1 | ||
[ | ||
'min' => 'As the minimum from `[2, 3]`, 2 must equal 1', | ||
] | ||
|
||
Negative | ||
⎺⎺⎺⎺⎺⎺⎺⎺ | ||
As the minimum from `[1, 2, 3]`, 1 must not equal 1 | ||
- As the minimum from `[1, 2, 3]`, 1 must not equal 1 | ||
[ | ||
'min' => 'As the minimum from `[1, 2, 3]`, 1 must not equal 1', | ||
] | ||
|
||
With template | ||
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺ | ||
That did not go as planned | ||
- That did not go as planned | ||
[ | ||
'min' => 'That did not go as planned', | ||
] | ||
|
||
With name | ||
⎺⎺⎺⎺⎺⎺⎺⎺⎺ | ||
The minimum from Options must equal 1 | ||
- The minimum from Options must equal 1 | ||
[ | ||
'Options' => 'The minimum from Options must equal 1', | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.