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