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

Validation::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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