Passed
Pull Request — master (#97)
by Alexander
08:38 queued 06:21
created

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