Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 18 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
ToggleSwitch | |
0.00% |
0 / 18 |
|
0.00% |
0 / 10 |
110 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
getInputId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getValue | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getLabel | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isChecked | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isDisabled | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getInputAttributes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getWrapperAttributes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getHtml | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * ToggleSwitch.php |
4 | * |
5 | * This file is part of the Codex design system, the official design system for Wikimedia projects. |
6 | * It contains the definition and implementation of the `ToggleSwitch` class, responsible for managing |
7 | * the behavior and properties of the corresponding component. |
8 | * |
9 | * @category Component |
10 | * @package Codex\Component |
11 | * @since 0.1.0 |
12 | * @author Doğu Abaris <abaris@null.net> |
13 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
14 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
15 | */ |
16 | |
17 | namespace Wikimedia\Codex\Component; |
18 | |
19 | use Wikimedia\Codex\Renderer\ToggleSwitchRenderer; |
20 | |
21 | /** |
22 | * ToggleSwitch |
23 | * |
24 | * This class is part of the Codex PHP library and is responsible for |
25 | * representing an immutable object. It is primarily intended for use |
26 | * with a builder class to construct its instances. |
27 | * |
28 | * @category Component |
29 | * @package Codex\Component |
30 | * @since 0.1.0 |
31 | * @author Doğu Abaris <abaris@null.net> |
32 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
33 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
34 | */ |
35 | class ToggleSwitch { |
36 | |
37 | /** |
38 | * The ID for the toggle switch input. |
39 | */ |
40 | protected string $inputId; |
41 | |
42 | /** |
43 | * The name attribute for the toggle switch input. |
44 | */ |
45 | protected string $name; |
46 | |
47 | /** |
48 | * The value associated with the toggle switch input. |
49 | */ |
50 | protected string $value; |
51 | |
52 | /** |
53 | * The label object for the toggle switch. |
54 | */ |
55 | protected Label $label; |
56 | |
57 | /** |
58 | * Whether the toggle is checked by default. |
59 | */ |
60 | protected bool $checked; |
61 | |
62 | /** |
63 | * Whether the toggle is disabled. |
64 | */ |
65 | protected bool $disabled; |
66 | |
67 | /** |
68 | * Additional HTML attributes for the input element. |
69 | */ |
70 | protected array $inputAttributes; |
71 | |
72 | /** |
73 | * Additional HTML attributes for the wrapper element. |
74 | */ |
75 | protected array $wrapperAttributes; |
76 | |
77 | /** |
78 | * The renderer instance used to render the toggle switch. |
79 | */ |
80 | protected ToggleSwitchRenderer $renderer; |
81 | |
82 | /** |
83 | * Constructor for the ToggleSwitch component. |
84 | * |
85 | * @param string $inputId The ID for the toggle switch input. |
86 | * @param string $name The name attribute for the toggle switch input. |
87 | * @param string $value The value associated with the toggle switch input. |
88 | * @param Label $label The label object for the toggle switch. |
89 | * @param bool $checked Whether the toggle switch is checked by default. |
90 | * @param bool $disabled Whether the toggle switch is disabled. |
91 | * @param array $inputAttributes Additional HTML attributes for the input element. |
92 | * @param array $wrapperAttributes Additional HTML attributes for the wrapper element. |
93 | * @param ToggleSwitchRenderer $renderer The renderer to use for rendering the toggle switch. |
94 | */ |
95 | public function __construct( |
96 | string $inputId, |
97 | string $name, |
98 | string $value, |
99 | Label $label, |
100 | bool $checked, |
101 | bool $disabled, |
102 | array $inputAttributes, |
103 | array $wrapperAttributes, |
104 | ToggleSwitchRenderer $renderer |
105 | ) { |
106 | $this->inputId = $inputId; |
107 | $this->name = $name; |
108 | $this->value = $value; |
109 | $this->label = $label; |
110 | $this->checked = $checked; |
111 | $this->disabled = $disabled; |
112 | $this->inputAttributes = $inputAttributes; |
113 | $this->wrapperAttributes = $wrapperAttributes; |
114 | $this->renderer = $renderer; |
115 | } |
116 | |
117 | /** |
118 | * Get the ID for the toggle switch input. |
119 | * |
120 | * This method returns the unique identifier for the toggle switch input element. |
121 | * The ID is used to associate the input with its corresponding label and for any JavaScript or CSS targeting. |
122 | * |
123 | * @since 0.1.0 |
124 | * @return string The ID for the toggle switch input. |
125 | */ |
126 | public function getInputId(): string { |
127 | return $this->inputId; |
128 | } |
129 | |
130 | /** |
131 | * Get the name attribute of the toggle switch input. |
132 | * |
133 | * This method returns the name attribute, which is used to identify the toggle switch when the form is submitted. |
134 | * |
135 | * @since 0.1.0 |
136 | * @return string The name attribute of the toggle switch input. |
137 | */ |
138 | public function getName(): string { |
139 | return $this->name; |
140 | } |
141 | |
142 | /** |
143 | * Get the value associated with the toggle switch input. |
144 | * |
145 | * This method returns the value that is submitted when the toggle switch is checked and the form is submitted. |
146 | * |
147 | * @since 0.1.0 |
148 | * @return string The value of the toggle switch input. |
149 | */ |
150 | public function getValue(): string { |
151 | return $this->value; |
152 | } |
153 | |
154 | /** |
155 | * Get the label object for the toggle switch. |
156 | * |
157 | * This method returns the label object that provides a descriptive label for the toggle switch. |
158 | * The label is crucial for accessibility and usability. |
159 | * |
160 | * @since 0.1.0 |
161 | * @return Label The label object for the toggle switch. |
162 | */ |
163 | public function getLabel(): Label { |
164 | return $this->label; |
165 | } |
166 | |
167 | /** |
168 | * Check if the toggle switch is checked by default. |
169 | * |
170 | * This method returns a boolean value indicating whether the toggle switch is checked by default. |
171 | * If true, the toggle switch is rendered in a checked state. |
172 | * |
173 | * @since 0.1.0 |
174 | * @return bool True if the toggle switch is checked, false otherwise. |
175 | */ |
176 | public function isChecked(): bool { |
177 | return $this->checked; |
178 | } |
179 | |
180 | /** |
181 | * Check if the toggle switch is disabled. |
182 | * |
183 | * This method returns a boolean value indicating whether the toggle switch is disabled, |
184 | * preventing user interaction. A disabled toggle switch cannot be checked or unchecked by the user. |
185 | * |
186 | * @since 0.1.0 |
187 | * @return bool True if the toggle switch is disabled, false otherwise. |
188 | */ |
189 | public function isDisabled(): bool { |
190 | return $this->disabled; |
191 | } |
192 | |
193 | /** |
194 | * Get the additional HTML attributes for the toggle switch input. |
195 | * |
196 | * This method returns an associative array of custom HTML attributes for the toggle switch input element, |
197 | * such as `id`, `data-*`, `aria-*`, or any other valid attributes. |
198 | * |
199 | * @since 0.1.0 |
200 | * @return array The additional attributes as an array. |
201 | */ |
202 | public function getInputAttributes(): array { |
203 | return $this->inputAttributes; |
204 | } |
205 | |
206 | /** |
207 | * Get additional HTML attributes for the outer wrapper element. |
208 | * |
209 | * This method returns an associative array of custom HTML attributes that are applied to the outer wrapper element, |
210 | * enhancing its behavior or styling. |
211 | * |
212 | * @since 0.1.0 |
213 | * @return array The associative array of HTML attributes for the wrapper element. |
214 | */ |
215 | public function getWrapperAttributes(): array { |
216 | return $this->wrapperAttributes; |
217 | } |
218 | |
219 | /** |
220 | * Get the component's HTML representation. |
221 | * |
222 | * This method generates the HTML markup for the component, incorporating relevant properties |
223 | * and any additional attributes. The component is structured using appropriate HTML elements |
224 | * as defined by the implementation. |
225 | * |
226 | * @since 0.1.0 |
227 | * @return string The generated HTML string for the component. |
228 | */ |
229 | public function getHtml(): string { |
230 | return $this->renderer->render( $this ); |
231 | } |
232 | } |