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