1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Attribute; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Yiisoft\Validator\FormatterInterface; |
10
|
|
|
use Yiisoft\Validator\Result; |
11
|
|
|
use Yiisoft\Validator\Rule; |
12
|
|
|
use Yiisoft\Validator\RuleSet; |
13
|
|
|
use Yiisoft\Validator\ValidationContext; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Validates an array by checking each of its elements against a set of rules. |
17
|
|
|
*/ |
18
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
19
|
|
|
final class Each extends Rule |
20
|
|
|
{ |
21
|
|
|
private ?RuleSet $ruleSet = null; |
22
|
|
|
|
23
|
8 |
|
public function __construct( |
24
|
|
|
iterable $rules = [], |
25
|
|
|
private string $incorrectInputMessage = 'Value should be array or iterable.', |
26
|
|
|
private string $message = '{error} {value} given.', |
27
|
|
|
?FormatterInterface $formatter = null, |
28
|
|
|
bool $skipOnEmpty = false, |
29
|
|
|
bool $skipOnError = false, |
30
|
|
|
$when = null, |
31
|
|
|
) { |
32
|
8 |
|
if ($rules !== []) { |
33
|
8 |
|
$this->ruleSet = new RuleSet($rules); |
34
|
|
|
} |
35
|
|
|
|
36
|
8 |
|
parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when); |
37
|
|
|
} |
38
|
|
|
|
39
|
4 |
|
protected function validateValue($value, ?ValidationContext $context = null): Result |
40
|
|
|
{ |
41
|
4 |
|
if ($this->ruleSet === null) { |
42
|
|
|
throw new InvalidArgumentException('Rules are required.'); |
43
|
|
|
} |
44
|
|
|
|
45
|
4 |
|
$result = new Result(); |
46
|
4 |
|
if (!is_iterable($value)) { |
47
|
|
|
$result->addError($this->incorrectInputMessage); |
48
|
|
|
|
49
|
|
|
return $result; |
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
foreach ($value as $index => $item) { |
53
|
4 |
|
$itemResult = $this->ruleSet->validate($item, $context); |
54
|
4 |
|
if ($itemResult->isValid()) { |
55
|
3 |
|
continue; |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
foreach ($itemResult->getErrors() as $error) { |
59
|
4 |
|
if (!is_array($item)) { |
60
|
4 |
|
$errorKey = [$index]; |
61
|
4 |
|
$formatMessage = true; |
62
|
|
|
} else { |
63
|
1 |
|
$errorKey = [$index, ...$error->getValuePath()]; |
64
|
1 |
|
$formatMessage = false; |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
$message = !$formatMessage ? $error->getMessage() : $this->formatMessage($this->message, [ |
68
|
4 |
|
'error' => $error->getMessage(), |
69
|
|
|
'value' => $item, |
70
|
|
|
]); |
71
|
|
|
|
72
|
4 |
|
$result->addError($message, $errorKey); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
4 |
|
return $result; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
public function getOptions(): array |
80
|
|
|
{ |
81
|
2 |
|
return $this->ruleSet->asArray(); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
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.