Passed
Pull Request — master (#97)
by Sergei
02:04
created

Rules::addFormatterToRules()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 3
rs 10
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