Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
73 / 73
100.00% covered (success)
100.00%
15 / 15
CRAP
100.00% covered (success)
100.00%
1 / 1
RadioSet
100.00% covered (success)
100.00%
73 / 73
100.00% covered (success)
100.00%
15 / 15
41
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
7
 setDisabled
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 setReadonly
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 setRadioAttribute
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 setRadioAttributes
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 setValue
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
 resetValue
100.00% covered (success)
100.00%
5 / 5
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
 setLegend
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getLegend
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validate
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 render
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
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@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Form\Element;
15
16use Pop\Dom\Child;
17
18/**
19 * Form radio element set class
20 *
21 * @category   Pop
22 * @package    Pop\Form
23 * @author     Nick Sagona, III <dev@nolainteractive.com>
24 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
25 * @license    http://www.popphp.org/license     New BSD License
26 * @version    3.6.0
27 */
28
29class RadioSet extends AbstractElement
30{
31
32    /**
33     * Array of radio input elements
34     * @var array
35     */
36    protected $radios = [];
37
38    /**
39     * Array of checked values
40     * @var string
41     */
42    protected $checked = null;
43
44    /**
45     * Fieldset legend
46     * @var string
47     */
48    protected $legend = null;
49
50    /**
51     * Constructor
52     *
53     * Instantiate the radio input form elements
54     *
55     * @param  string $name
56     * @param  array  $values
57     * @param  string $checked
58     * @param  string $indent
59     */
60    public function __construct($name, array $values, $checked = null, $indent = null)
61    {
62        parent::__construct('fieldset');
63
64        $this->setName($name);
65        $this->setAttribute('class', 'radio-fieldset');
66
67        if (null !== $checked) {
68            $this->setValue($checked);
69        }
70
71        if (null !== $indent) {
72            $this->setIndent($indent);
73        }
74
75        // Create the radio elements and related span elements.
76        $i = null;
77        foreach ($values as $k => $v) {
78            $radio = new Input\Radio($name, null, $indent);
79            $radio->setAttributes([
80                'class' => 'radio',
81                'id'    => ($name . $i),
82                'value' => $k
83            ]);
84
85            // Determine if the current radio element is checked.
86            if ((null !== $this->checked) && ($k == $this->checked)) {
87                $radio->check();
88            }
89
90            $span = new Child('span');
91            if (null !== $indent) {
92                $span->setIndent($indent);
93            }
94            $span->setAttribute('class', 'radio-span');
95            $span->setNodeValue($v);
96            $this->addChildren([$radio, $span]);
97            $this->radios[] = $radio;
98            $i++;
99        }
100    }
101
102    /**
103     * Set whether the form element is disabled
104     *
105     * @param  boolean $disabled
106     * @return Select
107     */
108    public function setDisabled($disabled)
109    {
110        if ($disabled) {
111            foreach ($this->childNodes as $childNode) {
112                $childNode->setAttribute('disabled', 'disabled');
113            }
114        } else {
115            foreach ($this->childNodes as $childNode) {
116                $childNode->removeAttribute('disabled');
117            }
118        }
119
120        return parent::setDisabled($disabled);
121    }
122
123    /**
124     * Set whether the form element is readonly
125     *
126     * @param  boolean $readonly
127     * @return Select
128     */
129    public function setReadonly($readonly)
130    {
131        if ($readonly) {
132            foreach ($this->childNodes as $childNode) {
133                $childNode->setAttribute('readonly', 'readonly');
134                $childNode->setAttribute('onclick', 'return false;');
135            }
136        } else {
137            foreach ($this->childNodes as $childNode) {
138                $childNode->removeAttribute('readonly');
139                $childNode->removeAttribute('onclick');
140            }
141        }
142
143        return parent::setReadonly($readonly);
144    }
145
146    /**
147     * Set an attribute for the input radio elements
148     *
149     * @param  string $a
150     * @param  string $v
151     * @return Child
152     */
153    public function setRadioAttribute($a, $v)
154    {
155        foreach ($this->radios as $radio) {
156            $radio->setAttribute($a, $v);
157            if ($a == 'tabindex') {
158                $v++;
159            }
160
161        }
162        return $this;
163    }
164
165    /**
166     * Set an attribute or attributes for the input radio elements
167     *
168     * @param  array $a
169     * @return Child
170     */
171    public function setRadioAttributes(array $a)
172    {
173        foreach ($this->radios as $radio) {
174            $radio->setAttributes($a);
175            if (isset($a['tabindex'])) {
176                $a['tabindex']++;
177            }
178        }
179        return $this;
180    }
181
182    /**
183     * Set the checked value of the radio form elements
184     *
185     * @param  mixed $value
186     * @return RadioSet
187     */
188    public function setValue($value)
189    {
190        $this->checked = $value;
191
192        if ((null !== $this->checked) && ($this->hasChildren())) {
193            foreach ($this->childNodes as $child) {
194                if ($child instanceof Input\Radio) {
195                    if ($child->getValue() == $this->checked) {
196                        $child->check();
197                    } else {
198                        $child->uncheck();
199                    }
200                }
201            }
202        }
203        return $this;
204    }
205
206    /**
207     * Reset the value of the form element
208     *
209     * @return RadioSet
210     */
211    public function resetValue()
212    {
213        $this->checked = null;
214        foreach ($this->childNodes as $child) {
215            if ($child instanceof Input\Radio) {
216                $child->uncheck();
217            }
218        }
219        return $this;
220    }
221
222    /**
223     * Get radio form element checked value
224     *
225     * @return mixed
226     */
227    public function getValue()
228    {
229        return $this->checked;
230    }
231
232    /**
233     * Get form element object type
234     *
235     * @return string
236     */
237    public function getType()
238    {
239        return 'radio';
240    }
241
242    /**
243     * Set the checked value
244     *
245     * @param  mixed $checked
246     * @return RadioSet
247     */
248    public function setChecked($checked)
249    {
250        return $this->setValue($checked);
251    }
252
253    /**
254     * Get the checked value
255     *
256     * @return string
257     */
258    public function getChecked()
259    {
260        return $this->getValue();
261    }
262
263    /**
264     * Method to set fieldset legend
265     *
266     * @param  string $legend
267     * @return RadioSet
268     */
269    public function setLegend($legend)
270    {
271        $this->legend = $legend;
272        return $this;
273    }
274
275    /**
276     * Method to get fieldset legend
277     *
278     * @return string
279     */
280    public function getLegend()
281    {
282        return $this->legend;
283    }
284
285    /**
286     * Validate the form element object
287     *
288     * @param  array $formValues
289     * @return boolean
290     */
291    public function validate(array $formValues = [])
292    {
293        $value = $this->getValue();
294
295        // Check if the element is required
296        if (($this->required) && empty($value)) {
297            $this->errors[] = 'This field is required.';
298        }
299
300        $this->validateValue($value, $formValues);
301
302        return (count($this->errors) == 0);
303    }
304
305    /**
306     * Render the child and its child nodes
307     *
308     * @param  int     $depth
309     * @param  string  $indent
310     * @param  boolean $inner
311     * @return string
312     */
313    public function render($depth = 0, $indent = null, $inner = false)
314    {
315        if (!empty($this->legend)) {
316            $this->addChild(new Child('legend', $this->legend));
317        }
318        return parent::render($depth, $indent, $inner);
319    }
320
321}