Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
43 / 43 |
|
100.00% |
23 / 23 |
CRAP | |
100.00% |
1 / 1 |
| AbstractValidator | |
100.00% |
43 / 43 |
|
100.00% |
23 / 23 |
33 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDescription | |
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 | |||
| getInput | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getField | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| getKeyFieldValue | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| getResults | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasDescription | |
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 | |||
| hasInput | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasKeyField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
3 | |||
| hasResults | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setName | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setDescription | |
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 | |||
| setInput | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setField | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| evaluate | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| generateDefaultMessage | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Validator; |
| 16 | |
| 17 | /** |
| 18 | * Abstract validator class |
| 19 | * |
| 20 | * @category Pop |
| 21 | * @package Pop\Validator |
| 22 | * @author Nick Sagona, III <nick@popphp.org> |
| 23 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 24 | * @license https://www.popphp.org/license New BSD License |
| 25 | * @version 5.0.0 |
| 26 | */ |
| 27 | abstract class AbstractValidator implements ValidatorInterface |
| 28 | { |
| 29 | |
| 30 | /** |
| 31 | * Validator name |
| 32 | * @var ?string |
| 33 | */ |
| 34 | protected mixed $name = null; |
| 35 | |
| 36 | /** |
| 37 | * Validator description |
| 38 | * @var ?string |
| 39 | */ |
| 40 | protected mixed $description = null; |
| 41 | |
| 42 | /** |
| 43 | * Validator value to test against |
| 44 | * @var mixed |
| 45 | */ |
| 46 | protected mixed $value = null; |
| 47 | |
| 48 | /** |
| 49 | * Input value to test |
| 50 | * @var mixed |
| 51 | */ |
| 52 | protected mixed $input = null; |
| 53 | |
| 54 | /** |
| 55 | * Input field |
| 56 | * @var ?string |
| 57 | */ |
| 58 | protected ?string $field = null; |
| 59 | |
| 60 | /** |
| 61 | * Validator message |
| 62 | * - The message provided when the validation fails |
| 63 | * @var ?string |
| 64 | */ |
| 65 | protected ?string $message = null; |
| 66 | |
| 67 | /** |
| 68 | * Validator results |
| 69 | * - Optional results to collect post-validation, would be something that was |
| 70 | * set by a custom validator in its "evaluate" method |
| 71 | * @var mixed |
| 72 | */ |
| 73 | protected mixed $results = null; |
| 74 | |
| 75 | /** |
| 76 | * Constructor |
| 77 | * |
| 78 | * Instantiate the validator object |
| 79 | * |
| 80 | * @param mixed $value |
| 81 | * @param ?string $message |
| 82 | * @param ?string $name |
| 83 | * @param ?string $description |
| 84 | */ |
| 85 | public function __construct(mixed $value = null, ?string $message = null, ?string $name = null, ?string $description = null) |
| 86 | { |
| 87 | $this->setValue($value); |
| 88 | if ($message !== null) { |
| 89 | $this->setMessage($message); |
| 90 | } |
| 91 | if ($name !== null) { |
| 92 | $this->setName($name); |
| 93 | } |
| 94 | if ($description !== null) { |
| 95 | $this->setDescription($description); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get the validator name |
| 101 | * |
| 102 | * @return ?string |
| 103 | */ |
| 104 | public function getName(): ?string |
| 105 | { |
| 106 | return $this->name; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get the validator description |
| 111 | * |
| 112 | * @return ?string |
| 113 | */ |
| 114 | public function getDescription(): ?string |
| 115 | { |
| 116 | return $this->description; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get the validator value |
| 121 | * |
| 122 | * @return mixed |
| 123 | */ |
| 124 | public function getValue(): mixed |
| 125 | { |
| 126 | return $this->value; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Get the validator default message |
| 131 | * |
| 132 | * @return string|null |
| 133 | */ |
| 134 | public function getMessage(): string|null |
| 135 | { |
| 136 | return $this->message; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get the validator input |
| 141 | * |
| 142 | * @return mixed |
| 143 | */ |
| 144 | public function getInput(): mixed |
| 145 | { |
| 146 | return $this->input; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Get the validator field |
| 151 | * |
| 152 | * @param bool $parse |
| 153 | * @return string|array|null |
| 154 | */ |
| 155 | public function getField(bool $parse = true): string|array|null |
| 156 | { |
| 157 | if (str_contains($this->field, '[') && str_ends_with($this->field, ']')) { |
| 158 | $key = substr($this->field, 0, strpos($this->field, '[')); |
| 159 | $field = substr($this->field, strpos($this->field, '[') + 1, -1); |
| 160 | |
| 161 | return ['key' => $key, 'field' => $field]; |
| 162 | } else { |
| 163 | return $this->field; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Get the validator key field value |
| 169 | * |
| 170 | * @return mixed |
| 171 | */ |
| 172 | public function getKeyFieldValue(): mixed |
| 173 | { |
| 174 | $inputValue = null; |
| 175 | |
| 176 | ['key' => $key, 'field' => $field] = $this->getField(); |
| 177 | if (!empty($key) && !empty($field) && isset($this->input[$key][$field])) { |
| 178 | $inputValue = $this->input[$key][$field]; |
| 179 | } |
| 180 | |
| 181 | return $inputValue; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Get the validator results |
| 186 | * |
| 187 | * @return mixed |
| 188 | */ |
| 189 | public function getResults(): mixed |
| 190 | { |
| 191 | return $this->results; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Has validator name |
| 196 | * |
| 197 | * @return bool |
| 198 | */ |
| 199 | public function hasName(): bool |
| 200 | { |
| 201 | return ($this->name !== null); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Has validator description |
| 206 | * |
| 207 | * @return bool |
| 208 | */ |
| 209 | public function hasDescription(): bool |
| 210 | { |
| 211 | return ($this->description !== null); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Has validator value |
| 216 | * |
| 217 | * @return bool |
| 218 | */ |
| 219 | public function hasValue(): bool |
| 220 | { |
| 221 | return ($this->value !== null); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Has validator message |
| 226 | * |
| 227 | * @return bool |
| 228 | */ |
| 229 | public function hasMessage(): bool |
| 230 | { |
| 231 | return ($this->message !== null); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Has validator input |
| 236 | * |
| 237 | * @return bool |
| 238 | */ |
| 239 | public function hasInput(): bool |
| 240 | { |
| 241 | return ($this->input !== null); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Has validator field |
| 246 | * |
| 247 | * @return bool |
| 248 | */ |
| 249 | public function hasField(): bool |
| 250 | { |
| 251 | return ($this->field !== null); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Has validator key field |
| 256 | * |
| 257 | * @return bool |
| 258 | */ |
| 259 | public function hasKeyField(): bool |
| 260 | { |
| 261 | return (($this->field !== null) && str_contains($this->field, '[') && is_array($this->input)); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Has validator results |
| 266 | * |
| 267 | * @return bool |
| 268 | */ |
| 269 | public function hasResults(): bool |
| 270 | { |
| 271 | return !empty($this->results); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Set the validator name |
| 276 | * |
| 277 | * @param ?string $name |
| 278 | * @return static |
| 279 | */ |
| 280 | public function setName(?string $name = null): static |
| 281 | { |
| 282 | $this->name = $name; |
| 283 | return $this; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Set the validator description |
| 288 | * |
| 289 | * @param ?string $description |
| 290 | * @return static |
| 291 | */ |
| 292 | public function setDescription(?string $description = null): static |
| 293 | { |
| 294 | $this->description = $description; |
| 295 | return $this; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Set the validator value |
| 300 | * |
| 301 | * @param mixed $value |
| 302 | * @return static |
| 303 | */ |
| 304 | public function setValue(mixed $value): static |
| 305 | { |
| 306 | $this->value = $value; |
| 307 | return $this; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Set the validator condition |
| 312 | * |
| 313 | * @param ?string $message |
| 314 | * @return AbstractValidator |
| 315 | */ |
| 316 | public function setMessage(?string $message = null): AbstractValidator |
| 317 | { |
| 318 | $this->message = $message; |
| 319 | return $this; |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Set the validator input |
| 324 | * |
| 325 | * @param mixed $input |
| 326 | * @return AbstractValidator |
| 327 | */ |
| 328 | public function setInput(mixed $input = null): AbstractValidator |
| 329 | { |
| 330 | $this->input = $input; |
| 331 | return $this; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Set the validator field |
| 336 | * |
| 337 | * @param ?string $field |
| 338 | * @return AbstractValidator |
| 339 | */ |
| 340 | public function setField(?string $field = null): AbstractValidator |
| 341 | { |
| 342 | $this->field = $field; |
| 343 | return $this; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Evaluate |
| 348 | |
| 349 | * @param mixed $input |
| 350 | * @return bool |
| 351 | */ |
| 352 | abstract public function evaluate(mixed $input = null): bool; |
| 353 | |
| 354 | /** |
| 355 | * Generate default message |
| 356 | |
| 357 | * @param mixed $name |
| 358 | * @param mixed $value |
| 359 | * @return string |
| 360 | */ |
| 361 | abstract public function generateDefaultMessage(mixed $name = null, mixed $value = null): string; |
| 362 | |
| 363 | } |