Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.32% |
56 / 62 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Select | |
90.32% |
56 / 62 |
|
75.00% |
3 / 4 |
33.99 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| setValues | |
77.78% |
21 / 27 |
|
0.00% |
0 / 1 |
18.81 | |||
| setValue | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
8 | |||
| resetValue | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (https://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/popphp-framework |
| 6 | * @author Nick Sagona, III <dev@noladev.com> |
| 7 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 8 | * @license https://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\Form\Element; |
| 15 | |
| 16 | /** |
| 17 | * Form select element class |
| 18 | * |
| 19 | * @category Pop |
| 20 | * @package Pop\Form |
| 21 | * @author Nick Sagona, III <dev@noladev.com> |
| 22 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 23 | * @license https://www.popphp.org/license New BSD License |
| 24 | * @version 4.2.2 |
| 25 | */ |
| 26 | |
| 27 | class Select 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 mixed $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 | $this->setName($name); |
| 47 | $this->setAttributes([ |
| 48 | 'name' => $name, |
| 49 | 'id' => $name |
| 50 | ]); |
| 51 | |
| 52 | if ($selected !== null) { |
| 53 | $this->setValue($selected); |
| 54 | } |
| 55 | if ($indent !== null) { |
| 56 | $this->setIndent($indent); |
| 57 | } |
| 58 | |
| 59 | $this->setValues($values); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Set the options values of the select form element |
| 64 | * |
| 65 | * @param string|array $values |
| 66 | * @param ?string $xmlFile |
| 67 | * @param ?string $indent |
| 68 | * @return Select |
| 69 | */ |
| 70 | public function setValues(string|array $values, ?string $xmlFile = null, ?string $indent = null): Select |
| 71 | { |
| 72 | $values = self::parseValues($values, $xmlFile); |
| 73 | |
| 74 | // Create the child option elements. |
| 75 | foreach ($values as $k => $v) { |
| 76 | if (is_array($v)) { |
| 77 | if (isset($v['value']) && isset($v['attributes'])) { |
| 78 | $option = new Select\Option($k, $v['value'], ['attributes' => $v['attributes']]); |
| 79 | $this->addChild($option); |
| 80 | } else { |
| 81 | $optGroup = new Select\Optgroup(); |
| 82 | if ($indent !== null) { |
| 83 | $optGroup->setIndent($indent); |
| 84 | } |
| 85 | $optGroup->setAttribute('label', $k); |
| 86 | foreach ($v as $ky => $vl) { |
| 87 | if (is_array($vl) && isset($vl['value']) && isset($vl['attributes'])) { |
| 88 | $option = new Select\Option($ky, $vl['value'], ['attributes' => $vl['attributes']]); |
| 89 | } else { |
| 90 | $option = new Select\Option($ky, $vl); |
| 91 | } |
| 92 | if ($indent !== null) { |
| 93 | $option->setIndent($indent); |
| 94 | } |
| 95 | |
| 96 | // Determine if the current option element is selected. |
| 97 | if (($this->selected !== null) && ($ky == $this->selected)) { |
| 98 | $option->select(); |
| 99 | } |
| 100 | $optGroup->addChild($option); |
| 101 | } |
| 102 | $this->addChild($optGroup); |
| 103 | } |
| 104 | } else { |
| 105 | $option = new Select\Option($k, $v); |
| 106 | if ($indent !== null) { |
| 107 | $option->setIndent($indent); |
| 108 | } |
| 109 | |
| 110 | // Determine if the current option element is selected. |
| 111 | if (($this->selected !== null) && ($k == $this->selected)) { |
| 112 | $option->select(); |
| 113 | } |
| 114 | $this->addChild($option); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return $this; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Set the selected value of the select form element |
| 123 | * |
| 124 | * @param mixed $value |
| 125 | * @return Select |
| 126 | */ |
| 127 | public function setValue(mixed $value): Select |
| 128 | { |
| 129 | $this->selected = $value; |
| 130 | |
| 131 | if ($this->hasChildren()) { |
| 132 | foreach ($this->childNodes as $child) { |
| 133 | if ($child instanceof Select\Option) { |
| 134 | if ($child->getValue() == $this->selected) { |
| 135 | $child->select(); |
| 136 | } else { |
| 137 | $child->deselect(); |
| 138 | } |
| 139 | } else if ($child instanceof Select\Optgroup) { |
| 140 | $options = $child->getOptions(); |
| 141 | foreach ($options as $option) { |
| 142 | if ($option->getValue() == $this->selected) { |
| 143 | $option->select(); |
| 144 | } else { |
| 145 | $option->deselect(); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return $this; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Reset the value of the form element |
| 157 | * |
| 158 | * @return Select |
| 159 | */ |
| 160 | public function resetValue(): Select |
| 161 | { |
| 162 | $this->selected = null; |
| 163 | |
| 164 | if ($this->hasChildren()) { |
| 165 | foreach ($this->childNodes as $child) { |
| 166 | if ($child instanceof Select\Option) { |
| 167 | $child->deselect(); |
| 168 | } else if ($child instanceof Select\Optgroup) { |
| 169 | $options = $child->getOptions(); |
| 170 | foreach ($options as $option) { |
| 171 | $option->deselect(); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return $this; |
| 178 | } |
| 179 | |
| 180 | } |