Total Complexity | 11 |
Total Lines | 60 |
Duplicated Lines | 0 % |
Coverage | 87.5% |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
15 | final class ResultSet implements IteratorAggregate |
||
16 | { |
||
17 | /** |
||
18 | * @var Result[] |
||
19 | */ |
||
20 | private array $results = []; |
||
21 | private bool $isValid = true; |
||
22 | |||
23 | 9 | public function addResult(string $attribute, Result $result): void |
|
24 | { |
||
25 | 9 | if (!$result->isValid()) { |
|
26 | 8 | $this->isValid = false; |
|
27 | } |
||
28 | |||
29 | 9 | if (!isset($this->results[$attribute])) { |
|
30 | 9 | $this->results[$attribute] = $result; |
|
31 | 9 | return; |
|
32 | } |
||
33 | |||
34 | 3 | if ($result->isValid()) { |
|
35 | 1 | return; |
|
36 | } |
||
37 | |||
38 | 2 | $this->results[$attribute]->addResult($result); |
|
39 | 2 | } |
|
40 | |||
41 | 6 | public function getResult(string $attribute): Result |
|
42 | { |
||
43 | 6 | if (!isset($this->results[$attribute])) { |
|
44 | throw new InvalidArgumentException("There is no result for attribute \"$attribute\""); |
||
45 | } |
||
46 | |||
47 | 6 | return $this->results[$attribute]; |
|
48 | } |
||
49 | |||
50 | public function getIterator(): ArrayIterator |
||
51 | { |
||
52 | return new ArrayIterator($this->results); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param ErrorMessageFormatterInterface|null $formatter |
||
57 | * |
||
58 | * @return string[][] |
||
59 | */ |
||
60 | 1 | public function getErrors(?ErrorMessageFormatterInterface $formatter = null): array |
|
61 | { |
||
62 | 1 | $errors = []; |
|
63 | 1 | foreach ($this->results as $attribute => $result) { |
|
64 | 1 | if (!$result->isValid()) { |
|
65 | 1 | $errors[$attribute] = $result->getErrors($formatter); |
|
66 | } |
||
67 | } |
||
68 | |||
69 | 1 | return $errors; |
|
70 | } |
||
71 | |||
72 | 2 | public function isValid(): bool |
|
75 | } |
||
76 | } |
||
77 |