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
CheckboxSet
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
9
 setCheckboxAttribute
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCheckboxAttributes
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
7
 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
 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
 getType
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 checkbox 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 CheckboxSet extends AbstractInputSet
31{
32
33    /**
34     * Array of checked values
35     * @var array
36     */
37    protected array $checked = [];
38
39    /**
40     * Constructor
41     *
42     * Instantiate a fieldset of checkbox input form elements
43     *
44     * @param  string            $name
45     * @param  array             $values
46     * @param  string|array|null $checked
47     * @param  ?string           $indent
48     * @param  ?string           $container
49     */
50    public function __construct(string $name, array $values, string|array|null $checked = null, ?string $indent = null, ?string $container = null)
51    {
52        parent::__construct('fieldset');
53
54        $this->setName($name);
55        $this->setAttribute('class', 'checkbox-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 checkbox elements and related span elements.
70        $i = null;
71        foreach ($values as $k => $v) {
72            $checkbox = new Input\Checkbox($name . '[]', null, $indent);
73            $checkbox->setAttributes([
74                'class' => 'checkbox',
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                $checkbox->setAttributes($v['attributes']);
82            } else {
83                $nodeValue = $v;
84            }
85
86            // Determine if the current checkbox element is checked.
87            if (in_array($k, $this->checked)) {
88                $checkbox->check();
89            }
90
91            $this->appendInputWithSpan($checkbox, $nodeValue, 'checkbox-span', $indent, 'checkbox-fieldset-container');
92            $i++;
93        }
94    }
95
96    /**
97     * Set an attribute for the input checkbox elements
98     *
99     * @param  string $a
100     * @param  string $v
101     * @return Child
102     */
103    public function setCheckboxAttribute(string $a, string $v): Child
104    {
105        return $this->setInputAttribute($a, $v);
106    }
107
108    /**
109     * Set an attribute or attributes for the input checkbox elements
110     *
111     * @param  array $a
112     * @return Child
113     */
114    public function setCheckboxAttributes(array $a): Child
115    {
116        return $this->setInputAttributes($a);
117    }
118
119    /**
120     * Set the checked value of the checkbox form elements
121     *
122     * @param  mixed $value
123     * @return CheckboxSet
124     */
125    public function setValue(mixed $value = null): CheckboxSet
126    {
127        $this->checked = (!is_array($value)) ? [$value] : $value;
128
129        if ((count($this->checked) > 0) && ($this->hasChildren())) {
130            $childNodes = $this->getFieldsetChildNodes();
131            foreach ($childNodes as $child) {
132                if ($child instanceof Input\Checkbox) {
133                    if (in_array($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 CheckboxSet
148     */
149    public function resetValue(): CheckboxSet
150    {
151        $childNodes    = $this->getFieldsetChildNodes();
152        $this->checked = [];
153
154        foreach ($childNodes as $child) {
155            if ($child instanceof Input\Checkbox) {
156                $child->uncheck();
157            }
158        }
159        return $this;
160    }
161
162    /**
163     * Get checkbox form element checked value
164     *
165     * @return mixed
166     */
167    public function getValue(): mixed
168    {
169        return $this->checked;
170    }
171
172    /**
173     * Set the checked value
174     *
175     * @param  mixed $checked
176     * @return CheckboxSet
177     */
178    public function setChecked(mixed $checked): CheckboxSet
179    {
180        return $this->setValue($checked);
181    }
182
183    /**
184     * Get the checked value
185     *
186     * @return mixed
187     */
188    public function getChecked(): mixed
189    {
190        return $this->getValue();
191    }
192
193    /**
194     * Get form element object type
195     *
196     * @return string
197     */
198    public function getType(): string
199    {
200        return 'checkbox';
201    }
202
203}