1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Translator\TranslatorInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Rule represents a single value validation rule. |
11
|
|
|
*/ |
12
|
|
|
abstract class Rule |
13
|
|
|
{ |
14
|
|
|
private bool $skipOnEmpty = false; |
15
|
|
|
private bool $skipOnError = true; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var callable|null |
19
|
|
|
*/ |
20
|
|
|
private $when = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Validates the value |
24
|
|
|
* |
25
|
|
|
* @param mixed $value value to be validated |
26
|
|
|
* @param DataSetInterface|null $dataSet optional data set that could be used for contextual validation |
27
|
|
|
* @param bool $previousRulesErrored set to true if rule is part of a group of rules and one of the previous validations failed |
28
|
|
|
* |
29
|
|
|
* @return Result |
30
|
|
|
*/ |
31
|
138 |
|
final public function validate($value, DataSetInterface $dataSet = null, bool $previousRulesErrored = false): Result |
32
|
|
|
{ |
33
|
138 |
|
if ($this->skipOnEmpty && $this->isEmpty($value)) { |
34
|
|
|
return new Result(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ( |
38
|
138 |
|
($this->skipOnError && $previousRulesErrored) || |
39
|
138 |
|
(is_callable($this->when) && !($this->when)($value, $dataSet)) |
40
|
|
|
) { |
41
|
7 |
|
return new Result(); |
42
|
|
|
} |
43
|
|
|
|
44
|
138 |
|
return $this->validateValue($value, $dataSet); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Validates the value. The method should be implemented by concrete validation rules. |
49
|
|
|
* |
50
|
|
|
* @param mixed $value value to be validated |
51
|
|
|
* @param DataSetInterface|null $dataSet optional data set that could be used for contextual validation |
52
|
|
|
* |
53
|
|
|
* @return Result |
54
|
|
|
*/ |
55
|
|
|
abstract protected function validateValue($value, DataSetInterface $dataSet = null): Result; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Add a PHP callable whose return value determines whether this rule should be applied. |
59
|
|
|
* By default rule will be always applied. |
60
|
|
|
* |
61
|
|
|
* The signature of the callable should be `function ($value, DataSetInterface $dataSet): bool`, where $value and $dataSet |
62
|
|
|
* refer to the value validated and the data set in which context it is validated. The callable should return |
63
|
|
|
* a boolean value. |
64
|
|
|
* |
65
|
|
|
* The following example will enable the validator only when the country currently selected is USA: |
66
|
|
|
* |
67
|
|
|
* ```php |
68
|
|
|
* function ($value, DataSetInterface $dataSet) { |
69
|
|
|
return $dataSet->getAttributeValue('country') === Country::USA; |
70
|
|
|
} |
71
|
|
|
* ``` |
72
|
|
|
* |
73
|
|
|
* @param callable $callback |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
1 |
|
public function when(callable $callback): self |
78
|
|
|
{ |
79
|
1 |
|
$new = clone $this; |
80
|
1 |
|
$new->when = $callback; |
81
|
1 |
|
return $new; |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
public function skipOnError(bool $value): self |
85
|
|
|
{ |
86
|
3 |
|
$new = clone $this; |
87
|
3 |
|
$new->skipOnError = $value; |
88
|
3 |
|
return $new; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param bool $value if validation should be skipped if value validated is empty |
93
|
|
|
* |
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
|
|
|
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
|
|
|
$value = 'object'; |
111
|
|
|
} elseif (is_resource($value)) { |
112
|
|
|
$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
|
|
|
* |
124
|
|
|
* @param mixed $value the value to be checked |
125
|
|
|
* |
126
|
|
|
* @return bool whether the value is empty |
127
|
|
|
*/ |
128
|
10 |
|
protected function isEmpty($value): bool |
129
|
|
|
{ |
130
|
10 |
|
return $value === null || $value === [] || $value === ''; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get name of the rule to be used when rule is converted to array. |
135
|
|
|
* By default it returns base name of the class, first letter in lowercase. |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
21 |
|
public function getName(): string |
140
|
|
|
{ |
141
|
21 |
|
$className = static::class; |
142
|
21 |
|
return lcfirst(substr($className, strrpos($className, '\\') + 1)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns rule options as array. |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
58 |
|
public function getOptions(?TranslatorInterface $translator = null): array |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
return [ |
153
|
58 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
154
|
58 |
|
'skipOnError' => $this->skipOnError, |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.