Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| LengthBetweenInclude | |
100.00% |
21 / 21 |
|
100.00% |
2 / 2 |
15 | |
100.00% |
1 / 1 |
| evaluate | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
7 | |||
| generateDefaultMessage | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
8 | |||
| 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 | * Length between/include 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 | class LengthBetweenInclude extends AbstractValidator |
| 28 | { |
| 29 | |
| 30 | /** |
| 31 | * Method to evaluate the validator |
| 32 | * |
| 33 | * @param mixed $input |
| 34 | * @throws Exception |
| 35 | * @return bool |
| 36 | */ |
| 37 | public function evaluate(mixed $input = null): bool |
| 38 | { |
| 39 | if (!is_array($this->value)) { |
| 40 | throw new Exception('The value must be an array.'); |
| 41 | } else if (count($this->value) != 2) { |
| 42 | throw new Exception('The value must be an array that contains 2 values.'); |
| 43 | } |
| 44 | |
| 45 | // Set the input, if passed |
| 46 | if ($input !== null) { |
| 47 | $this->input = $input; |
| 48 | } |
| 49 | |
| 50 | // Set the default message |
| 51 | if (!$this->hasMessage()) { |
| 52 | $this->generateDefaultMessage(); |
| 53 | } |
| 54 | |
| 55 | $inputValue = ($this->hasKeyField()) ? $this->getKeyFieldValue() : $this->input; |
| 56 | |
| 57 | return ((strlen((string)$inputValue) >= $this->value[0]) && (strlen((string)$inputValue) <= $this->value[1])); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Generate default message |
| 62 | |
| 63 | * @param mixed $name |
| 64 | * @param mixed $value |
| 65 | * @return string |
| 66 | */ |
| 67 | public function generateDefaultMessage(mixed $name = null, mixed $value = null): string |
| 68 | { |
| 69 | $value1 = null; |
| 70 | $value2 = null; |
| 71 | if (($value !== null) && is_array($value) && (count($value) == 2)) { |
| 72 | $value1 = $value[0]; |
| 73 | $value2 = $value[1]; |
| 74 | } else if (($this->value !== null) && is_array($this->value) && (count($this->value) == 2)) { |
| 75 | $value1 = $this->value[0]; |
| 76 | $value2 = $this->value[1]; |
| 77 | } |
| 78 | |
| 79 | $this->message = "The " . (($name !== null) ? "'" . $name . "'" : "value") . |
| 80 | " length must be between or equal to '" . $value1 . "' and '" . $value2 . "'."; |
| 81 | |
| 82 | return $this->message; |
| 83 | } |
| 84 | |
| 85 | } |