Passed
Pull Request — master (#151)
by
unknown
01:58
created

ResultSet::getErrors()   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 0
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
        $errors = [];
55 1
        foreach ($this->results as $attribute => $result) {
56 1
            if ($result->isValid()) {
57 1
                continue;
58
            }
59
60 1
            foreach ($result->getErrorsIndexedByPath() as $path => $innerErrors) {
61 1
                $path = !$path ? $attribute : $attribute . $separator . $path;
62 1
                $errors[$path] = $innerErrors;
63
            }
64
        }
65
66 1
        return $errors;
67
    }
68
69 2
    private function getErrorsMap(Closure $getErrorsClosure): array
70
    {
71 2
        $errors = [];
72 2
        foreach ($this->results as $attribute => $result) {
73 2
            if (!$result->isValid()) {
74 2
                $errors[$attribute] = $getErrorsClosure($result);
75
            }
76
        }
77
78 2
        return $errors;
79
    }
80
81 19
    public function addResult(string $attribute, Result $result): void
82
    {
83 19
        if (!$result->isValid()) {
84 9
            $this->isValid = false;
85
        }
86
87 19
        if (!isset($this->results[$attribute])) {
88 19
            $this->results[$attribute] = $result;
89 19
            return;
90
        }
91
92 5
        if ($result->isValid()) {
93 1
            return;
94
        }
95
96 4
        foreach ($result->getErrors() as $error) {
97 4
            $this->results[$attribute]->addError($error);
98
        }
99 4
    }
100
101 5
    public function getResult(string $attribute): Result
102
    {
103 5
        if (!isset($this->results[$attribute])) {
104
            throw new InvalidArgumentException("There is no result for attribute \"$attribute\"");
105
        }
106
107 5
        return $this->results[$attribute];
108
    }
109
}
110