Passed
Pull Request — master (#61)
by Alexander
01:36
created

Rule::translationDomain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
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...
8
9
/**
10
 * Rule represents a single value validation rule.
11
 */
12
abstract class Rule
13
{
14
    private ?TranslatorInterface $translator = null;
15
    private ?string $translationDomain = null;
16
    private ?string $translationLocale = null;
17
    private bool $skipOnEmpty = false;
18
    private bool $skipOnError = true;
19
20
    /**
21
     * Validates the value
22
     *
23
     * @param mixed $value value to be validated
24
     * @param DataSetInterface|null $dataSet optional data set that could be used for contextual validation
25
     * @param bool $previousRulesErrored set to true if rule is part of a group of rules and one of the previous validations failed
26
     * @return Result
27
     */
28 120
    final public function validate($value, DataSetInterface $dataSet = null, bool $previousRulesErrored = false): Result
29
    {
30 120
        if ($this->skipOnEmpty && $this->isEmpty($value)) {
31
            return new Result();
32
        }
33
34 120
        if ($this->skipOnError && $previousRulesErrored) {
35 2
            return new Result();
36
        }
37
38 120
        return $this->validateValue($value, $dataSet);
39
    }
40
41
    /**
42
     * Validates the value. The method should be implemented by concrete validation rules.
43
     *
44
     * @param mixed $value value to be validated
45
     * @param DataSetInterface|null $dataSet optional data set that could be used for contextual validation
46
     * @return Result
47
     */
48
    abstract protected function validateValue($value, DataSetInterface $dataSet = null): Result;
49
50
    public function translator(TranslatorInterface $translator): self
51
    {
52
        $new = clone $this;
53
        $new->translator = $translator;
54
        return $new;
55
    }
56
57
    public function translationDomain(string $translation): self
58
    {
59
        $new = clone $this;
60
        $new->translationDomain = $translation;
61
        return $new;
62
    }
63
64
    public function translationLocale(string $locale): self
65
    {
66
        $new = clone $this;
67
        $new->translationLocale = $locale;
68
        return $new;
69
    }
70
71 100
    public function translateMessage(string $message, array $arguments = []): string
72
    {
73 100
        if ($this->translator === null) {
74 100
            return $this->formatMessage($message, $arguments);
75
        }
76
77
        return $this->translator->translate(
78
            $message,
79
            $arguments,
80
            $this->translationDomain ?? 'validators',
81
            $this->translationLocale
82
        );
83
    }
84
85 1
    public function skipOnError(bool $value): self
86
    {
87 1
        $new = clone $this;
88 1
        $new->skipOnError = $value;
89 1
        return $new;
90
    }
91
92
    /**
93
     * @param bool $value if validation should be skipped if value validated is empty
94
     * @return self
95
     */
96 1
    public function skipOnEmpty(bool $value): self
97
    {
98 1
        $new = clone $this;
99 1
        $new->skipOnEmpty = $value;
100 1
        return $new;
101
    }
102
103 100
    private function formatMessage(string $message, array $arguments = []): string
104
    {
105 100
        $replacements = [];
106 100
        foreach ($arguments as $key => $value) {
107 37
            if (is_array($value)) {
108 1
                $value = 'array';
109 36
            } elseif (is_object($value)) {
110 2
                $value = 'object';
111 34
            } elseif (is_resource($value)) {
112 1
                $value = 'resource';
113
            }
114 37
            $replacements['{' . $key . '}'] = $value;
115
        }
116 100
        return strtr($message, $replacements);
117
    }
118
119
    /**
120
     * Checks if the given value is empty.
121
     * A value is considered empty if it is null, an empty array, or an empty string.
122
     * Note that this method is different from PHP empty(). It will return false when the value is 0.
123
     * @param mixed $value the value to be checked
124
     * @return bool whether the value is empty
125
     */
126 3
    protected function isEmpty($value): bool
127
    {
128 3
        return $value === null || $value === [] || $value === '';
129
    }
130
}
131