Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
HasOneNotEmpty
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
16
100.00% covered (success)
100.00%
1 / 1
 evaluate
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
13
 generateDefaultMessage
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2declare(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 */
15namespace Pop\Validator;
16
17/**
18 * Has one not empty 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 */
27class HasOneNotEmpty 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
53        $field = $this->value;
54
55        // Set the default message
56        if (!$this->hasMessage()) {
57            $this->generateDefaultMessage();
58        }
59
60        if (!str_contains($field, '.') && (!$this->hasField())) {
61            return (array_key_exists($field, $this->input) &&
62                !is_array($this->input[$field]) && !empty($this->input[$field]));
63        } else if ($this->hasKeyField()) {
64            ['key' => $key, 'field' => $field] = $this->getField();
65            return (array_key_exists($key, $this->input) && array_key_exists($field, $this->input[$key]) &&
66                !empty($this->input[$key][$field]));
67        } else {
68            $value = [];
69            self::traverseData($field, $this->input, $value);
70
71            foreach ($value as $val) {
72                if (!empty($val)) {
73                    return true;
74                }
75            }
76            return false;
77        }
78    }
79
80    /**
81     * Generate default message
82
83     * @param  mixed $name
84     * @param  mixed $value
85     * @return string
86     */
87    public function generateDefaultMessage(mixed $name = null, mixed $value = null): string
88    {
89        $field = $value ?? $this->value;
90
91        $this->message = "The " . (($name !== null) ? "'" . $name . "'" : "input") .
92            " must contain one item" . (($this->value !== null) ? " of '" . $field . "'" : "") .
93            " that is not empty.";
94
95        return $this->message;
96    }
97
98}