Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
29 / 29 |
|
100.00% |
18 / 18 |
CRAP | |
100.00% |
1 / 1 |
| AbstractValidator | |
100.00% |
29 / 29 |
|
100.00% |
18 / 18 |
21 | |
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 | |||
| 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 | |||
| 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 | |||
| evaluate | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| generateDefaultMessage | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 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 | * Abstract 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 | abstract class AbstractValidator implements ValidatorInterface |
| 27 | { |
| 28 | |
| 29 | /** |
| 30 | * Validator name |
| 31 | * @var ?string |
| 32 | */ |
| 33 | protected mixed $name = null; |
| 34 | |
| 35 | /** |
| 36 | * Validator description |
| 37 | * @var ?string |
| 38 | */ |
| 39 | protected mixed $description = null; |
| 40 | |
| 41 | /** |
| 42 | * Validator value to test against |
| 43 | * @var mixed |
| 44 | */ |
| 45 | protected mixed $value = null; |
| 46 | |
| 47 | /** |
| 48 | * Input value to test |
| 49 | * @var mixed |
| 50 | */ |
| 51 | protected mixed $input = null; |
| 52 | |
| 53 | /** |
| 54 | * Validator message |
| 55 | * - The message provided when the validation fails |
| 56 | * @var ?string |
| 57 | */ |
| 58 | protected ?string $message = null; |
| 59 | |
| 60 | /** |
| 61 | * Validator results |
| 62 | * - Optional results to collect post-validation, would be something that was |
| 63 | * set by a custom validator in its "evaluate" method |
| 64 | * @var mixed |
| 65 | */ |
| 66 | protected mixed $results = null; |
| 67 | |
| 68 | /** |
| 69 | * Constructor |
| 70 | * |
| 71 | * Instantiate the validator object |
| 72 | * |
| 73 | * @param mixed $value |
| 74 | * @param ?string $message |
| 75 | * @param ?string $name |
| 76 | * @param ?string $description |
| 77 | */ |
| 78 | public function __construct(mixed $value = null, ?string $message = null, ?string $name = null, ?string $description = null) |
| 79 | { |
| 80 | $this->setValue($value); |
| 81 | if ($message !== null) { |
| 82 | $this->setMessage($message); |
| 83 | } |
| 84 | if ($name !== null) { |
| 85 | $this->setName($name); |
| 86 | } |
| 87 | if ($description !== null) { |
| 88 | $this->setDescription($description); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get the validator name |
| 94 | * |
| 95 | * @return ?string |
| 96 | */ |
| 97 | public function getName(): ?string |
| 98 | { |
| 99 | return $this->name; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get the validator description |
| 104 | * |
| 105 | * @return ?string |
| 106 | */ |
| 107 | public function getDescription(): ?string |
| 108 | { |
| 109 | return $this->description; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Get the validator value |
| 114 | * |
| 115 | * @return mixed |
| 116 | */ |
| 117 | public function getValue(): mixed |
| 118 | { |
| 119 | return $this->value; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Get the validator default message |
| 124 | * |
| 125 | * @return string|null |
| 126 | */ |
| 127 | public function getMessage(): string|null |
| 128 | { |
| 129 | return $this->message; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Get the validator input |
| 134 | * |
| 135 | * @return mixed |
| 136 | */ |
| 137 | public function getInput(): mixed |
| 138 | { |
| 139 | return $this->input; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Get the validator results |
| 144 | * |
| 145 | * @return mixed |
| 146 | */ |
| 147 | public function getResults(): mixed |
| 148 | { |
| 149 | return $this->results; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Has validator name |
| 154 | * |
| 155 | * @return bool |
| 156 | */ |
| 157 | public function hasName(): bool |
| 158 | { |
| 159 | return ($this->name !== null); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Has validator description |
| 164 | * |
| 165 | * @return bool |
| 166 | */ |
| 167 | public function hasDescription(): bool |
| 168 | { |
| 169 | return ($this->description !== null); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Has validator value |
| 174 | * |
| 175 | * @return bool |
| 176 | */ |
| 177 | public function hasValue(): bool |
| 178 | { |
| 179 | return ($this->value !== null); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Has validator message |
| 184 | * |
| 185 | * @return bool |
| 186 | */ |
| 187 | public function hasMessage(): bool |
| 188 | { |
| 189 | return ($this->message !== null); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Has validator input |
| 194 | * |
| 195 | * @return bool |
| 196 | */ |
| 197 | public function hasInput(): bool |
| 198 | { |
| 199 | return ($this->input !== null); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Has validator results |
| 204 | * |
| 205 | * @return bool |
| 206 | */ |
| 207 | public function hasResults(): bool |
| 208 | { |
| 209 | return !empty($this->results); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Set the validator name |
| 214 | * |
| 215 | * @param ?string $name |
| 216 | * @return static |
| 217 | */ |
| 218 | public function setName(?string $name = null): static |
| 219 | { |
| 220 | $this->name = $name; |
| 221 | return $this; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Set the validator description |
| 226 | * |
| 227 | * @param ?string $description |
| 228 | * @return static |
| 229 | */ |
| 230 | public function setDescription(?string $description = null): static |
| 231 | { |
| 232 | $this->description = $description; |
| 233 | return $this; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Set the validator value |
| 238 | * |
| 239 | * @param mixed $value |
| 240 | * @return AbstractValidator |
| 241 | */ |
| 242 | public function setValue(mixed $value): AbstractValidator |
| 243 | { |
| 244 | $this->value = $value; |
| 245 | return $this; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Set the validator condition |
| 250 | * |
| 251 | * @param ?string $message |
| 252 | * @return AbstractValidator |
| 253 | */ |
| 254 | public function setMessage(?string $message = null): AbstractValidator |
| 255 | { |
| 256 | $this->message = $message; |
| 257 | return $this; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Set the validator input |
| 262 | * |
| 263 | * @param mixed $input |
| 264 | * @return AbstractValidator |
| 265 | */ |
| 266 | public function setInput(mixed $input = null): AbstractValidator |
| 267 | { |
| 268 | $this->input = $input; |
| 269 | return $this; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Evaluate |
| 274 | |
| 275 | * @param mixed $input |
| 276 | * @return bool |
| 277 | */ |
| 278 | abstract public function evaluate(mixed $input = null): bool; |
| 279 | |
| 280 | /** |
| 281 | * Generate default message |
| 282 | |
| 283 | * @param mixed $name |
| 284 | * @param mixed $value |
| 285 | * @return string |
| 286 | */ |
| 287 | abstract public function generateDefaultMessage(mixed $name = null, mixed $value = null): string; |
| 288 | |
| 289 | } |