Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
31 / 31 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| HasCountGreaterThanEqual | |
100.00% |
31 / 31 |
|
100.00% |
2 / 2 |
21 | |
100.00% |
1 / 1 |
| evaluate | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
16 | |||
| generateDefaultMessage | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |||
| 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 count greater than or equal 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 HasCountGreaterThanEqual 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 | * @throws Exception |
| 40 | * @return bool |
| 41 | */ |
| 42 | public function evaluate(mixed $input = null): bool |
| 43 | { |
| 44 | // Set the input, if passed |
| 45 | if ($input !== null) { |
| 46 | $this->input = $input; |
| 47 | } |
| 48 | |
| 49 | if (!is_array($this->input)) { |
| 50 | throw new Exception('Error: The evaluated input must be an array.'); |
| 51 | } |
| 52 | if (!is_array($this->value) || !is_numeric(reset($this->value))) { |
| 53 | throw new Exception("Error: The evaluated value must be an array of node name and count value, e.g. ['node' => 3]."); |
| 54 | } |
| 55 | |
| 56 | $field = array_key_first($this->value); |
| 57 | $count = reset($this->value); |
| 58 | |
| 59 | // Set the default message |
| 60 | if (!$this->hasMessage()) { |
| 61 | $this->generateDefaultMessage(); |
| 62 | } |
| 63 | |
| 64 | if (!str_contains($field, '.') && (!$this->hasField())) { |
| 65 | return (array_key_exists($field, $this->input) && |
| 66 | is_array($this->input[$field]) && count($this->input[$field]) >= $count); |
| 67 | } else if ($this->hasKeyField()) { |
| 68 | ['key' => $key, 'field' => $field] = $this->getField(); |
| 69 | return (array_key_exists($key, $this->input) && array_key_exists($field, $this->input[$key]) && |
| 70 | is_array($this->input[$key][$field]) && (count($this->input[$key][$field]) >= $count)); |
| 71 | } else { |
| 72 | $value = []; |
| 73 | self::traverseData($field, $this->input, $value); |
| 74 | |
| 75 | return (isset($value[0]) && is_countable($value[0]) && (count($value[0]) >= $count)); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Generate default message |
| 81 | |
| 82 | * @param mixed $name |
| 83 | * @param mixed $value |
| 84 | * @return string |
| 85 | */ |
| 86 | public function generateDefaultMessage(mixed $name = null, mixed $value = null): string |
| 87 | { |
| 88 | $field = null; |
| 89 | $count = null; |
| 90 | |
| 91 | if (($value !== null) && is_array($value)) { |
| 92 | $field = array_key_first($value); |
| 93 | $count = reset($value); |
| 94 | } else if ($this->value !== null) { |
| 95 | $field = array_key_first($this->value); |
| 96 | $count = reset($this->value); |
| 97 | } |
| 98 | |
| 99 | $this->message = "The " . (($name !== null) ? "'" . $name . "'" : "array") . |
| 100 | " must have a field '" . $field . "' with at least " . $count . " item(s)."; |
| 101 | |
| 102 | return $this->message; |
| 103 | } |
| 104 | |
| 105 | } |