1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Yiisoft\Translator\TranslatorInterface; |
9
|
|
|
use Yiisoft\Validator\Exception\UnexpectedRuleException; |
10
|
|
|
use Yiisoft\Validator\Result; |
11
|
|
|
use Yiisoft\Validator\RuleHandlerInterface; |
12
|
|
|
use Yiisoft\Validator\ValidationContext; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Can be used for early stopping the validation process of the value when the validation on the stage was failed. |
16
|
|
|
* |
17
|
|
|
* For example, we have several rules, but we want not to process the rest rules when fail was ocurred: |
18
|
|
|
* |
19
|
|
|
* ```php |
20
|
|
|
* $request = [ |
21
|
|
|
* 'username' => 'yiisoft', |
22
|
|
|
* ]; |
23
|
|
|
* ``` |
24
|
|
|
* |
25
|
|
|
* So to make validation we can configure it like this: |
26
|
|
|
* |
27
|
|
|
* ```php |
28
|
|
|
* $rule = new Lazy([ |
29
|
|
|
* new HasLength(min: 3), |
30
|
|
|
* new ExistInDatabase(), // Heavy operation. It would be great not to call it if the previous rule was failed. |
31
|
|
|
* )]; |
32
|
|
|
* ]); |
33
|
|
|
* ``` |
34
|
|
|
*/ |
35
|
|
|
final class LazyHandler implements RuleHandlerInterface |
36
|
|
|
{ |
37
|
6 |
|
public function __construct(private TranslatorInterface $translator) |
38
|
|
|
{ |
39
|
|
|
} |
40
|
|
|
|
41
|
5 |
|
public function validate(mixed $value, object $rule, ValidationContext $context): Result |
42
|
|
|
{ |
43
|
5 |
|
if (!$rule instanceof Lazy) { |
44
|
1 |
|
throw new UnexpectedRuleException(Lazy::class, $rule); |
45
|
|
|
} |
46
|
|
|
|
47
|
4 |
|
if ($rule->getRules() === []) { |
48
|
|
|
throw new InvalidArgumentException( |
49
|
|
|
'Rules for Lazy rule are required.' |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
4 |
|
$compoundResult = new Result(); |
54
|
4 |
|
$results = []; |
55
|
|
|
|
56
|
4 |
|
foreach ($rule->getRules() as $rule) { |
57
|
4 |
|
$rules = [$rule]; |
58
|
|
|
|
59
|
4 |
|
if (is_iterable($rule)) { |
60
|
1 |
|
if (!$rule instanceof Lazy) { |
61
|
1 |
|
$rules = [new Lazy($rule)]; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
4 |
|
$lastResult = $context->getValidator()->validate($value, $rules); |
66
|
4 |
|
$results[] = $lastResult; |
67
|
|
|
|
68
|
4 |
|
if (!$lastResult->isValid()) { |
69
|
3 |
|
break; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
4 |
|
foreach ($results as $result) { |
74
|
4 |
|
foreach ($result->getErrors() as $error) { |
75
|
3 |
|
$compoundResult->addError($error->getMessage(), $error->getValuePath()); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
return $compoundResult; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|