Passed
Pull Request — master (#81)
by Def
01:23
created

Validator::__construct()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.0488

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 6
nop 1
dl 0
loc 10
ccs 7
cts 8
cp 0.875
crap 5.0488
rs 9.6111
c 0
b 0
f 0
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
    /**
13
     * @var Rules[]
14
     */
15
    private array $attributeRules = [];
16
17 5
    public function __construct(iterable $rules = [])
18
    {
19 5
        foreach ($rules as $attribute => $ruleSets) {
20 4
            if ($ruleSets instanceof Rule) {
21 1
                $ruleSets = [$ruleSets];
22 4
            } elseif (!is_iterable($ruleSets)) {
23
                throw new \InvalidArgumentException('Attribute rules should be either an instance of Rule class or an array of instances of Rule class.');
24
            }
25 4
            foreach ($ruleSets as $rule) {
26 4
                $this->addRule($attribute, $rule);
27
            }
28
        }
29
    }
30
31 4
    public function validate(DataSetInterface $dataSet): ResultSet
32
    {
33 4
        $results = new ResultSet();
34 4
        foreach ($this->attributeRules as $attribute => $rules) {
35 4
            $results->addResult(
36 4
                $attribute,
37 4
                $rules->validate($dataSet->getAttributeValue($attribute), $dataSet)
38
            );
39
        }
40 4
        return $results;
41
    }
42
43
    /**
44
     * @param string $attribute
45
     * @param Rule|callable
46
     */
47 5
    public function addRule(string $attribute, $rule): void
48
    {
49 5
        if (!isset($this->attributeRules[$attribute])) {
50 5
            $this->attributeRules[$attribute] = new Rules([]);
51
        }
52 5
        $this->attributeRules[$attribute]->add($rule);
53
    }
54
55
    /**
56
     * Return all attribute rules as array.
57
     *
58
     * For example:
59
     *
60
     * ```php
61
     * [
62
     *    'amount' => [
63
     *        [
64
     *            'number',
65
     *            'integer' => true,
66
     *            'max' => 100,
67
     *            'notANumberMessage' => 'Value must be an integer.',
68
     *            'tooBigMessage' => 'Value must be no greater than 100.'
69
     *        ],
70
     *        ['callback'],
71
     *    ],
72
     *    'name' => [
73
     *        'hasLength',
74
     *        'max' => 20,
75
     *        'message' => 'Value must contain at most 20 characters.'
76
     *    ],
77
     * ]
78
     * ```
79
     *
80
     * @return array
81
     */
82 1
    public function asArray(): array
83
    {
84 1
        $rulesOfArray = [];
85 1
        foreach ($this->attributeRules as $attribute => $rules) {
86 1
            $rulesOfArray[$attribute] = $rules->asArray();
87
        }
88 1
        return $rulesOfArray;
89
    }
90
}
91