Passed
Pull Request — master (#151)
by
unknown
02:19
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, ErrorsReadInterface
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->getAttributeToErrorsMap(static fn (Result $result) => $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->getAttributeToErrorsMap(static fn (Result $result) => $result->getErrors());
50
    }
51
52 1
    public function getNestedErrors(): array
53
    {
54 1
        return $this->getAttributeToErrorsMap(static fn (Result $result) => $result->getNestedErrors());
55
    }
56
57 1
    public function getErrorsIndexedByPath(string $separator = '.'): array
58
    {
59 1
        return $this->getAttributeToErrorsMap(static fn (Result $result) => $result->getErrorsIndexedByPath($separator));
60
    }
61
62 4
    private function getAttributeToErrorsMap(Closure $getErrorsClosure): array
63
    {
64 4
        $errors = [];
65 4
        foreach ($this->results as $attribute => $result) {
66 4
            if (!$result->isValid()) {
67 4
                $errors[$attribute] = $getErrorsClosure($result);
68
            }
69
        }
70
71 4
        return $errors;
72
    }
73
74 20
    public function addResult(string $attribute, Result $result): void
75
    {
76 20
        if (!$result->isValid()) {
77 10
            $this->isValid = false;
78
        }
79
80 20
        if (!isset($this->results[$attribute])) {
81 20
            $this->results[$attribute] = $result;
82 20
            return;
83
        }
84
85 6
        if ($result->isValid()) {
86 1
            return;
87
        }
88
89 5
        foreach ($result->getErrors() as $error) {
90 5
            $this->results[$attribute]->addError($error);
91
        }
92 5
    }
93
94 5
    public function getResult(string $attribute): Result
95
    {
96 5
        if (!isset($this->results[$attribute])) {
97
            throw new InvalidArgumentException("There is no result for attribute \"$attribute\"");
98
        }
99
100 5
        return $this->results[$attribute];
101
    }
102
}
103