1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator; |
6
|
|
|
|
7
|
|
|
use Yiisoft\I18n\TranslatorInterface; |
|
|
|
|
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
|
119 |
|
* @return Result |
27
|
|
|
*/ |
28
|
119 |
|
final public function validate($value, DataSetInterface $dataSet = null, bool $previousRulesErrored = false): Result |
29
|
|
|
{ |
30
|
|
|
if ($this->skipOnEmpty && $this->isEmpty($value)) { |
31
|
|
|
return new Result(); |
32
|
119 |
|
} |
33
|
|
|
|
34
|
|
|
if ($this->skipOnError && $previousRulesErrored) { |
35
|
|
|
return new Result(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
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 withTranslator(TranslatorInterface $translator): self |
51
|
|
|
{ |
52
|
|
|
$new = clone $this; |
53
|
|
|
$new->translator = $translator; |
54
|
|
|
return $new; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function withTranslationDomain(string $translation): self |
58
|
|
|
{ |
59
|
|
|
$new = clone $this; |
60
|
|
|
$new->translationDomain = $translation; |
61
|
|
|
return $new; |
62
|
99 |
|
} |
63
|
|
|
|
64
|
99 |
|
public function withTranslationLocale(string $locale): self |
65
|
99 |
|
{ |
66
|
|
|
$new = clone $this; |
67
|
|
|
$new->translationLocale = $locale; |
68
|
|
|
return $new; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function translateMessage(string $message, array $arguments = []): string |
72
|
|
|
{ |
73
|
|
|
if ($this->translator === null) { |
74
|
|
|
return $this->formatMessage($message, $arguments); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->translator->translate( |
78
|
|
|
$message, |
79
|
|
|
$arguments, |
80
|
1 |
|
$this->translationDomain ?? 'validators', |
81
|
|
|
$this->translationLocale |
82
|
1 |
|
); |
83
|
1 |
|
} |
84
|
1 |
|
|
85
|
|
|
public function skipOnError(bool $value): self |
86
|
|
|
{ |
87
|
99 |
|
$new = clone $this; |
88
|
|
|
$new->skipOnError = $value; |
89
|
99 |
|
return $new; |
90
|
99 |
|
} |
91
|
36 |
|
|
92
|
1 |
|
/** |
93
|
35 |
|
* @param bool $value if validation should be skipped if value validated is empty |
94
|
2 |
|
* @return self |
95
|
33 |
|
*/ |
96
|
1 |
|
public function skipOnEmpty(bool $value): self |
97
|
|
|
{ |
98
|
36 |
|
$new = clone $this; |
99
|
|
|
$new->skipOnEmpty = $value; |
100
|
99 |
|
return $new; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function formatMessage(string $message, array $arguments = []): string |
104
|
|
|
{ |
105
|
|
|
$replacements = []; |
106
|
|
|
foreach ($arguments as $key => $value) { |
107
|
|
|
if (is_array($value)) { |
108
|
|
|
$value = 'array'; |
109
|
|
|
} elseif (is_object($value)) { |
110
|
3 |
|
$value = 'object'; |
111
|
|
|
} elseif (is_resource($value)) { |
112
|
3 |
|
$value = 'resource'; |
113
|
|
|
} |
114
|
|
|
$replacements['{' . $key . '}'] = $value; |
115
|
|
|
} |
116
|
|
|
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
|
|
|
protected function isEmpty($value): bool |
127
|
|
|
{ |
128
|
|
|
return $value === null || $value === [] || $value === ''; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths