Passed
Push — master ( 72c1ff...1d076d )
by Alexander
02:24
created

ResultSet::getErrorsIndexedByPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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