|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator; |
|
6
|
|
|
|
|
7
|
|
|
use ArrayIterator; |
|
8
|
|
|
use Closure; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
use IteratorAggregate; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* ResultSet stores validation result of each attribute from {@link DataSetInterface}. |
|
14
|
|
|
* It is typically obtained by validating data set with {@link Validator}. |
|
15
|
|
|
*/ |
|
16
|
|
|
final class ResultSet implements IteratorAggregate |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Result[] |
|
20
|
|
|
* @psalm-var array<string, Result> |
|
21
|
|
|
*/ |
|
22
|
|
|
private array $results = []; |
|
23
|
|
|
private bool $isValid = true; |
|
24
|
|
|
|
|
25
|
|
|
public function getIterator(): ArrayIterator |
|
26
|
|
|
{ |
|
27
|
|
|
return new ArrayIterator($this->results); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
11 |
|
public function isValid(): bool |
|
31
|
|
|
{ |
|
32
|
11 |
|
return $this->isValid; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @psalm-return array<string, Error> |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function getErrorObjects(): array |
|
39
|
|
|
{ |
|
40
|
1 |
|
return $this->getErrorsMap(static fn (Result $result): array => $result->getErrorObjects()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return string[][] |
|
45
|
|
|
* @psalm-return array<string, array<int|string, string>> |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function getErrors(): array |
|
48
|
|
|
{ |
|
49
|
1 |
|
return $this->getErrorsMap(static fn (Result $result): array => $result->getErrors()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
public function getErrorsIndexedByPath(string $separator = '.'): array |
|
53
|
|
|
{ |
|
54
|
1 |
|
return $this->getErrorsMap(static fn (Result $result): array => $result->getErrorsIndexedByPath($separator)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
3 |
|
private function getErrorsMap(Closure $getErrorsClosure): array |
|
58
|
|
|
{ |
|
59
|
3 |
|
$errors = []; |
|
60
|
3 |
|
foreach ($this->results as $attribute => $result) { |
|
61
|
3 |
|
if (!$result->isValid()) { |
|
62
|
3 |
|
$errors[$attribute] = $getErrorsClosure($result); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
3 |
|
return $errors; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
19 |
|
public function addResult(string $attribute, Result $result): void |
|
70
|
|
|
{ |
|
71
|
19 |
|
if (!$result->isValid()) { |
|
72
|
9 |
|
$this->isValid = false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
19 |
|
if (!isset($this->results[$attribute])) { |
|
76
|
19 |
|
$this->results[$attribute] = $result; |
|
77
|
19 |
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
5 |
|
if ($result->isValid()) { |
|
81
|
1 |
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
4 |
|
foreach ($result->getErrors() as $error) { |
|
85
|
4 |
|
$this->results[$attribute]->addError($error); |
|
86
|
|
|
} |
|
87
|
4 |
|
} |
|
88
|
|
|
|
|
89
|
5 |
|
public function getResult(string $attribute): Result |
|
90
|
|
|
{ |
|
91
|
5 |
|
if (!isset($this->results[$attribute])) { |
|
92
|
|
|
throw new InvalidArgumentException("There is no result for attribute \"$attribute\""); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
5 |
|
return $this->results[$attribute]; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|