Passed
Pull Request — master (#151)
by
unknown
02:20
created

ResultSet   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 91.89%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 30
c 5
b 0
f 0
dl 0
loc 88
ccs 34
cts 37
cp 0.9189
rs 10
wmc 17

8 Methods

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