-
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 wrapper-based rules
Signed-off-by: Henrique Moody <[email protected]>
- Loading branch information
1 parent
e341fef
commit 99dc872
Showing
12 changed files
with
329 additions
and
598 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
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\Helpers; | ||
|
||
use Respect\Validation\Exceptions\ComponentException; | ||
use Respect\Validation\Validatable; | ||
use Respect\Validation\Validator; | ||
|
||
use function array_map; | ||
use function count; | ||
use function current; | ||
use function sprintf; | ||
|
||
trait CanExtractRules | ||
{ | ||
private function extractSingle(Validatable $rule, string $class): Validatable | ||
{ | ||
if ($rule instanceof Validator) { | ||
return $this->extractSingleFromValidator($rule, $class); | ||
} | ||
|
||
if (!$rule instanceof $class) { | ||
throw new ComponentException(sprintf( | ||
'Could not extract rule %s from %s', | ||
$class, | ||
$rule::class, | ||
)); | ||
} | ||
|
||
return $rule; | ||
} | ||
|
||
/** | ||
* @param array<Validatable> $rules | ||
* | ||
* @return array<Validatable> | ||
*/ | ||
private function extractMany(array $rules, string $class): array | ||
{ | ||
return array_map(fn (Validatable $rule) => $this->extractSingle($rule, $class), $rules); | ||
} | ||
|
||
private function extractSingleFromValidator(Validator $rule, string $class): Validatable | ||
{ | ||
$rules = $rule->getRules(); | ||
if (count($rules) !== 1) { | ||
throw new ComponentException(sprintf( | ||
'Validator must contain exactly one rule' | ||
)); | ||
} | ||
|
||
return $this->extractSingle(current($rules), $class); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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.