Passed
Pull Request — master (#99)
by Def
02:19
created

ResultSet   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 82
ccs 30
cts 33
cp 0.9091
rs 10
c 2
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 3 1
A getResult() 0 7 2
A addResult() 0 17 5
A getRawErrors() 0 10 3
A getErrors() 0 13 2
A isValid() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use ArrayIterator;
8
use InvalidArgumentException;
9
use IteratorAggregate;
10
11
/**
12
 * ResultSet stores validation result of each attribute from {@link DataSetInterface}.
13
 * It is typically obtained by validating data set with {@link Validator}.
14
 */
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
        foreach ($result->getRawErrors() as $error) {
39 2
            $this->results[$attribute]->addError($error);
40
        }
41 2
    }
42
43 6
    public function getResult(string $attribute): Result
44
    {
45 6
        if (!isset($this->results[$attribute])) {
46
            throw new InvalidArgumentException("There is no result for attribute \"$attribute\"");
47
        }
48
49 6
        return $this->results[$attribute];
50
    }
51
52
    public function getIterator(): ArrayIterator
53
    {
54
        return new ArrayIterator($this->results);
55
    }
56
57
    /**
58
     * @param ErrorMessageFormatterInterface|null $formatter
59
     *
60
     * @return ErrorMessage[][]
61
     */
62 1
    public function getRawErrors(): array
63
    {
64 1
        $errors = [];
65 1
        foreach ($this->results as $attribute => $result) {
66 1
            if (!$result->isValid()) {
67 1
                $errors[$attribute] = $result->getRawErrors();
68
            }
69
        }
70
71 1
        return $errors;
72
    }
73
74
    /**
75
     * @param ErrorMessageFormatterInterface|null $formatter
76
     *
77
     * @return string[][]
78
     */
79 1
    public function getErrors(?ErrorMessageFormatterInterface $formatter = null): array
80
    {
81 1
        $errors = $this->getRawErrors();
82 1
        foreach ($errors as $attribute => $attributeErrors) {
83 1
            $errors[$attribute] = array_map(
84 1
                function ($error) use ($formatter) {
85 1
                    return $error->getFormattedMessage($formatter);
86 1
                },
87
                $attributeErrors
88
            );
89
        }
90
91 1
        return $errors;
92
    }
93
94 2
    public function isValid(): bool
95
    {
96 2
        return $this->isValid;
97
    }
98
}
99