-
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.
Update the validation engine of the "Each" rule
These changes will also introduce an abstract rule that validates non-empty-iterable values. The abstract rule can also be the parent of the recently created "Min" rule. Therefore, I've changed that class too. I've introduced many tests for the "Each" rule to make sure what its expected behavior is. I'm not super happy with its output, but I tried a couple of options, and it is the best choice. Note that Each now rejects `stdClass` and empty iterable values. I thought that would make sense, as it would be useless when the input is empty. Signed-off-by: Henrique Moody <[email protected]>
- Loading branch information
1 parent
210aa4a
commit 433ceb4
Showing
10 changed files
with
431 additions
and
215 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Validation\Rules\Core; | ||
|
||
use Respect\Validation\Helpers\CanBindEvaluateRule; | ||
use Respect\Validation\Result; | ||
use Respect\Validation\Rules\IterableType; | ||
use Respect\Validation\Rules\NotEmpty; | ||
use Respect\Validation\Rules\Wrapper; | ||
|
||
use function is_array; | ||
use function iterator_to_array; | ||
use function lcfirst; | ||
use function strrchr; | ||
use function substr; | ||
|
||
abstract class FilteredNonEmptyArray extends Wrapper | ||
{ | ||
use CanBindEvaluateRule; | ||
|
||
/** @param non-empty-array<mixed> $input */ | ||
abstract protected function evaluateNonEmptyArray(array $input): Result; | ||
|
||
public function evaluate(mixed $input): Result | ||
{ | ||
$id = $this->rule->getName() ?? $this->getName() ?? lcfirst(substr((string) strrchr(static::class, '\\'), 1)); | ||
$iterableResult = $this->bindEvaluate(new IterableType(), $this, $input); | ||
if (!$iterableResult->isValid) { | ||
return $iterableResult->withId($id); | ||
} | ||
|
||
$array = $this->toArray($input); | ||
$notEmptyResult = $this->bindEvaluate(new NotEmpty(), $this, $array); | ||
if (!$notEmptyResult->isValid) { | ||
return $notEmptyResult->withId($id); | ||
} | ||
|
||
// @phpstan-ignore-next-line | ||
return $this->evaluateNonEmptyArray($array); | ||
} | ||
|
||
/** | ||
* @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
Oops, something went wrong.