Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.37% |
35 / 41 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| HasOneLessThan | |
85.37% |
35 / 41 |
|
0.00% |
0 / 2 |
30.46 | |
0.00% |
0 / 1 |
| evaluate | |
84.38% |
27 / 32 |
|
0.00% |
0 / 1 |
23.85 | |||
| generateDefaultMessage | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
6.05 | |||
| 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 | * Has one that is less than 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 HasOneLessThan extends AbstractValidator |
| 28 | { |
| 29 | |
| 30 | /** |
| 31 | * Traits |
| 32 | */ |
| 33 | use TraverseTrait; |
| 34 | |
| 35 | /** |
| 36 | * Method to evaluate the validator |
| 37 | * |
| 38 | * @param mixed $input |
| 39 | * @param bool $isDateTime |
| 40 | * @return bool |
| 41 | *@throws Exception |
| 42 | */ |
| 43 | public function evaluate(mixed $input = null, bool $isDateTime = false): bool |
| 44 | { |
| 45 | // Set the input, if passed |
| 46 | if ($input !== null) { |
| 47 | $this->input = $input; |
| 48 | } |
| 49 | |
| 50 | if (!is_array($this->input)) { |
| 51 | throw new Exception('Error: The evaluated input must be an array.'); |
| 52 | } |
| 53 | if (!is_array($this->value)) { |
| 54 | throw new Exception("Error: The evaluated value must be an array of node name and value, e.g. ['node' => 3]."); |
| 55 | } |
| 56 | |
| 57 | $field = array_key_first($this->value); |
| 58 | $requiredValue = reset($this->value); |
| 59 | |
| 60 | // Set the default message |
| 61 | if (!$this->hasMessage()) { |
| 62 | $this->generateDefaultMessage(); |
| 63 | } |
| 64 | |
| 65 | if (!str_contains($field, '.') && (!$this->hasField())) { |
| 66 | if ($isDateTime) { |
| 67 | return (array_key_exists($field, $this->input) && |
| 68 | !is_array($this->input[$field]) && (strtotime($this->input[$field]) < strtotime($requiredValue))); |
| 69 | } else { |
| 70 | return (array_key_exists($field, $this->input) && |
| 71 | !is_array($this->input[$field]) && ($this->input[$field] < $requiredValue)); |
| 72 | } |
| 73 | } else if ($this->hasKeyField()) { |
| 74 | ['key' => $key, 'field' => $field] = $this->getField(); |
| 75 | if ($isDateTime) { |
| 76 | return (array_key_exists($key, $this->input) && array_key_exists($field, $this->input[$key]) && |
| 77 | (strtotime($this->input[$key][$field]) < strtotime($requiredValue))); |
| 78 | } else { |
| 79 | return (array_key_exists($key, $this->input) && array_key_exists($field, $this->input[$key]) && |
| 80 | ($this->input[$key][$field] < $requiredValue)); |
| 81 | } |
| 82 | } else { |
| 83 | $value = []; |
| 84 | self::traverseData($field, $this->input, $value); |
| 85 | |
| 86 | foreach ($value as $val) { |
| 87 | if ($isDateTime) { |
| 88 | if (strtotime($val) < strtotime($requiredValue)) { |
| 89 | return true; |
| 90 | } |
| 91 | } else { |
| 92 | if ($val < $requiredValue) { |
| 93 | return true; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Generate default message |
| 103 | |
| 104 | * @param mixed $name |
| 105 | * @param mixed $value |
| 106 | * @return string |
| 107 | */ |
| 108 | public function generateDefaultMessage(mixed $name = null, mixed $value = null): string |
| 109 | { |
| 110 | $field = null; |
| 111 | |
| 112 | if (($value !== null) && is_array($value)) { |
| 113 | $field = array_key_first($value); |
| 114 | } else if ($this->value !== null) { |
| 115 | $field = array_key_first($this->value); |
| 116 | } |
| 117 | |
| 118 | $this->message = "The " . (($name !== null) ? "'" . $name . "'" : "input") . |
| 119 | " must contain one item" . (($this->value !== null) ? " of '" . $field . "'" : "") . |
| 120 | " that is less than the required value."; |
| 121 | |
| 122 | return $this->message; |
| 123 | } |
| 124 | |
| 125 | } |