Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
HasCountGreaterThan | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
11 | |
100.00% |
1 / 1 |
evaluate | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
11 |
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 | * Has count greater than 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 HasCountGreaterThan extends AbstractValidator |
27 | { |
28 | |
29 | /** |
30 | * Traits |
31 | */ |
32 | use TraverseTrait; |
33 | |
34 | /** |
35 | * Method to evaluate the validator |
36 | * |
37 | * @param mixed $input |
38 | * @throws Exception |
39 | * @return bool |
40 | */ |
41 | public function evaluate(mixed $input = null): bool |
42 | { |
43 | // Set the input, if passed |
44 | if ($input !== null) { |
45 | $this->input = $input; |
46 | } |
47 | |
48 | if (!is_array($input)) { |
49 | throw new Exception('Error: The evaluated input must be an array.'); |
50 | } |
51 | if (!is_array($this->value) || !is_numeric(reset($this->value))) { |
52 | throw new Exception("Error: The evaluated value must be an array of node name and count value, e.g. ['node' => 3]."); |
53 | } |
54 | |
55 | $field = array_key_first($this->value); |
56 | $count = reset($this->value); |
57 | |
58 | if ($this->message === null) { |
59 | $this->message = "The array must have a field '" . $field . "' with more than " . $count . " item(s)."; |
60 | } |
61 | |
62 | if (!str_contains($field, '.')) { |
63 | return (array_key_exists($field, $this->input) && |
64 | is_array($this->input[$field]) && count($this->input[$field]) > $count); |
65 | } else { |
66 | $value = []; |
67 | self::traverseData($field, $this->input, $value); |
68 | |
69 | return (is_array($value) && (isset($value[0])) && (count($value[0]) > $count)); |
70 | } |
71 | } |
72 | |
73 | } |