Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
73 / 73 |
|
100.00% |
15 / 15 |
CRAP | |
100.00% |
1 / 1 |
CheckboxSet | |
100.00% |
73 / 73 |
|
100.00% |
15 / 15 |
41 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
6 | |||
setDisabled | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
setReadonly | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
setCheckboxAttribute | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
setCheckboxAttributes | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
setValue | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
7 | |||
resetValue | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setChecked | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getChecked | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setLegend | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getLegend | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
validate | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
render | |
100.00% |
3 / 3 |
|
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 | */ |
14 | namespace Pop\Form\Element; |
15 | |
16 | use Pop\Dom\Child; |
17 | |
18 | /** |
19 | * Form checkbox 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 | |
29 | class CheckboxSet extends AbstractElement |
30 | { |
31 | |
32 | /** |
33 | * Array of checkbox input elements |
34 | * @var array |
35 | */ |
36 | protected $checkboxes = []; |
37 | |
38 | /** |
39 | * Array of checked values |
40 | * @var array |
41 | */ |
42 | protected $checked = []; |
43 | |
44 | /** |
45 | * Fieldset legend |
46 | * @var string |
47 | */ |
48 | protected $legend = null; |
49 | |
50 | /** |
51 | * Constructor |
52 | * |
53 | * Instantiate a fieldset of checkbox input form elements |
54 | * |
55 | * @param string $name |
56 | * @param array $values |
57 | * @param string|array $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', 'checkbox-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 checkbox elements and related span elements. |
76 | $i = null; |
77 | foreach ($values as $k => $v) { |
78 | $checkbox = new Input\Checkbox($name . '[]', null, $indent); |
79 | $checkbox->setAttributes([ |
80 | 'class' => 'checkbox', |
81 | 'id' => ($name . $i), |
82 | 'value' => $k |
83 | ]); |
84 | |
85 | // Determine if the current radio element is checked. |
86 | if (in_array($k, $this->checked)) { |
87 | $checkbox->check(); |
88 | } |
89 | |
90 | $span = new Child('span'); |
91 | if (null !== $indent) { |
92 | $span->setIndent($indent); |
93 | } |
94 | $span->setAttribute('class', 'checkbox-span'); |
95 | $span->setNodeValue($v); |
96 | $this->addChildren([$checkbox, $span]); |
97 | $this->checkboxes[] = $checkbox; |
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 checkbox elements |
148 | * |
149 | * @param string $a |
150 | * @param string $v |
151 | * @return Child |
152 | */ |
153 | public function setCheckboxAttribute($a, $v) |
154 | { |
155 | foreach ($this->checkboxes as $checkbox) { |
156 | $checkbox->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 checkbox elements |
167 | * |
168 | * @param array $a |
169 | * @return Child |
170 | */ |
171 | public function setCheckboxAttributes(array $a) |
172 | { |
173 | foreach ($this->checkboxes as $checkbox) { |
174 | $checkbox->setAttributes($a); |
175 | if (isset($a['tabindex'])) { |
176 | $a['tabindex']++; |
177 | } |
178 | } |
179 | return $this; |
180 | } |
181 | |
182 | |
183 | /** |
184 | * Set the checked value of the checkbox form elements |
185 | * |
186 | * @param $value |
187 | * @return CheckboxSet |
188 | */ |
189 | public function setValue($value) |
190 | { |
191 | $this->checked = (!is_array($value)) ? [$value] : $value; |
192 | |
193 | if ((count($this->checked) > 0) && ($this->hasChildren())) { |
194 | foreach ($this->childNodes as $child) { |
195 | if ($child instanceof Input\Checkbox) { |
196 | if (in_array($child->getValue(), $this->checked)) { |
197 | $child->check(); |
198 | } else { |
199 | $child->uncheck(); |
200 | } |
201 | } |
202 | } |
203 | } |
204 | return $this; |
205 | } |
206 | |
207 | /** |
208 | * Reset the value of the form element |
209 | * |
210 | * @return CheckboxSet |
211 | */ |
212 | public function resetValue() |
213 | { |
214 | $this->checked = null; |
215 | foreach ($this->childNodes as $child) { |
216 | if ($child instanceof Input\Checkbox) { |
217 | $child->uncheck(); |
218 | } |
219 | } |
220 | return $this; |
221 | } |
222 | |
223 | /** |
224 | * Get checkbox form element checked value |
225 | * |
226 | * @return mixed |
227 | */ |
228 | public function getValue() |
229 | { |
230 | return $this->checked; |
231 | } |
232 | |
233 | /** |
234 | * Set the checked value |
235 | * |
236 | * @param mixed $checked |
237 | * @return CheckboxSet |
238 | */ |
239 | public function setChecked($checked) |
240 | { |
241 | return $this->setValue($checked); |
242 | } |
243 | |
244 | /** |
245 | * Get the checked value |
246 | * |
247 | * @return string |
248 | */ |
249 | public function getChecked() |
250 | { |
251 | return $this->getValue(); |
252 | } |
253 | |
254 | /** |
255 | * Method to set fieldset legend |
256 | * |
257 | * @param string $legend |
258 | * @return CheckboxSet |
259 | */ |
260 | public function setLegend($legend) |
261 | { |
262 | $this->legend = $legend; |
263 | return $this; |
264 | } |
265 | |
266 | /** |
267 | * Method to get fieldset legend |
268 | * |
269 | * @return string |
270 | */ |
271 | public function getLegend() |
272 | { |
273 | return $this->legend; |
274 | } |
275 | |
276 | /** |
277 | * Get form element object type |
278 | * |
279 | * @return string |
280 | */ |
281 | public function getType() |
282 | { |
283 | return 'checkbox'; |
284 | } |
285 | |
286 | /** |
287 | * Validate the form element object |
288 | * |
289 | * @param array $formValues |
290 | * @return boolean |
291 | */ |
292 | public function validate(array $formValues = []) |
293 | { |
294 | $value = $this->getValue(); |
295 | |
296 | // Check if the element is required |
297 | if (($this->required) && empty($value)) { |
298 | $this->errors[] = 'This field is required.'; |
299 | } |
300 | |
301 | $this->validateValue($value, $formValues); |
302 | |
303 | return (count($this->errors) == 0); |
304 | } |
305 | |
306 | /** |
307 | * Render the child and its child nodes |
308 | * |
309 | * @param int $depth |
310 | * @param string $indent |
311 | * @param boolean $inner |
312 | * @return string |
313 | */ |
314 | public function render($depth = 0, $indent = null, $inner = false) |
315 | { |
316 | if (!empty($this->legend)) { |
317 | $this->addChild(new Child('legend', $this->legend)); |
318 | } |
319 | return parent::render($depth, $indent, $inner); |
320 | } |
321 | |
322 | } |