Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| HasOnlyOne | |
100.00% |
23 / 23 |
|
100.00% |
2 / 2 |
17 | |
100.00% |
1 / 1 |
| evaluate | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
13 | |||
| generateDefaultMessage | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| 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 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 HasOnlyOne 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 (empty($this->value)) { |
| 53 | throw new Exception('Error: The evaluated value cannot be empty.'); |
| 54 | } |
| 55 | |
| 56 | // Set the default message |
| 57 | if (!$this->hasMessage()) { |
| 58 | $this->generateDefaultMessage(); |
| 59 | } |
| 60 | |
| 61 | if (!str_contains($this->value, '.') && (!$this->hasField())) { |
| 62 | return (array_key_exists($this->value, $this->input) && |
| 63 | is_array($this->input[$this->value]) && count($this->input[$this->value]) == 1); |
| 64 | } else if ($this->hasKeyField()) { |
| 65 | ['key' => $key, 'field' => $field] = $this->getField(); |
| 66 | return (array_key_exists($key, $this->input) && array_key_exists($field, $this->input[$key]) && |
| 67 | is_array($this->input[$key][$field]) && (count($this->input[$key][$field]) == 1)); |
| 68 | } else { |
| 69 | $value = []; |
| 70 | self::traverseData($this->value, $this->input, $value); |
| 71 | return (count($value) == 1); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Generate default message |
| 77 | |
| 78 | * @param mixed $name |
| 79 | * @param mixed $value |
| 80 | * @return string |
| 81 | */ |
| 82 | public function generateDefaultMessage(mixed $name = null, mixed $value = null): string |
| 83 | { |
| 84 | if ($value === null) { |
| 85 | $value = $this->value; |
| 86 | } |
| 87 | $this->message = "The " . (($name !== null) ? "'" . $name . "'" : "input") . |
| 88 | " must contain only one item" . (($value !== null) ? " of '" . $value . "'." : "."); |
| 89 | |
| 90 | return $this->message; |
| 91 | } |
| 92 | |
| 93 | } |