Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.43% covered (success)
96.43%
27 / 28
50.00% covered (warning)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
HasOneThatEquals
96.43% covered (success)
96.43%
27 / 28
50.00% covered (warning)
50.00%
1 / 2
16
0.00% covered (danger)
0.00%
0 / 1
 evaluate
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
10.01
 generateDefaultMessage
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@noladev.com>
7 * @copyright  Copyright (c) 2009-2026 NOLA Interactive, LLC.
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Validator;
15
16/**
17 * Has one that equals validator class
18 *
19 * @category   Pop
20 * @package    Pop\Validator
21 * @author     Nick Sagona, III <dev@noladev.com>
22 * @copyright  Copyright (c) 2009-2026 NOLA Interactive, LLC.
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    4.6.5
25 */
26class HasOneThatEquals extends AbstractValidator
27{
28
29    /**
30     * Traits
31     */
32    use TraverseTrait;
33
34    /**
35     * Method to evaluate the validator
36     *
37     * @param  mixed $input
38     * @throws Exception
39     * @return bool
40     */
41    public function evaluate(mixed $input = null): bool
42    {
43        // Set the input, if passed
44        if ($input !== null) {
45            $this->input = $input;
46        }
47
48        if (!is_array($input)) {
49            throw new Exception('Error: The evaluated input must be an array.');
50        }
51        if (!is_array($this->value)) {
52            throw new Exception("Error: The evaluated value must be an array of node name and value, e.g. ['node' => 3].");
53        }
54
55        $field         = array_key_first($this->value);
56        $requiredValue = reset($this->value);
57
58        // Set the default message
59        if (!$this->hasMessage()) {
60            $this->generateDefaultMessage();
61        }
62
63        if (!str_contains($field, '.')) {
64            return (array_key_exists($field, $this->input) &&
65                !is_array($this->input[$field]) && ($this->input[$field] == $requiredValue));
66        } else {
67            $value = [];
68            self::traverseData($field, $this->input, $value);
69
70            if (is_array($value)) {
71                return (is_array($requiredValue)) ?
72                    !empty(array_intersect($requiredValue, $value)) : in_array($requiredValue, $value);
73            } else {
74                return ($value == $requiredValue);
75            }
76        }
77    }
78
79    /**
80     * Generate default message
81
82     * @param  mixed $name
83     * @param  mixed $value
84     * @return string
85     */
86    public function generateDefaultMessage(mixed $name = null, mixed $value = null): string
87    {
88        $field = null;
89
90        if (($value !== null) && is_array($value)) {
91            $field = array_key_first($value);
92        } else if ($this->value !== null) {
93            $field = array_key_first($this->value);
94        }
95
96        $this->message = "The " . (($name !== null) ? "'" . $name . "'" : "value") .
97            " must contain one item" . (($this->value !== null) ? " of '" . $field . "'" : "") .
98            " with the required value.";
99
100        return $this->message;
101    }
102
103}