Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
47 / 47 |
|
100.00% |
21 / 21 |
CRAP | |
100.00% |
1 / 1 |
Condition | |
100.00% |
47 / 47 |
|
100.00% |
21 / 21 |
34 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
6 | |||
createFromRule | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setField | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setValidator | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setValue | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setMessage | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setPrefix | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getValidator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getMessage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getPrefix | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getValidatorObject | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasValidator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasMessage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasPrefix | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasValidatorObject | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
evaluate | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
9 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@noladev.com> |
7 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Validator; |
15 | |
16 | /** |
17 | * Has one that equals validator class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Validator |
21 | * @author Nick Sagona, III <dev@noladev.com> |
22 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 4.5.0 |
25 | */ |
26 | class Condition |
27 | { |
28 | |
29 | /** |
30 | * Field |
31 | * @var ?string |
32 | */ |
33 | protected ?string $field = null; |
34 | |
35 | /** |
36 | * Validator |
37 | * @var ?string |
38 | */ |
39 | protected ?string $validator = null; |
40 | |
41 | /** |
42 | * Value |
43 | * @var mixed |
44 | */ |
45 | protected mixed $value = null; |
46 | |
47 | /** |
48 | * Message |
49 | * @var ?string |
50 | */ |
51 | protected ?string $message = null; |
52 | |
53 | |
54 | /** |
55 | * Prefix |
56 | * @var ?string |
57 | */ |
58 | protected ?string $prefix = null; |
59 | |
60 | /** |
61 | * Validator |
62 | * @var ?AbstractValidator |
63 | */ |
64 | protected ?AbstractValidator $validatorObject = null; |
65 | |
66 | /** |
67 | * Constructor |
68 | * |
69 | * Instantiate the condition object |
70 | * |
71 | * @param ?string $field |
72 | * @param ?string $validator |
73 | * @param mixed $value |
74 | * @param ?string $message |
75 | * @param ?string $prefix |
76 | */ |
77 | public function __construct( |
78 | ?string $field = null, ?string $validator = null, mixed $value = null, |
79 | ?string $message = null, ?string $prefix = 'Pop\Validator\\' |
80 | ) |
81 | { |
82 | if ($field !== null) { |
83 | $this->setField($field); |
84 | } |
85 | if ($validator !== null) { |
86 | $this->setValidator($validator); |
87 | } |
88 | if ($value !== null) { |
89 | $this->setValue($value); |
90 | } |
91 | if ($message !== null) { |
92 | $this->setMessage($message); |
93 | } |
94 | if ($prefix !== null) { |
95 | $this->setPrefix($prefix); |
96 | } |
97 | } |
98 | |
99 | /** |
100 | * Create condition from rule |
101 | * |
102 | * @param string $rule |
103 | * @return Condition |
104 | */ |
105 | public static function createFromRule(string $rule, string $prefix = 'Pop\Validator\\'): Condition |
106 | { |
107 | ['field' => $field, 'validator' => $validator, 'value' => $value, 'message' => $message] = Rule::parse($rule, $prefix); |
108 | return new static($field, $validator, $value, $message, $prefix); |
109 | } |
110 | |
111 | /** |
112 | * Create condition |
113 | * |
114 | * @param ?string $field |
115 | * @param ?string $validator |
116 | * @param mixed $value |
117 | * @param ?string $message |
118 | * @param ?string $prefix |
119 | * @return Condition |
120 | */ |
121 | public static function create( |
122 | ?string $field = null, ?string $validator = null, mixed $value = null, |
123 | ?string $message = null, ?string $prefix = 'Pop\Validator\\' |
124 | ): Condition |
125 | { |
126 | return new static($field, $validator, $value, $message, $prefix); |
127 | } |
128 | |
129 | /** |
130 | * Set the condition field |
131 | * |
132 | * @param ?string $field |
133 | * @return static |
134 | */ |
135 | public function setField(?string $field = null): static |
136 | { |
137 | $this->field = $field; |
138 | return $this; |
139 | } |
140 | |
141 | /** |
142 | * Set the condition validator |
143 | * |
144 | * @param ?string $validator |
145 | * @return static |
146 | */ |
147 | public function setValidator(?string $validator = null): static |
148 | { |
149 | $this->validator = $validator; |
150 | return $this; |
151 | } |
152 | |
153 | /** |
154 | * Set the condition value |
155 | * |
156 | * @param mixed $value |
157 | * @return static |
158 | */ |
159 | public function setValue(mixed $value = null): static |
160 | { |
161 | $this->value = $value; |
162 | return $this; |
163 | } |
164 | |
165 | /** |
166 | * Set the condition message |
167 | * |
168 | * @param ?string $message |
169 | * @return static |
170 | */ |
171 | public function setMessage(?string $message = null): static |
172 | { |
173 | $this->message = $message; |
174 | return $this; |
175 | } |
176 | |
177 | /** |
178 | * Set the condition prefix |
179 | * |
180 | * @param ?string $prefix |
181 | * @return static |
182 | */ |
183 | public function setPrefix(?string $prefix = null): static |
184 | { |
185 | $this->prefix = $prefix; |
186 | return $this; |
187 | } |
188 | |
189 | /** |
190 | * Get the condition field |
191 | * |
192 | * @return ?string |
193 | */ |
194 | public function getField(): ?string |
195 | { |
196 | return $this->field; |
197 | } |
198 | |
199 | /** |
200 | * Get the condition validator |
201 | * |
202 | * @return ?string |
203 | */ |
204 | public function getValidator(): ?string |
205 | { |
206 | return $this->validator; |
207 | } |
208 | |
209 | /** |
210 | * Get the condition value |
211 | * |
212 | * @return mixed |
213 | */ |
214 | public function getValue(): mixed |
215 | { |
216 | return $this->value; |
217 | } |
218 | |
219 | /** |
220 | * Get the condition message |
221 | * |
222 | * @return ?string |
223 | */ |
224 | public function getMessage(): ?string |
225 | { |
226 | return $this->message; |
227 | } |
228 | |
229 | /** |
230 | * Get the condition prefix |
231 | * |
232 | * @return ?string |
233 | */ |
234 | public function getPrefix(): ?string |
235 | { |
236 | return $this->prefix; |
237 | } |
238 | |
239 | /** |
240 | * Get the condition validator object |
241 | * |
242 | * @return ?AbstractValidator |
243 | */ |
244 | public function getValidatorObject(): ?AbstractValidator |
245 | { |
246 | return $this->validatorObject; |
247 | } |
248 | |
249 | /** |
250 | * Has the condition field |
251 | * |
252 | * @return bool |
253 | */ |
254 | public function hasField(): bool |
255 | { |
256 | return ($this->field !== null); |
257 | } |
258 | |
259 | /** |
260 | * Has the condition validator |
261 | * |
262 | * @return bool |
263 | */ |
264 | public function hasValidator(): bool |
265 | { |
266 | return ($this->validator !== null); |
267 | } |
268 | |
269 | /** |
270 | * Has the condition value |
271 | * |
272 | * @return bool |
273 | */ |
274 | public function hasValue(): bool |
275 | { |
276 | return ($this->value !== null); |
277 | } |
278 | |
279 | /** |
280 | * Has the condition message |
281 | * |
282 | * @return bool |
283 | */ |
284 | public function hasMessage(): bool |
285 | { |
286 | return ($this->message !== null); |
287 | } |
288 | |
289 | /** |
290 | * Has the condition prefix |
291 | * |
292 | * @return bool |
293 | */ |
294 | public function hasPrefix(): bool |
295 | { |
296 | return ($this->prefix !== null); |
297 | } |
298 | |
299 | /** |
300 | * Has the condition validator object |
301 | * |
302 | * @return bool |
303 | */ |
304 | public function hasValidatorObject(): bool |
305 | { |
306 | return ($this->validatorObject !== null); |
307 | } |
308 | |
309 | /** |
310 | * Evaluate the condition |
311 | * |
312 | * @param mixed $input |
313 | * @throws Exception |
314 | * @return bool |
315 | */ |
316 | public function evaluate(array $input): bool |
317 | { |
318 | if (!class_exists($this->prefix . $this->validator)) { |
319 | throw new Exception('Error: The condition class does not exist.'); |
320 | } |
321 | |
322 | if (!str_contains($this->field, '.') && !array_key_exists($this->field, $input)) { |
323 | throw new Exception("Error: The input data does not contain a '" . $this->field . "' field value."); |
324 | } |
325 | |
326 | // If the value references a value in the input array |
327 | if (is_string($this->value) && str_starts_with($this->value, '[') && str_ends_with($this->value, ']')) { |
328 | $value = substr($this->value, 1, -1); |
329 | if (array_key_exists($value, $input)) { |
330 | $this->value = $input[$value]; |
331 | } |
332 | } |
333 | |
334 | $class = $this->prefix . $this->validator; |
335 | |
336 | $this->validatorObject = (str_starts_with($this->validator, 'Has')) ? |
337 | new $class([$this->field => $this->value], $this->message) : new $class($this->value, $this->message); |
338 | |
339 | return $this->validatorObject->evaluate(($input[$this->field] ?? $input)); |
340 | } |
341 | |
342 | } |