Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
RadioSet
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
9 / 9
25
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
10
 setRadioAttribute
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setRadioAttributes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setValue
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
6
 resetValue
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setChecked
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getChecked
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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\Form\Element;
16
17use Pop\Dom\Child;
18
19/**
20 * Form radio element set class
21 *
22 * @category   Pop
23 * @package    Pop\Form
24 * @author     Nick Sagona, III <nick@popphp.org>
25 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
26 * @license    https://www.popphp.org/license     New BSD License
27 * @version    5.0.0
28 */
29
30class RadioSet extends AbstractInputSet
31{
32
33    /**
34     * Array of checked values
35     * @var ?string
36     */
37    protected ?string $checked = null;
38
39    /**
40     * Constructor
41     *
42     * Instantiate the radio input form elements
43     *
44     * @param  string  $name
45     * @param  array   $values
46     * @param  ?string $checked
47     * @param  ?string $indent
48     * @param  ?string $container
49     */
50    public function __construct(string $name, array $values, ?string $checked = null, ?string $indent = null, ?string $container = null)
51    {
52        parent::__construct('fieldset');
53
54        $this->setName($name);
55        $this->setAttribute('class', 'radio-fieldset');
56
57        if ($checked !== null) {
58            $this->setValue($checked);
59        }
60
61        if ($indent !== null) {
62            $this->setIndent($indent);
63        }
64
65        if ($container !== null) {
66            $this->setContainer($container);
67        }
68
69        // Create the radio elements and related span elements.
70        $i = null;
71        foreach ($values as $k => $v) {
72            $radio = new Input\Radio($name, null, $indent);
73            $radio->setAttributes([
74                'class' => 'radio',
75                'id'    => ($name . $i),
76                'value' => $k
77            ]);
78
79            if (is_array($v) && isset($v['value']) && isset($v['attributes'])) {
80                $nodeValue = $v['value'];
81                $radio->setAttributes($v['attributes']);
82            } else {
83                $nodeValue = $v;
84            }
85
86            // Determine if the current radio element is checked.
87            if (($this->checked !== null) && ($k == $this->checked)) {
88                $radio->check();
89            }
90
91            $this->appendInputWithSpan($radio, $nodeValue, 'radio-span', $indent, 'radio-fieldset-container');
92            $i++;
93        }
94    }
95
96    /**
97     * Set an attribute for the input radio elements
98     *
99     * @param  string $a
100     * @param  string $v
101     * @return Child
102     */
103    public function setRadioAttribute(string $a, string $v): Child
104    {
105        return $this->setInputAttribute($a, $v);
106    }
107
108    /**
109     * Set an attribute or attributes for the input radio elements
110     *
111     * @param  array $a
112     * @return Child
113     */
114    public function setRadioAttributes(array $a): Child
115    {
116        return $this->setInputAttributes($a);
117    }
118
119    /**
120     * Set the checked value of the radio form elements
121     *
122     * @param  mixed $value
123     * @return RadioSet
124     */
125    public function setValue(mixed $value = null): RadioSet
126    {
127        $childNodes    = $this->getFieldsetChildNodes();
128        $this->checked = $value;
129
130        if (($this->checked !== null) && ($this->hasChildren())) {
131            foreach ($childNodes as $child) {
132                if ($child instanceof Input\Radio) {
133                    if ($child->getValue() == $this->checked) {
134                        $child->check();
135                    } else {
136                        $child->uncheck();
137                    }
138                }
139            }
140        }
141        return $this;
142    }
143
144    /**
145     * Reset the value of the form element
146     *
147     * @return RadioSet
148     */
149    public function resetValue(): RadioSet
150    {
151        $childNodes    = $this->getFieldsetChildNodes();
152        $this->checked = null;
153        foreach ($childNodes as $child) {
154            if ($child instanceof Input\Radio) {
155                $child->uncheck();
156            }
157        }
158        return $this;
159    }
160
161    /**
162     * Get radio form element checked value
163     *
164     * @return mixed
165     */
166    public function getValue(): mixed
167    {
168        return $this->checked;
169    }
170
171    /**
172     * Get form element object type
173     *
174     * @return string
175     */
176    public function getType(): string
177    {
178        return 'radio';
179    }
180
181    /**
182     * Set the checked value
183     *
184     * @param  mixed $checked
185     * @return RadioSet
186     */
187    public function setChecked(mixed $checked): RadioSet
188    {
189        return $this->setValue($checked);
190    }
191
192    /**
193     * Get the checked value
194     *
195     * @return mixed
196     */
197    public function getChecked(): mixed
198    {
199        return $this->getValue();
200    }
201
202}