Passed
Pull Request — master (#97)
by Alexander
02:08
created

Validator::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 3
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use Yiisoft\Translator\TranslatorInterface;
8
9
/**
10
 * Validator validates {@link DataSetInterface} against rules set for data set attributes.
11
 */
12
final class Validator implements ValidatorInterface
13
{
14
    private ?TranslatorInterface $translator;
15
16 3
    public function __construct(?TranslatorInterface $translator = null)
17
    {
18 3
        $this->translator = $translator;
19 3
    }
20
21
    /**
22
     * @param DataSetInterface $dataSet
23
     * @param Rule[] $rules
24
     *
25
     * @return ResultSet
26
     */
27 3
    public function validate(DataSetInterface $dataSet, iterable $rules): ResultSet
28
    {
29 3
        $results = new ResultSet();
30 3
        foreach ($rules as $attribute => $attributeRules) {
31 3
            $aggregateRule = new Rules($attributeRules);
32 3
            if ($this->translator !== null) {
33 1
                $aggregateRule = $aggregateRule->withTranslator($this->translator);
34
            }
35 3
            $results->addResult(
36 3
                $attribute,
37 3
                $aggregateRule->validate($dataSet->getAttributeValue($attribute), $dataSet)
38
            );
39
        }
40 3
        return $results;
41
    }
42
43 1
    public function withTranslator(?TranslatorInterface $translator): self
44
    {
45 1
        $new = clone $this;
46 1
        $new->translator = $translator;
47 1
        return $new;
48
    }
49
}
50