Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (success)
83.33%
40 / 48
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
HasOnlyOneThatContains
83.33% covered (success)
83.33%
40 / 48
0.00% covered (danger)
0.00%
0 / 2
36.74
0.00% covered (danger)
0.00%
0 / 1
 evaluate
82.05% covered (success)
82.05%
32 / 39
0.00% covered (danger)
0.00%
0 / 1
29.91
 generateDefaultMessage
88.89% covered (success)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
6.05
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 only one that contains 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 */
27class HasOnlyOneThatContains 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)) {
53            throw new Exception("Error: The evaluated value must be an array of node name and value, e.g. ['node' => 3].");
54        }
55
56        $field         = array_key_first($this->value);
57        $requiredValue = reset($this->value);
58        $count         = 0;
59
60        // Set the default message
61        if (!$this->hasMessage()) {
62            $this->generateDefaultMessage();
63        }
64
65        if (!str_contains($field, '.')) {
66            $value = (array_key_exists($field, $this->input) && !is_array($this->input[$field])) ? $this->input[$field] : null;
67        } else {
68            $value = [];
69            self::traverseData($field, $this->input, $value);
70        }
71
72        if (is_array($value)) {
73            foreach ($value as $val) {
74                $needle   = $requiredValue;
75                $haystack = $val;
76
77                if (!is_array($needle) && !is_array($haystack) && str_contains((string)$haystack, (string)$needle)) {
78                    $count++;
79                } else if (!is_array($needle) && is_array($haystack) && in_array($needle, $haystack)) {
80                    $count++;
81                } else if (is_array($needle)) {
82                    foreach ($needle as $n) {
83                        if (is_array($haystack)) {
84                            if (in_array($n, $haystack)) {
85                                $count++;
86                            }
87                        } else if (str_contains((string)$haystack, (string)$n)) {
88                            $count++;
89                        }
90                    }
91                }
92            }
93        } else {
94            $needle   = $requiredValue;
95            $haystack = $value;
96
97            if (!is_array($needle) && str_contains((string)$haystack, (string)$needle)) {
98                $count++;
99            } else if (is_array($needle)) {
100                foreach ($needle as $n) {
101                    if (str_contains((string)$haystack, (string)$n)) {
102                        $count++;
103                    }
104                }
105            }
106        }
107
108        return ($count == 1);
109    }
110
111    /**
112     * Generate default message
113
114     * @param  mixed $name
115     * @param  mixed $value
116     * @return string
117     */
118    public function generateDefaultMessage(mixed $name = null, mixed $value = null): string
119    {
120        $field = null;
121
122        if (($value !== null) && is_array($value)) {
123            $field = array_key_first($value);
124        } else if ($this->value !== null) {
125            $field = array_key_first($this->value);
126        }
127
128        $this->message = "The " . (($name !== null) ? "'" . $name . "'" : "input") .
129            " must have only one item" . (($this->value !== null) ? " of '" . $field . "'" : "") .
130            " that contains the required value.";
131
132        return $this->message;
133    }
134
135}