Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.49% |
55 / 57 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
SelectMultiple | |
96.49% |
55 / 57 |
|
66.67% |
2 / 3 |
28 | |
0.00% |
0 / 1 |
__construct | |
93.94% |
31 / 33 |
|
0.00% |
0 / 1 |
13.04 | |||
setValue | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
9 | |||
resetValue | |
100.00% |
10 / 10 |
|
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@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2024 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 | /** |
17 | * Form select multiple element class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Form |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 4.0.0 |
25 | */ |
26 | |
27 | class SelectMultiple extends AbstractSelect |
28 | { |
29 | |
30 | /** |
31 | * Constructor |
32 | * |
33 | * Instantiate the select form element object |
34 | * |
35 | * @param string $name |
36 | * @param string|array $values |
37 | * @param string|array $selected |
38 | * @param ?string $xmlFile |
39 | * @param ?string $indent |
40 | */ |
41 | public function __construct( |
42 | string $name, string|array $values, mixed $selected = null, ?string $xmlFile = null, ?string $indent = null |
43 | ) |
44 | { |
45 | parent::__construct('select'); |
46 | |
47 | $this->setName($name); |
48 | $this->setAttributes([ |
49 | 'name' => $name . '[]', |
50 | 'id' => $name, |
51 | 'multiple' => 'multiple' |
52 | ]); |
53 | |
54 | if ($indent !== null) { |
55 | $this->setIndent($indent); |
56 | } |
57 | |
58 | $values = self::parseValues($values, $xmlFile); |
59 | |
60 | // Create the child option elements. |
61 | foreach ($values as $k => $v) { |
62 | if (is_array($v)) { |
63 | $optGroup = new Select\Optgroup(); |
64 | if ($indent !== null) { |
65 | $optGroup->setIndent($indent); |
66 | } |
67 | $optGroup->setAttribute('label', $k); |
68 | foreach ($v as $ky => $vl) { |
69 | $option = new Select\Option($ky, $vl); |
70 | if ($indent !== null) { |
71 | $option->setIndent($indent); |
72 | } |
73 | |
74 | // Determine if the current option element is selected. |
75 | if (is_array($this->selected) && in_array($ky, $this->selected, true)) { |
76 | $option->select(); |
77 | } |
78 | $optGroup->addChild($option); |
79 | } |
80 | $this->addChild($optGroup); |
81 | } else { |
82 | $option = new Select\Option($k, $v); |
83 | if ($indent !== null) { |
84 | $option->setIndent($indent); |
85 | } |
86 | |
87 | // Determine if the current option element is selected. |
88 | if (is_array($this->selected) && in_array($k, $this->selected, true)) { |
89 | $option->select(); |
90 | } |
91 | $this->addChild($option); |
92 | } |
93 | } |
94 | |
95 | if ($selected !== null) { |
96 | $this->setValue($selected); |
97 | } else { |
98 | $this->selected = []; |
99 | } |
100 | } |
101 | |
102 | /** |
103 | * Set the selected value of the select form element |
104 | * |
105 | * @param mixed $value |
106 | * @return SelectMultiple |
107 | */ |
108 | public function setValue(mixed $value): SelectMultiple |
109 | { |
110 | $this->selected = (!is_array($value)) ? [$value] : $value; |
111 | |
112 | if ($this->hasChildren()) { |
113 | foreach ($this->childNodes as $child) { |
114 | if ($child instanceof Select\Option) { |
115 | if (in_array($child->getValue(), $this->selected)) { |
116 | $child->select(); |
117 | } else { |
118 | $child->deselect(); |
119 | } |
120 | } else if ($child instanceof Select\Optgroup) { |
121 | $options = $child->getOptions(); |
122 | foreach ($options as $option) { |
123 | if (in_array($option->getValue(), $this->selected)) { |
124 | $option->select(); |
125 | } else { |
126 | $option->deselect(); |
127 | } |
128 | } |
129 | } |
130 | } |
131 | } |
132 | |
133 | return $this; |
134 | } |
135 | |
136 | /** |
137 | * Reset the value of the form element |
138 | * |
139 | * @return SelectMultiple |
140 | */ |
141 | public function resetValue(): SelectMultiple |
142 | { |
143 | $this->selected = []; |
144 | |
145 | if ($this->hasChildren()) { |
146 | foreach ($this->childNodes as $child) { |
147 | if ($child instanceof Select\Option) { |
148 | $child->deselect(); |
149 | } else if ($child instanceof Select\Optgroup) { |
150 | $options = $child->getOptions(); |
151 | foreach ($options as $option) { |
152 | $option->deselect(); |
153 | } |
154 | } |
155 | } |
156 | } |
157 | |
158 | return $this; |
159 | } |
160 | |
161 | } |