Passed
Pull Request — master (#96)
by Dmitriy
02:36
created

ValidatorFactory::normalizeRule()   B

Complexity

Conditions 7
Paths 20

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.116

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 14
c 2
b 0
f 0
nc 20
nop 1
dl 0
loc 28
ccs 13
cts 15
cp 0.8667
crap 7.116
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use Yiisoft\Translator\TranslatorInterface;
8
use Yiisoft\Validator\Rule\Callback;
9
10
final class ValidatorFactory implements ValidatorFactoryInterface
11
{
12
    private ?TranslatorInterface $translator;
13
    private ?string $translationDomain;
14
    private ?string $translationLocale;
15
16 3
    public function __construct(
17
        TranslatorInterface $translator = null,
18
        string $translationDomain = null,
19
        string $translationLocale = null
20
    ) {
21 3
        $this->translator = $translator;
22 3
        $this->translationDomain = $translationDomain;
23 3
        $this->translationLocale = $translationLocale;
24 3
    }
25
26 3
    public function create(array $rules): ValidatorInterface
27
    {
28 3
        return new Validator($this->normalizeRules($rules));
29
    }
30
31 3
    private function normalizeRules(array $rules)
32
    {
33 3
        foreach ($rules as $attribute => $ruleSets) {
34 3
            foreach ($ruleSets as $index => $rule) {
35 3
                $ruleSets[$index] = $this->normalizeRule($rule);
36
            }
37 2
            $rules[$attribute] = $ruleSets;
38
        }
39 2
        return $rules;
40
    }
41
42
    /**
43
     * @param callable|RuleInterface $rule
44
     */
45 3
    private function normalizeRule($rule): RuleInterface
46
    {
47 3
        if (is_callable($rule)) {
48 2
            $rule = new Callback($rule);
0 ignored issues
show
Bug introduced by
It seems like $rule can also be of type Yiisoft\Validator\RuleInterface; however, parameter $callback of Yiisoft\Validator\Rule\Callback::__construct() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

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

48
            $rule = new Callback(/** @scrutinizer ignore-type */ $rule);
Loading history...
49
        }
50
51 3
        if (!$rule instanceof RuleInterface) {
52 1
            throw new \InvalidArgumentException(sprintf(
53 1
                'Rule should be either instance of %s or a callable',
54 1
                RuleInterface::class
55
            ));
56
        }
57
58 2
        if ($rule instanceof TranslatableRuleInterface) {
59 2
            if ($this->translator !== null) {
60 1
                $rule = $rule->translator($this->translator);
61
            }
62
63 2
            if ($this->translationDomain !== null) {
64
                $rule = $rule->translationDomain($this->translationDomain);
65
            }
66
67 2
            if ($this->translationLocale !== null) {
68
                $rule = $rule->translationLocale($this->translationLocale);
69
            }
70
        }
71
72 2
        return $rule;
73
    }
74
}
75