|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Validator\Exception\UnexpectedRuleException; |
|
8
|
|
|
use Yiisoft\Validator\Result; |
|
9
|
|
|
use Yiisoft\Validator\Rule\Trait\PreValidateTrait; |
|
10
|
|
|
use Yiisoft\Validator\ValidationContext; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Can be used to group rules for validation by `skipOnEmpty`, `skipOnError` or `when`. |
|
14
|
|
|
* |
|
15
|
|
|
* For example, we have same when closure: |
|
16
|
|
|
* ``` |
|
17
|
|
|
* 'name' => [ |
|
18
|
|
|
* new Required(when: fn() => $this->useName), |
|
19
|
|
|
* new HasLength(min: 1, max: 50, skipOnEmpty: true, when: fn() => $this->useName), |
|
20
|
|
|
* ], |
|
21
|
|
|
* ``` |
|
22
|
|
|
* So we can configure it like this: |
|
23
|
|
|
* ``` |
|
24
|
|
|
* 'name' => [ |
|
25
|
|
|
* new Composite( |
|
26
|
|
|
* rules: [ |
|
27
|
|
|
* new Required(), |
|
28
|
|
|
* new HasLength(min: 1, max: 50, skipOnEmpty: true), |
|
29
|
|
|
* ], |
|
30
|
|
|
* when: fn() => $this->useName, |
|
31
|
|
|
* ), |
|
32
|
|
|
* ], |
|
33
|
|
|
* ``` |
|
34
|
|
|
*/ |
|
35
|
|
|
final class CompositeHandler implements RuleHandlerInterface |
|
36
|
|
|
{ |
|
37
|
|
|
use PreValidateTrait; |
|
38
|
|
|
|
|
39
|
6 |
|
public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result |
|
40
|
|
|
{ |
|
41
|
6 |
|
if (!$rule instanceof Composite) { |
|
42
|
1 |
|
throw new UnexpectedRuleException(Composite::class, $rule); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
5 |
|
$context->setParameter($this->parameterPreviousRulesErrored, true); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
5 |
|
if ($this->preValidate($value, $context, $rule)) { |
|
|
|
|
|
|
48
|
3 |
|
return new Result(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
return $context->getValidator()->validate($value, $rule->getRules()); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.