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