Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (success)
88.89%
32 / 36
50.00% covered (warning)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
HasOneThatEquals
88.89% covered (success)
88.89%
32 / 36
50.00% covered (warning)
50.00%
1 / 2
25.86
0.00% covered (danger)
0.00%
0 / 1
 evaluate
85.19% covered (success)
85.19%
23 / 27
0.00% covered (danger)
0.00%
0 / 1
20.17
 generateDefaultMessage
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
6
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 equals 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 HasOneThatEquals 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     * @param  bool  $isDateTime
40     * @return bool
41     *@throws Exception
42     */
43    public function evaluate(mixed $input = null, bool $isDateTime = false): bool
44    {
45        // Set the input, if passed
46        if ($input !== null) {
47            $this->input = $input;
48        }
49
50        if (!is_array($this->input)) {
51            throw new Exception('Error: The evaluated input must be an array.');
52        }
53        if (!is_array($this->value)) {
54            throw new Exception("Error: The evaluated value must be an array of node name and value, e.g. ['node' => 3].");
55        }
56
57        $field         = array_key_first($this->value);
58        $requiredValue = reset($this->value);
59
60        // Set the default message
61        if (!$this->hasMessage()) {
62            $this->generateDefaultMessage();
63        }
64
65        if (!str_contains($field, '.') && (!$this->hasField())) {
66            if ($isDateTime) {
67                return (array_key_exists($field, $this->input) &&
68                    !is_array($this->input[$field]) && (strtotime($this->input[$field]) == strtotime($requiredValue)));
69            } else {
70                return (array_key_exists($field, $this->input) &&
71                    !is_array($this->input[$field]) && ($this->input[$field] == $requiredValue));
72            }
73        } else if ($this->hasKeyField()) {
74            ['key' => $key, 'field' => $field] = $this->getField();
75            if ($isDateTime) {
76                return (array_key_exists($key, $this->input) && array_key_exists($field, $this->input[$key]) &&
77                    (strtotime($this->input[$key][$field]) == strtotime($requiredValue)));
78            } else {
79                return (array_key_exists($key, $this->input) && array_key_exists($field, $this->input[$key]) &&
80                    ($this->input[$key][$field] == $requiredValue));
81            }
82        } else {
83            $value = [];
84            self::traverseData($field, $this->input, $value);
85
86            return (is_array($requiredValue)) ?
87                !empty(array_intersect($requiredValue, $value)) : in_array($requiredValue, $value);
88        }
89    }
90
91    /**
92     * Generate default message
93
94     * @param  mixed $name
95     * @param  mixed $value
96     * @return string
97     */
98    public function generateDefaultMessage(mixed $name = null, mixed $value = null): string
99    {
100        $field = null;
101
102        if (($value !== null) && is_array($value)) {
103            $field = array_key_first($value);
104        } else if ($this->value !== null) {
105            $field = array_key_first($this->value);
106        }
107
108        $this->message = "The " . (($name !== null) ? "'" . $name . "'" : "input") .
109            " must contain one item" . (($this->value !== null) ? " of '" . $field . "'" : "") .
110            " with the required value.";
111
112        return $this->message;
113    }
114
115}