Passed
Pull Request — master (#72)
by Wilmer
12:57
created

Validator::addRules()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 4
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use Yiisoft\Validator\Rule\Callback;
8
use Yiisoft\I18n\TranslatorInterface;
9
10
/**
11
 * Validator validates {@link DataSetInterface} against rules set for data set attributes.
12
 */
13
class Validator implements ValidatorInterface
14
{
15
    private ?TranslatorInterface $translator;
16
    private ?string $translationDomain;
17
    private ?string $translationLocale;
18
19
    /**
20
     * @var Rules[]
21
     */
22
    private array $attributeRules = [];
23
24 2
    public function __construct(
25
        iterable $rules = [],
0 ignored issues
show
Unused Code introduced by
The parameter $rules is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

25
        /** @scrutinizer ignore-unused */ iterable $rules = [],

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
26
        TranslatorInterface $translator = null,
27
        string $translationDomain = null,
28
        string $translationLocale = null
29
    ) {
30 2
        $this->translator = $translator;
31 2
        $this->translationDomain = $translationDomain;
32 2
        $this->translationLocale = $translationLocale;
33
    }
34 2
35 1
    public function validate(DataSetInterface $dataSet): ResultSet
36 1
    {
37 1
        $results = new ResultSet();
38
        foreach ($this->attributeRules as $attribute => $rules) {
39 1
            $results->addResult(
40
                $attribute,
41
                $rules->validate($dataSet->getAttributeValue($attribute), $dataSet)
42
            );
43
        }
44 2
        return $results;
45
    }
46 2
47 2
    public function addRule(string $attribute, Rule $rule): void
48 2
    {
49 2
        if (!isset($this->attributeRules[$attribute])) {
50 2
            $this->attributeRules[$attribute] = new Rules(
51
                [],
52
                $this->translator,
53 2
                $this->translationDomain,
54
                $this->translationLocale
55
            );
56 2
        }
57
58 2
        $this->attributeRules[$attribute]->add($rule);
59 2
    }
60 2
61 2
    public function addRules(iterable $rules = []): void
62 2
    {
63 2
        foreach ($rules as $attribute => $ruleSets) {
64
            foreach ($ruleSets as $rule) {
65
                if (is_callable($rule)) {
66
                    $rule = new Callback($rule);
67 2
                }
68
                $this->addRule($attribute, $rule);
69
            }
70
        }
71
    }
72
}
73