Passed
Pull Request — master (#97)
by Sergei
01:55
created

Rules   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 88.37%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 37
c 3
b 0
f 0
dl 0
loc 89
ccs 38
cts 43
cp 0.8837
rs 10
wmc 19

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A asArray() 0 9 3
A add() 0 7 3
A normalizeRule() 0 15 3
A validate() 0 13 4
A addFormatterToRules() 0 5 3
A withFormatter() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use Yiisoft\Validator\Rule\Callback;
8
9
/**
10
 * Rules represents multiple rules for a single value
11
 */
12
final class Rules
13
{
14
    private ?FormatterInterface $formatter = null;
15
16
    /**
17
     * @var RuleInterface[]
18
     */
19
    private array $rules = [];
20
21 16
    public function __construct(iterable $rules = [])
22
    {
23 16
        foreach ($rules as $rule) {
24 15
            $this->add($rule);
25
        }
26 16
    }
27
28
    /**
29
     * @param callable|RuleInterface $rule
30
     */
31 16
    public function add($rule): void
32
    {
33 16
        $rule = $this->normalizeRule($rule);
34 16
        if ($this->formatter !== null && $rule instanceof FormattableRuleInterface) {
35
            $rule = $rule->withFormatter($this->formatter);
36
        }
37 16
        $this->rules[] = $rule;
38 16
    }
39
40 10
    public function validate($value, DataSetInterface $dataSet = null, bool $previousRulesErrored = false): Result
41
    {
42 10
        $compoundResult = new Result();
43 10
        foreach ($this->rules as $rule) {
44 10
            $ruleResult = $rule->validate($value, $dataSet, $previousRulesErrored);
45 10
            if ($ruleResult->isValid() === false) {
46 10
                $previousRulesErrored = true;
47 10
                foreach ($ruleResult->getErrors() as $message) {
48 10
                    $compoundResult->addError($message);
49
                }
50
            }
51
        }
52 10
        return $compoundResult;
53
    }
54
55 16
    private function normalizeRule($rule): RuleInterface
56
    {
57 16
        if (is_callable($rule)) {
58 2
            $rule = new Callback($rule);
59
        }
60
61 16
        if (!$rule instanceof RuleInterface) {
62
            throw new \InvalidArgumentException(sprintf(
63
                'Rule should be either instance of %s or a callable, %s given.',
64
                RuleInterface::class,
65
                gettype($rule)
66
            ));
67
        }
68
69 16
        return $rule;
70
    }
71
72 1
    public function withFormatter(?FormatterInterface $formatter): self
73
    {
74 1
        $new = clone $this;
75 1
        $new->formatter = $formatter;
76 1
        $new->addFormatterToRules($formatter);
77 1
        return $new;
78
    }
79
80
    /**
81
     * Return rules as array.
82
     *
83
     * @return array
84
     */
85 5
    public function asArray(): array
86
    {
87 5
        $arrayOfRules = [];
88 5
        foreach ($this->rules as $rule) {
89 5
            if ($rule instanceof ParametrizedRuleInterface) {
90 5
                $arrayOfRules[] = array_merge([$rule->getName()], $rule->getOptions());
91
            }
92
        }
93 5
        return $arrayOfRules;
94
    }
95
96 1
    private function addFormatterToRules(?FormatterInterface $formatter): void
97
    {
98 1
        foreach ($this->rules as &$rule) {
99 1
            if ($rule instanceof FormattableRuleInterface) {
100 1
                $rule = $rule->withFormatter($formatter);
101
            }
102
        }
103 1
    }
104
}
105