Passed
Pull Request — master (#73)
by
unknown
08:38
created

Validation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 44
ccs 0
cts 31
cp 0
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeRules() 0 23 5
A __construct() 0 8 1
A create() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use Yiisoft\I18n\TranslatorInterface;
8
use Yiisoft\Validator\Rule\Callback;
9
10
final class Validation implements ValidationInterface
11
{
12
    private ?TranslatorInterface $translator;
13
    private ?string $translationDomain;
14
    private ?string $translationLocale;
15
16
    public function __construct(
17
        TranslatorInterface $translator = null,
18
        string $translationDomain = null,
19
        string $translationLocale = null
20
    ) {
21
        $this->translator = $translator;
22
        $this->translationDomain = $translationDomain;
23
        $this->translationLocale = $translationLocale;
24
    }
25
26
    public function create(array $rules): ValidatorInterface
27
    {
28
        return new Validator($this->normalizeRules($rules));
29
    }
30
31
    private function normalizeRules(array $rules)
32
    {
33
        return array_map(
34
            function ($rule) {
35
                if (is_callable($rule)) {
36
                    $rule = new Callback($rule);
37
                }
38
39
                if ($this->translator !== null) {
40
                    $rule = $rule->withTranslator($this->translator);
41
                }
42
43
                if ($this->translationDomain !== null) {
44
                    $rule = $rule->withTranslationDomain($this->translationDomain);
45
                }
46
47
                if ($this->translationLocale !== null) {
48
                    $rule = $rule->withTranslationLocale($this->translationLocale);
49
                }
50
51
                return $rule;
52
            },
53
            $rules
54
        );
55
    }
56
}
57