Passed
Pull Request — master (#41)
by Alexander
12:06 queued 10:41
created

ResultSet::getResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yiisoft\Validator;
4
5
/**
6
 * ResultSet stores validation result of each attribute from {@link DataSetInterface}.
7
 * It is typically obtained by validating data set with {@link Validator}.
8
 */
9
final class ResultSet implements \IteratorAggregate
10
{
11
    /**
12
     * @var Result[]
13
     */
14
    private array $results = [];
15
16 4
    public function addResult(string $attribute, Result $result): void
17
    {
18 4
        if (!isset($this->results[$attribute])) {
19 4
            $this->results[$attribute] = $result;
20 4
            return;
21
        }
22 2
        if ($result->isValid()) {
23 1
            return;
24
        }
25 1
        foreach ($result->getErrors() as $error) {
26 1
            $this->results[$attribute]->addError($error);
27
        }
28
    }
29
30 4
    public function getResult(string $attribute): Result
31
    {
32 4
        if (!isset($this->results[$attribute])) {
33
            throw new \InvalidArgumentException("There is no result for attribute \"$attribute\"");
34
        }
35
36 4
        return $this->results[$attribute];
37
    }
38
39
    public function getIterator()
40
    {
41
        return new \ArrayIterator($this->results);
42
    }
43
}
44