Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
HasOnlyOneThatEquals
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
2 / 2
18
100.00% covered (success)
100.00%
1 / 1
 evaluate
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
12
 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-2025 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-2025 NOLA Interactive, LLC.
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    4.5.0
25 */
26class HasOnlyOneThatEquals 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        $result = false;
49
50        if (!is_array($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.name' => 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, '.')) {
66            throw new Exception("Error: The evaluated value must been an array with a column name, e.g. 'users.username'.");
67        }
68
69        $parent = substr($field, 0, strrpos($field, '.'));
70        $child  = substr($field, (strrpos($field, '.') + 1));
71        $value  = [];
72        $count  = 0;
73        self::traverseData($parent, $this->input, $value);
74
75        foreach ($value as $val) {
76            if (is_array($val)) {
77                foreach ($val as $item) {
78                    if (is_array($item) && isset($item[$child]) && ($item[$child] == $requiredValue)) {
79                        $count++;
80                    }
81                }
82            }
83        }
84
85        return ($count == 1);
86    }
87
88    /**
89     * Generate default message
90
91     * @param  mixed $name
92     * @param  mixed $value
93     * @return string
94     */
95    public function generateDefaultMessage(mixed $name = null, mixed $value = null): string
96    {
97        $field = null;
98
99        if (($value !== null) && is_array($value)) {
100            $field = array_key_first($value);
101        } else if ($this->value !== null) {
102            $field = array_key_first($this->value);
103        }
104
105        $this->message = "The " . (($name !== null) ? "'" . $name . "'" : "value") .
106            " must contain one item" . (($this->value !== null) ? " of '" . $field . "'" : "") .
107            " with the required value.";
108
109        return $this->message;
110    }
111
112}