Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 13 |
CRAP | |
0.00% |
0 / 1 |
| Select | |
0.00% |
0 / 20 |
|
0.00% |
0 / 13 |
210 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getOptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getOptGroups | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAttributes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSelectedOption | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isDisabled | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setId | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setOptions | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setOptGroups | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setAttributes | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| setDisabled | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setSelectedOption | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | /** |
| 5 | * Select.php |
| 6 | * |
| 7 | * This file is part of the Codex design system, the official design system for Wikimedia projects. |
| 8 | * It contains the definition and implementation of the `Select` class, responsible for managing |
| 9 | * the behavior and properties of the corresponding component. |
| 10 | * |
| 11 | * @category Component |
| 12 | * @package Codex\Component |
| 13 | * @since 0.1.0 |
| 14 | * @author Doğu Abaris <abaris@null.net> |
| 15 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
| 16 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
| 17 | */ |
| 18 | |
| 19 | namespace Wikimedia\Codex\Component; |
| 20 | |
| 21 | use Wikimedia\Codex\Contract\Component; |
| 22 | use Wikimedia\Codex\Renderer\SelectRenderer; |
| 23 | |
| 24 | /** |
| 25 | * Select |
| 26 | * |
| 27 | * @category Component |
| 28 | * @package Codex\Component |
| 29 | * @since 0.1.0 |
| 30 | * @author Doğu Abaris <abaris@null.net> |
| 31 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
| 32 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
| 33 | */ |
| 34 | class Select extends Component { |
| 35 | private string $id = ''; |
| 36 | |
| 37 | public function __construct( |
| 38 | SelectRenderer $renderer, |
| 39 | private array $options, |
| 40 | private array $optGroups, |
| 41 | private ?string $selectedOption, |
| 42 | private bool $disabled, |
| 43 | private array $attributes |
| 44 | ) { |
| 45 | parent::__construct( $renderer ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get the Select's HTML ID attribute. |
| 50 | * |
| 51 | * This method returns the ID assigned to the select element, which is used |
| 52 | * for identifying the select component in the HTML document. |
| 53 | * |
| 54 | * @since 0.1.0 |
| 55 | * @return string The ID of the Select element. |
| 56 | */ |
| 57 | public function getId(): string { |
| 58 | return $this->id; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get the options for the select element. |
| 63 | * |
| 64 | * This method returns an associative array where the keys are the option values |
| 65 | * and the values are the display text shown to the user. |
| 66 | * |
| 67 | * @since 0.1.0 |
| 68 | * @return array The associative array of options for the select element. |
| 69 | */ |
| 70 | public function getOptions(): array { |
| 71 | return $this->options; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get the optGroups for the select element. |
| 76 | * |
| 77 | * This method returns an associative array of optGroups, where each key is a label for a group |
| 78 | * and the value is an array of options within that group. |
| 79 | * |
| 80 | * @since 0.1.0 |
| 81 | * @return array The associative array of optGroups for the select element. |
| 82 | */ |
| 83 | public function getOptGroups(): array { |
| 84 | return $this->optGroups; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get the additional HTML attributes for the `<select>` element. |
| 89 | * |
| 90 | * This method returns an associative array of custom HTML attributes that are applied to the `<select>` element, |
| 91 | * such as `id`, `data-*`, `aria-*`, or any other valid attributes. |
| 92 | * |
| 93 | * @since 0.1.0 |
| 94 | * @return array The additional attributes as an array. |
| 95 | */ |
| 96 | public function getAttributes(): array { |
| 97 | return $this->attributes; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get the currently selected option value. |
| 102 | * |
| 103 | * This method returns the value of the currently selected option in the select element. |
| 104 | * |
| 105 | * @since 0.1.0 |
| 106 | * @return string|null The value of the currently selected option, or null if no option is selected. |
| 107 | */ |
| 108 | public function getSelectedOption(): ?string { |
| 109 | return $this->selectedOption; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Check if the select element is disabled. |
| 114 | * |
| 115 | * This method returns a boolean value indicating whether the select element is disabled. |
| 116 | * If true, the `disabled` attribute is present on the `<select>` element. |
| 117 | * |
| 118 | * @since 0.1.0 |
| 119 | * @return bool True if the select element is disabled, false otherwise. |
| 120 | */ |
| 121 | public function isDisabled(): bool { |
| 122 | return $this->disabled; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Set the Selects HTML ID attribute. |
| 127 | * |
| 128 | * @deprecated Use setAttributes() to set the ID |
| 129 | * @since 0.1.0 |
| 130 | * @param string $id The ID for the Select element. |
| 131 | * @return $this |
| 132 | */ |
| 133 | public function setId( string $id ): self { |
| 134 | $this->id = $id; |
| 135 | |
| 136 | return $this; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Set one or more options for the select element. |
| 141 | * |
| 142 | * This method allows one or more options to be added to the select dropdown. |
| 143 | * Each option can be provided as a simple key-value pair, or as an array with `value`, `text`, |
| 144 | * and `selected` keys for more complex options. |
| 145 | * |
| 146 | * Example usage: |
| 147 | * |
| 148 | * // Using key-value pairs: |
| 149 | * $select->setOptions([ |
| 150 | * 'value1' => 'Label 1', |
| 151 | * 'value2' => 'Label 2' |
| 152 | * ]); |
| 153 | * |
| 154 | * // Using an array for more complex options: |
| 155 | * $select->setOptions([ |
| 156 | * ['value' => 'value1', 'text' => 'Label 1', 'selected' => true], |
| 157 | * ['value' => 'value2', 'text' => 'Label 2'] |
| 158 | * ]); |
| 159 | * |
| 160 | * @since 0.1.0 |
| 161 | * @param array $options An array of options, either as key-value pairs or |
| 162 | * arrays with `value`, `text`, and `selected` keys. |
| 163 | * @return $this Returns the Select instance for method chaining. |
| 164 | */ |
| 165 | public function setOptions( array $options ): self { |
| 166 | $this->options = array_merge( $this->options, $options ); |
| 167 | |
| 168 | return $this; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Set the optGroups for the select element. |
| 173 | * |
| 174 | * This method allows options to be grouped under labels in the select dropdown. |
| 175 | * Each optGroup can contain options that are either key-value pairs or arrays with `value`, |
| 176 | * `text`, and `selected` keys for more complex options. |
| 177 | * |
| 178 | * Example usage: |
| 179 | * |
| 180 | * $select->setOptGroups([ |
| 181 | * 'Group 1' => [ |
| 182 | * 'value1' => 'Option 1', |
| 183 | * ['value' => 'value2', 'text' => 'Option 2', 'selected' => true] |
| 184 | * ], |
| 185 | * 'Group 2' => [ |
| 186 | * 'value3' => 'Option 3', |
| 187 | * 'value4' => 'Option 4' |
| 188 | * ] |
| 189 | * ]); |
| 190 | * |
| 191 | * @since 0.1.0 |
| 192 | * @param array $optGroups An associative array of optGroups where keys are labels and values are arrays of options. |
| 193 | * @return $this Returns the Select instance for method chaining. |
| 194 | */ |
| 195 | public function setOptGroups( array $optGroups ): self { |
| 196 | $this->optGroups = array_merge( $this->optGroups, $optGroups ); |
| 197 | return $this; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Set additional HTML attributes for the `<select>` element. |
| 202 | * |
| 203 | * This method allows custom HTML attributes to be added to the `<select>` element, |
| 204 | * such as `id`, `data-*`, `aria-*`, or any other valid attributes. These attributes can be used |
| 205 | * to enhance accessibility or integrate with JavaScript. |
| 206 | * |
| 207 | * Example usage: |
| 208 | * |
| 209 | * $select->setAttributes([ |
| 210 | * 'id' => 'select-example', |
| 211 | * 'data-category' => 'selection', |
| 212 | * ]); |
| 213 | * |
| 214 | * @since 0.1.0 |
| 215 | * @param array $attributes An associative array of HTML attributes. |
| 216 | * @return $this Returns the Select instance for method chaining. |
| 217 | */ |
| 218 | public function setAttributes( array $attributes ): self { |
| 219 | foreach ( $attributes as $key => $value ) { |
| 220 | $this->attributes[$key] = $value; |
| 221 | } |
| 222 | return $this; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Set whether the select element should be disabled. |
| 227 | * |
| 228 | * This method disables the select element, preventing user interaction. |
| 229 | * When called with `true`, the `disabled` attribute is added to the `<select>` element. |
| 230 | * |
| 231 | * Example usage: |
| 232 | * |
| 233 | * $select->setDisabled(true); |
| 234 | * |
| 235 | * @since 0.1.0 |
| 236 | * @param bool $disabled Indicates whether the select element should be disabled. |
| 237 | * @return $this Returns the Select instance for method chaining. |
| 238 | */ |
| 239 | public function setDisabled( bool $disabled ): self { |
| 240 | $this->disabled = $disabled; |
| 241 | |
| 242 | return $this; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Set the selected option for the select element. |
| 247 | * |
| 248 | * This method specifies which option should be selected by default when the select element is rendered. |
| 249 | * |
| 250 | * @since 0.1.0 |
| 251 | * @param string|null $value The value of the option to be selected, or null to unset the selection. |
| 252 | * @return $this Returns the Select instance for method chaining. |
| 253 | */ |
| 254 | public function setSelectedOption( ?string $value ): self { |
| 255 | $this->selectedOption = $value; |
| 256 | |
| 257 | return $this; |
| 258 | } |
| 259 | } |