Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 1 |
Checkbox | |
0.00% |
0 / 20 |
|
0.00% |
0 / 11 |
132 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 10 |
|
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 | |||
getLabel | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getValue | |
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 | |||
isInline | |
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 | * Checkbox.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 `Checkbox` 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\CheckboxRenderer; |
20 | |
21 | /** |
22 | * Checkbox |
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 Checkbox { |
36 | |
37 | /** |
38 | * The ID for the checkbox input. |
39 | */ |
40 | protected string $inputId; |
41 | |
42 | /** |
43 | * The name attribute for the checkbox input. |
44 | */ |
45 | protected string $name; |
46 | |
47 | /** |
48 | * The label object for the checkbox. |
49 | */ |
50 | protected Label $label; |
51 | |
52 | /** |
53 | * The value associated with the checkbox input. |
54 | */ |
55 | protected string $value; |
56 | |
57 | /** |
58 | * Indicates if the checkbox is selected by default. |
59 | */ |
60 | protected bool $checked; |
61 | |
62 | /** |
63 | * Indicates if the checkbox is disabled. |
64 | */ |
65 | protected bool $disabled; |
66 | |
67 | /** |
68 | * Indicates if the checkbox should be displayed inline. |
69 | */ |
70 | protected bool $inline; |
71 | |
72 | /** |
73 | * Additional HTML attributes for the input. |
74 | */ |
75 | private array $inputAttributes; |
76 | |
77 | /** |
78 | * Additional attributes for the wrapper element. |
79 | */ |
80 | private array $wrapperAttributes; |
81 | |
82 | /** |
83 | * The renderer instance used to render the checkbox. |
84 | */ |
85 | protected CheckboxRenderer $renderer; |
86 | |
87 | /** |
88 | * Constructor for the Checkbox component. |
89 | * |
90 | * Initializes a Checkbox instance with the specified properties. |
91 | * |
92 | * @param string $id The ID for the checkbox input. |
93 | * @param string $name The name attribute for the checkbox input. |
94 | * @param Label $label The Label object associated with the checkbox. |
95 | * @param string $value The value associated with the checkbox input. |
96 | * @param bool $checked Indicates if the checkbox is selected by default. |
97 | * @param bool $disabled Indicates if the checkbox is disabled. |
98 | * @param bool $inline Indicates if the checkbox should be displayed inline. |
99 | * @param array $inputAttributes Additional HTML attributes for the input element. |
100 | * @param array $wrapperAttributes Additional HTML attributes for the wrapper element. |
101 | * @param CheckboxRenderer $renderer The renderer to use for rendering the checkbox. |
102 | */ |
103 | public function __construct( |
104 | string $id, |
105 | string $name, |
106 | Label $label, |
107 | string $value, |
108 | bool $checked, |
109 | bool $disabled, |
110 | bool $inline, |
111 | array $inputAttributes, |
112 | array $wrapperAttributes, |
113 | CheckboxRenderer $renderer |
114 | ) { |
115 | $this->inputId = $id; |
116 | $this->name = $name; |
117 | $this->label = $label; |
118 | $this->value = $value; |
119 | $this->checked = $checked; |
120 | $this->disabled = $disabled; |
121 | $this->inline = $inline; |
122 | $this->inputAttributes = $inputAttributes; |
123 | $this->wrapperAttributes = $wrapperAttributes; |
124 | $this->renderer = $renderer; |
125 | } |
126 | |
127 | /** |
128 | * Get the ID of the checkbox input. |
129 | * |
130 | * This method returns the unique identifier used for the checkbox input element. |
131 | * The ID is essential for associating the checkbox with its label and for targeting with JavaScript or CSS. |
132 | * |
133 | * @since 0.1.0 |
134 | * @return string The ID of the checkbox input. |
135 | */ |
136 | public function getInputId(): string { |
137 | return $this->inputId; |
138 | } |
139 | |
140 | /** |
141 | * Get the name attribute of the checkbox input. |
142 | * |
143 | * This method returns the name attribute, which is used to identify the checkbox when the form is submitted. |
144 | * The name is especially important when handling multiple checkboxes as part of a group. |
145 | * |
146 | * @since 0.1.0 |
147 | * @return string The name attribute of the checkbox input. |
148 | */ |
149 | public function getName(): string { |
150 | return $this->name; |
151 | } |
152 | |
153 | /** |
154 | * Get the label of the checkbox input. |
155 | * |
156 | * This method returns the label text associated with the checkbox. The label provides a descriptive |
157 | * name that helps users understand the purpose of the checkbox. |
158 | * |
159 | * @since 0.1.0 |
160 | * @return Label The label of the checkbox input. |
161 | */ |
162 | public function getLabel(): Label { |
163 | return $this->label; |
164 | } |
165 | |
166 | /** |
167 | * Get the value of the checkbox input. |
168 | * |
169 | * This method returns the value that is submitted when the checkbox is checked and the form is submitted. |
170 | * The value is important for differentiating between various checkboxes in a group. |
171 | * |
172 | * @since 0.1.0 |
173 | * @return string The value of the checkbox input. |
174 | */ |
175 | public function getValue(): string { |
176 | return $this->value; |
177 | } |
178 | |
179 | /** |
180 | * Check if the checkbox is selected. |
181 | * |
182 | * This method returns a boolean indicating whether the checkbox is checked by default. |
183 | * |
184 | * @since 0.1.0 |
185 | * @return bool True if the checkbox is checked, false otherwise. |
186 | */ |
187 | public function isChecked(): bool { |
188 | return $this->checked; |
189 | } |
190 | |
191 | /** |
192 | * Check if the checkbox is disabled. |
193 | * |
194 | * This method returns a boolean indicating whether the checkbox is disabled, which prevents user interaction. |
195 | * |
196 | * @since 0.1.0 |
197 | * @return bool True if the checkbox is disabled, false otherwise. |
198 | */ |
199 | public function isDisabled(): bool { |
200 | return $this->disabled; |
201 | } |
202 | |
203 | /** |
204 | * Check if the checkbox is displayed inline. |
205 | * |
206 | * This method returns a boolean indicating whether the checkbox |
207 | * and its label are displayed inline with other elements. |
208 | * |
209 | * @since 0.1.0 |
210 | * @return bool True if the checkbox is displayed inline, false otherwise. |
211 | */ |
212 | public function isInline(): bool { |
213 | return $this->inline; |
214 | } |
215 | |
216 | /** |
217 | * Get the additional HTML attributes for the checkbox input. |
218 | * |
219 | * This method returns an associative array of custom HTML attributes that are applied |
220 | * to the checkbox element. These attributes can be used to customize the checkboxes behavior |
221 | * and appearance or to enhance its integration with JavaScript. |
222 | * |
223 | * @since 0.1.0 |
224 | * @return array The additional attributes as an array. |
225 | */ |
226 | public function getInputAttributes(): array { |
227 | return $this->inputAttributes; |
228 | } |
229 | |
230 | /** |
231 | * Get additional HTML attributes for the outer wrapper element. |
232 | * |
233 | * This method returns an associative array of custom HTML attributes that are applied to the outer wrapper element, |
234 | * enhancing its behavior or styling. |
235 | * |
236 | * @since 0.1.0 |
237 | * @return array The associative array of HTML attributes for the wrapper element. |
238 | */ |
239 | public function getWrapperAttributes(): array { |
240 | return $this->wrapperAttributes; |
241 | } |
242 | |
243 | /** |
244 | * Get the component's HTML representation. |
245 | * |
246 | * This method generates the HTML markup for the component, incorporating relevant properties |
247 | * and any additional attributes. The component is structured using appropriate HTML elements |
248 | * as defined by the implementation. |
249 | * |
250 | * @since 0.1.0 |
251 | * @return string The generated HTML string for the component. |
252 | */ |
253 | public function getHtml(): string { |
254 | return $this->renderer->render( $this ); |
255 | } |
256 | } |