Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.70% covered (success)
80.70%
46 / 57
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
HasOneThatContains
80.70% covered (success)
80.70%
46 / 57
0.00% covered (danger)
0.00%
0 / 2
35.04
0.00% covered (danger)
0.00%
0 / 1
 evaluate
79.17% covered (success)
79.17%
38 / 48
0.00% covered (danger)
0.00%
0 / 1
27.78
 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 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 HasOneThatContains 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
59        // Set the default message
60        if (!$this->hasMessage()) {
61            $this->generateDefaultMessage();
62        }
63
64        if (!str_contains($field, '.') && (!$this->hasField())) {
65            $value = (array_key_exists($field, $this->input) && !is_array($this->input[$field])) ? $this->input[$field] : null;
66        } else if ($this->hasKeyField()) {
67            ['key' => $key, 'field' => $field] = $this->getField();
68            $value = $this->input[$key][$field] ?? null;
69        } else {
70            $value = [];
71            self::traverseData($field, $this->input, $value);
72        }
73
74        if (is_array($value)) {
75            $result = false;
76            foreach ($value as $val) {
77                $needle   = $requiredValue;
78                $haystack = $val;
79
80                if (!is_array($needle) && !is_array($haystack)) {
81                    $result = (str_contains((string)$haystack, (string)$needle));
82                } else if (!is_array($needle)) {
83                    $result = in_array($needle, $haystack);
84                } else {
85                    foreach ($needle as $n) {
86                        if (is_array($haystack)) {
87                            if (in_array($n, $haystack)) {
88                                $result = true;
89                                break;
90                            }
91                        } else if (str_contains((string)$haystack, (string)$n)) {
92                            $result = true;
93                            break;
94                        }
95                    }
96                }
97                if ($result) {
98                    return true;
99                }
100            }
101            return $result;
102        } else {
103            $result   = false;
104            $needle   = $requiredValue;
105            $haystack = $value;
106
107            if (!is_array($needle)) {
108                $result = (str_contains((string)$haystack, (string)$needle));
109            } else {
110                $result = true;
111                foreach ($needle as $n) {
112                    if (!str_contains((string)$haystack, (string)$n)) {
113                        $result = false;
114                        break;
115                    }
116                }
117            }
118
119            return $result;
120        }
121    }
122
123    /**
124     * Generate default message
125
126     * @param  mixed $name
127     * @param  mixed $value
128     * @return string
129     */
130    public function generateDefaultMessage(mixed $name = null, mixed $value = null): string
131    {
132        $field = null;
133
134        if (($value !== null) && is_array($value)) {
135            $field = array_key_first($value);
136        } else if ($this->value !== null) {
137            $field = array_key_first($this->value);
138        }
139
140        $this->message = "The " . (($name !== null) ? "'" . $name . "'" : "input") .
141            " must contain one item" . (($this->value !== null) ? " of '" . $field . "'" : "") .
142            " that contains the required value.";
143
144        return $this->message;
145    }
146
147}