Passed
Push — master ( 9d8a75...f0a095 )
by Alexander
01:39
created

Rules::validate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
nc 4
nop 2
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 4
rs 10
c 1
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;
0 ignored issues
show
Bug introduced by
The type Yiisoft\I18n\TranslatorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
/**
11
 * Rules represents multiple rules for a single value
12
 */
13
class Rules
14
{
15
    /**
16
     * @var Rule[]
17
     */
18
    private array $rules = [];
19
    private ?TranslatorInterface $translator;
20
    private ?string $translationDomain;
21
    private ?string $translationLocale;
22
23 6
    public function __construct(
24
        iterable $rules = [],
25
        ?TranslatorInterface $translator = null,
26
        ?string $translationDomain = null,
27
        ?string $translationLocale = null
28
    ) {
29 6
        $this->translator = $translator;
30 6
        $this->translationDomain = $translationDomain;
31 6
        $this->translationLocale = $translationLocale;
32
33 6
        foreach ($rules as $rule) {
34 3
            $this->rules[] = $this->normalizeRule($rule);
35
        }
36
    }
37
38 6
    private function normalizeRule($rule): Rule
39
    {
40 6
        if (is_callable($rule)) {
41 1
            $rule = new Callback($rule);
42
        }
43
44 6
        if (!$rule instanceof Rule) {
45
            throw new \InvalidArgumentException('Rule should be either instance of Rule class or a callable');
46
        }
47
48 6
        if ($this->translator !== null) {
49
            $rule->setTranslator($this->translator);
50
        }
51
52 6
        if ($this->translationDomain !== null) {
53
            $rule->setTranslationDomain($this->translationDomain);
54
        }
55
56 6
        if ($this->translationLocale !== null) {
57
            $rule->setTranslationLocale($this->translationLocale);
58
        }
59
60 6
        return $rule;
61
    }
62
63 3
    public function add(Rule $rule): void
64
    {
65 3
        $this->rules[] = $this->normalizeRule($rule);
66
    }
67
68 6
    public function validate($value, DataSetInterface $dataSet = null): Result
69
    {
70 6
        $compoundResult = new Result();
71 6
        foreach ($this->rules as $rule) {
72 6
            $ruleResult = $rule->validate($value, $dataSet);
73 6
            if ($ruleResult->isValid() === false) {
74 6
                foreach ($ruleResult->getErrors() as $message) {
75 6
                    $compoundResult->addError($message);
76
                }
77
            }
78
        }
79 6
        return $compoundResult;
80
    }
81
}
82