1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Spiral\Validation\Checkers; |
10
|
|
|
|
11
|
|
|
use Spiral\Core\Container\SingletonInterface; |
12
|
|
|
use Spiral\Validation\Checkers\Traits\NotEmptyTrait; |
13
|
|
|
use Spiral\Validation\Prototypes\AbstractChecker; |
14
|
|
|
use Spiral\Validation\Validator; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Validations based dependencies between fields. External values checked using notEmpty method. |
18
|
|
|
* |
19
|
|
|
* See tests. |
20
|
|
|
*/ |
21
|
|
|
class RequiredChecker extends AbstractChecker implements SingletonInterface |
22
|
|
|
{ |
23
|
|
|
use NotEmptyTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
const MESSAGES = [ |
29
|
|
|
'notEmpty' => '[[This value is required.]]', |
30
|
|
|
'with' => '[[This value is required.]]', |
31
|
|
|
'withAll' => '[[This value is required.]]', |
32
|
|
|
'without' => '[[This value is required.]]', |
33
|
|
|
'withoutAll' => '[[This value is required.]]', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Check if field not empty but only if any of listed fields presented or not empty. |
38
|
|
|
* Also: requiredWhenAnyExternalSet |
39
|
|
|
* |
40
|
|
|
* @param mixed $value |
41
|
|
|
* @param array|string $with |
42
|
|
|
* @param bool $asString Automatically trim external values before non empty |
43
|
|
|
* comparision. |
44
|
|
|
* |
45
|
|
|
* @return bool|int |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function with($value, $with, bool $asString = true) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
if ($this->notEmpty($value, $asString)) { |
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$with = (array)$with; |
54
|
|
|
foreach ($with as $field) { |
55
|
|
|
if (!$this->isEmpty($field, $asString)) { |
56
|
|
|
//External field presented, BUT value must not be empty! |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
//Value is empty, but no external fields are set = value not required |
62
|
|
|
return Validator::STOP_VALIDATION; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Check if field not empty but only if all of listed fields presented and not empty. |
67
|
|
|
* Also: requiredWhenAllExternalSet |
68
|
|
|
* |
69
|
|
|
* @param mixed $value |
70
|
|
|
* @param array $withAll |
71
|
|
|
* @param bool $asString Automatically trim external values before non empty comparision. |
72
|
|
|
* |
73
|
|
|
* @return bool|int |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function withAll($value, $withAll, bool $asString = true) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
if ($this->notEmpty($value, $asString)) { |
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$withAll = (array)$withAll; |
82
|
|
|
foreach ($withAll as $field) { |
83
|
|
|
if ($this->isEmpty($field, $asString)) { |
84
|
|
|
//External field is missing, value becomes non required |
85
|
|
|
return Validator::STOP_VALIDATION; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
//Value is empty but all external field not empty |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Check if field not empty but only if one of listed fields missing or empty. |
95
|
|
|
* Also: requiredWhenNoExternalsSet |
96
|
|
|
* |
97
|
|
|
* @param mixed $value |
98
|
|
|
* @param array|string $without |
99
|
|
|
* @param bool $asString Automatically trim external values before non empty |
100
|
|
|
* comparision. |
101
|
|
|
* |
102
|
|
|
* @return bool|int |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function without($value, $without, bool $asString = true) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
if ($this->notEmpty($value, $asString)) { |
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$without = (array)$without; |
111
|
|
|
foreach ($without as $field) { |
112
|
|
|
if (!$this->isEmpty($field, $asString)) { |
113
|
|
|
//External field set, field becomes non required |
114
|
|
|
return Validator::STOP_VALIDATION; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
//Value is empty and no external fields are set |
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Check if field not empty but only if all of listed fields missing or empty. |
124
|
|
|
* Also: requiredWhenAllExternalNotSet |
125
|
|
|
* |
126
|
|
|
* @param mixed $value |
127
|
|
|
* @param array|string $withoutAll |
128
|
|
|
* @param bool $asString Automatically trim external values before non empty |
129
|
|
|
* comparision. |
130
|
|
|
* |
131
|
|
|
* @return bool|int |
132
|
|
|
*/ |
133
|
|
|
public function withoutAll($value, $withoutAll, bool $asString = true) |
134
|
|
|
{ |
135
|
|
|
if ($this->notEmpty($value, $asString)) { |
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$withoutAll = (array)$withoutAll; |
140
|
|
|
|
141
|
|
|
$allNotSet = true; |
142
|
|
|
foreach ($withoutAll as $field) { |
143
|
|
|
$allNotSet = $allNotSet && $this->isEmpty($field, $asString); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if ($allNotSet) { |
147
|
|
|
//No external values set, value not required |
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return Validator::STOP_VALIDATION; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Check if external validation value is empty. |
156
|
|
|
* |
157
|
|
|
* @param string $field |
158
|
|
|
* @param bool $string |
159
|
|
|
* |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
private function isEmpty(string $field, bool $string) |
163
|
|
|
{ |
164
|
|
|
return !$this->notEmpty($this->getValidator()->getValue($field, null), $string); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.