Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 1 |
Radio | |
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 | * Radio.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 `Radio` 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\RadioRenderer; |
20 | |
21 | /** |
22 | * Radio |
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 Radio { |
36 | |
37 | /** |
38 | * The ID for the Radio input. |
39 | */ |
40 | protected string $inputId; |
41 | |
42 | /** |
43 | * The name attribute for the radio input. |
44 | */ |
45 | protected string $name; |
46 | |
47 | /** |
48 | * The label object for the radio. |
49 | */ |
50 | protected Label $label; |
51 | |
52 | /** |
53 | * The value associated with the radio input. |
54 | */ |
55 | protected string $value; |
56 | |
57 | /** |
58 | * Indicates if the radio is selected by default. |
59 | */ |
60 | protected bool $checked; |
61 | |
62 | /** |
63 | * Indicates if the radio is disabled. |
64 | */ |
65 | protected bool $disabled; |
66 | |
67 | /** |
68 | * Indicates if the radio 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 radio. |
84 | */ |
85 | protected RadioRenderer $renderer; |
86 | |
87 | /** |
88 | * Constructor for the Radio component. |
89 | * |
90 | * Initializes a Radio instance with the specified properties. |
91 | * |
92 | * @param string $id The ID for the radio input. |
93 | * @param string $name The name attribute for the radio input. |
94 | * @param Label $label The Label object associated with the radio. |
95 | * @param string $value The value associated with the radio input. |
96 | * @param bool $checked Indicates if the radio is selected by default. |
97 | * @param bool $disabled Indicates if the radio is disabled. |
98 | * @param bool $inline Indicates if the radio 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 RadioRenderer $renderer The renderer to use for rendering the radio. |
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 | RadioRenderer $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 for the radio input. |
129 | * |
130 | * This method returns the unique identifier for the radio input element. The ID is used to associate the input |
131 | * with its corresponding label and for any JavaScript or CSS targeting. |
132 | * |
133 | * @since 0.1.0 |
134 | * @return string The ID for the radio input. |
135 | */ |
136 | public function getInputId(): string { |
137 | return $this->inputId; |
138 | } |
139 | |
140 | /** |
141 | * Get the name attribute for the radio input. |
142 | * |
143 | * This method returns the name attribute used to identify form data after the form is submitted. |
144 | * It is crucial when handling groups of radio buttons where only one option can be selected at a time. |
145 | * |
146 | * @since 0.1.0 |
147 | * @return string The name attribute for the radio input. |
148 | */ |
149 | public function getName(): string { |
150 | return $this->name; |
151 | } |
152 | |
153 | /** |
154 | * Get the label object for the radio input. |
155 | * |
156 | * This method returns the label object that provides a descriptive label for the radio button. |
157 | * The label is crucial for accessibility and usability. |
158 | * |
159 | * @since 0.1.0 |
160 | * @return Label The label object for the radio button. |
161 | */ |
162 | public function getLabel(): Label { |
163 | return $this->label; |
164 | } |
165 | |
166 | /** |
167 | * Get the value associated with the radio input. |
168 | * |
169 | * This method returns the value that is submitted when the radio button is selected and the form is submitted. |
170 | * This is particularly important when dealing with groups of radio buttons where each needs a distinct value. |
171 | * |
172 | * @since 0.1.0 |
173 | * @return string The value for the radio input. |
174 | */ |
175 | public function getValue(): string { |
176 | return $this->value; |
177 | } |
178 | |
179 | /** |
180 | * Check if the radio is selected by default. |
181 | * |
182 | * This method returns a boolean value indicating whether the radio button is selected by default. |
183 | * If true, the radio button is rendered in a checked state. |
184 | * |
185 | * @since 0.1.0 |
186 | * @return bool True if the radio button is checked, false otherwise. |
187 | */ |
188 | public function isChecked(): bool { |
189 | return $this->checked; |
190 | } |
191 | |
192 | /** |
193 | * Check if the radio is disabled. |
194 | * |
195 | * This method returns a boolean value indicating whether the radio button is disabled, |
196 | * preventing user interaction. A disabled radio button cannot be selected or deselected by the user. |
197 | * |
198 | * @since 0.1.0 |
199 | * @return bool True if the radio button is disabled, false otherwise. |
200 | */ |
201 | public function isDisabled(): bool { |
202 | return $this->disabled; |
203 | } |
204 | |
205 | /** |
206 | * Check if the radio button should be displayed inline. |
207 | * |
208 | * This method returns a boolean value indicating whether the radio button and its label are displayed |
209 | * inline with other elements. Inline radio buttons are typically used when multiple radio buttons need |
210 | * to appear on the same line. |
211 | * |
212 | * @since 0.1.0 |
213 | * @return bool True if the radio button is displayed inline, false otherwise. |
214 | */ |
215 | public function isInline(): bool { |
216 | return $this->inline; |
217 | } |
218 | |
219 | /** |
220 | * Get the additional HTML attributes for the radio input. |
221 | * |
222 | * This method returns an associative array of custom HTML attributes for the radio input element, |
223 | * such as `id`, `data-*`, `aria-*`, or any other valid attributes. |
224 | * |
225 | * @since 0.1.0 |
226 | * @return array The additional attributes as an array. |
227 | */ |
228 | public function getInputAttributes(): array { |
229 | return $this->inputAttributes; |
230 | } |
231 | |
232 | /** |
233 | * Get additional HTML attributes for the outer wrapper element. |
234 | * |
235 | * This method returns an associative array of custom HTML attributes that are applied to the outer wrapper element, |
236 | * enhancing its behavior or styling. |
237 | * |
238 | * @since 0.1.0 |
239 | * @return array The associative array of HTML attributes for the wrapper element. |
240 | */ |
241 | public function getWrapperAttributes(): array { |
242 | return $this->wrapperAttributes; |
243 | } |
244 | |
245 | /** |
246 | * Get the component's HTML representation. |
247 | * |
248 | * This method generates the HTML markup for the component, incorporating relevant properties |
249 | * and any additional attributes. The component is structured using appropriate HTML elements |
250 | * as defined by the implementation. |
251 | * |
252 | * @since 0.1.0 |
253 | * @return string The generated HTML string for the component. |
254 | */ |
255 | public function getHtml(): string { |
256 | return $this->renderer->render( $this ); |
257 | } |
258 | } |