Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 28 |
|
0.00% |
0 / 15 |
CRAP | |
0.00% |
0 / 1 |
TextInput | |
0.00% |
0 / 28 |
|
0.00% |
0 / 15 |
240 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
getType | |
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 | |||
getPlaceholder | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getInputId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
hasStartIcon | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
hasEndIcon | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getStartIconClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getEndIconClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isDisabled | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getStatus | |
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 | * TextInput.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 `TextInput` 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\TextInputRenderer; |
20 | |
21 | /** |
22 | * TextInput |
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 TextInput { |
36 | |
37 | /** |
38 | * Input field type. |
39 | */ |
40 | private string $type; |
41 | |
42 | /** |
43 | * Whether the input field has a start icon. |
44 | */ |
45 | private bool $hasStartIcon; |
46 | |
47 | /** |
48 | * Whether the input field has an end icon. |
49 | */ |
50 | private bool $hasEndIcon; |
51 | |
52 | /** |
53 | * Whether the input field is disabled. |
54 | */ |
55 | private bool $disabled; |
56 | |
57 | /** |
58 | * Validation status of the input (default, error, warning, or success). |
59 | */ |
60 | private string $status; |
61 | |
62 | /** |
63 | * The CSS class for the start icon. |
64 | */ |
65 | private string $startIconClass; |
66 | |
67 | /** |
68 | * The CSS class for the end icon. |
69 | */ |
70 | private string $endIconClass; |
71 | |
72 | /** |
73 | * Additional HTML attributes for the input element. |
74 | */ |
75 | private array $inputAttributes; |
76 | |
77 | /** |
78 | * Additional HTML attributes for the wrapper element. |
79 | */ |
80 | private array $wrapperAttributes; |
81 | |
82 | /** |
83 | * The placeholder text for the input field. |
84 | */ |
85 | private string $placeholder; |
86 | |
87 | /** |
88 | * The name attribute of the input field. |
89 | */ |
90 | private string $name; |
91 | |
92 | /** |
93 | * The value attribute of the input field. |
94 | */ |
95 | private string $value; |
96 | |
97 | /** |
98 | * The ID attribute for the input field. |
99 | */ |
100 | private string $inputId; |
101 | |
102 | /** |
103 | * The renderer instance used to render the text input. |
104 | */ |
105 | private TextInputRenderer $renderer; |
106 | |
107 | /** |
108 | * Constructor for the TextInput component. |
109 | * |
110 | * Initializes a TextInput instance with the specified properties. |
111 | * |
112 | * @param string $type The type of the input field (e.g., 'text', 'email'). |
113 | * @param bool $hasStartIcon Indicates if the input has a start icon. |
114 | * @param bool $hasEndIcon Indicates if the input has an end icon. |
115 | * @param bool $disabled Indicates if the input is disabled. |
116 | * @param string $status Validation status. |
117 | * @param string $startIconClass CSS class for the start icon. |
118 | * @param string $endIconClass CSS class for the end icon. |
119 | * @param array $inputAttributes Additional HTML attributes for the input element. |
120 | * @param array $wrapperAttributes Additional HTML attributes for the wrapper element. |
121 | * @param string $placeholder Placeholder text for the input. |
122 | * @param string $name Name attribute of the input. |
123 | * @param string $value Default value of the input. |
124 | * @param string $inputId ID attribute for the input. |
125 | * @param TextInputRenderer $renderer The renderer to use for rendering the text input. |
126 | */ |
127 | public function __construct( |
128 | string $type, |
129 | bool $hasStartIcon, |
130 | bool $hasEndIcon, |
131 | bool $disabled, |
132 | string $status, |
133 | string $startIconClass, |
134 | string $endIconClass, |
135 | array $inputAttributes, |
136 | array $wrapperAttributes, |
137 | string $placeholder, |
138 | string $name, |
139 | string $value, |
140 | string $inputId, |
141 | TextInputRenderer $renderer |
142 | ) { |
143 | $this->type = $type; |
144 | $this->hasStartIcon = $hasStartIcon; |
145 | $this->hasEndIcon = $hasEndIcon; |
146 | $this->disabled = $disabled; |
147 | $this->status = $status; |
148 | $this->startIconClass = $startIconClass; |
149 | $this->endIconClass = $endIconClass; |
150 | $this->inputAttributes = $inputAttributes; |
151 | $this->wrapperAttributes = $wrapperAttributes; |
152 | $this->placeholder = $placeholder; |
153 | $this->name = $name; |
154 | $this->value = $value; |
155 | $this->inputId = $inputId; |
156 | $this->renderer = $renderer; |
157 | } |
158 | |
159 | /** |
160 | * Get the type of the input field. |
161 | * |
162 | * This method returns the type attribute of the input field, which determines the type of data |
163 | * the input field accepts (e.g., 'text', 'email', 'password'). |
164 | * |
165 | * @since 0.1.0 |
166 | * @return string The type of the input field. |
167 | */ |
168 | public function getType(): string { |
169 | return $this->type; |
170 | } |
171 | |
172 | /** |
173 | * Get the name attribute of the input field. |
174 | * |
175 | * This method returns the name attribute of the input field, which is used to identify the input |
176 | * form control when submitting the form data. |
177 | * |
178 | * @since 0.1.0 |
179 | * @return string The name attribute of the input field. |
180 | */ |
181 | public function getName(): string { |
182 | return $this->name; |
183 | } |
184 | |
185 | /** |
186 | * Get the value of the input field. |
187 | * |
188 | * This method returns the current value of the input field. |
189 | * |
190 | * @since 0.1.0 |
191 | * @return string The value of the input field. |
192 | */ |
193 | public function getValue(): string { |
194 | return $this->value; |
195 | } |
196 | |
197 | /** |
198 | * Get the placeholder text of the input field. |
199 | * |
200 | * This method returns the placeholder text, which is displayed when the input field is empty. |
201 | * It provides a hint to the user about what should be entered in the field. |
202 | * |
203 | * @since 0.1.0 |
204 | * @return string The placeholder text of the input field. |
205 | */ |
206 | public function getPlaceholder(): string { |
207 | return $this->placeholder; |
208 | } |
209 | |
210 | /** |
211 | * Get the ID of the input field. |
212 | * |
213 | * This method returns the ID attribute of the input field, which is useful for linking |
214 | * the input field to a label or for other JavaScript interactions. |
215 | * |
216 | * @since 0.1.0 |
217 | * @return string The ID of the input field. |
218 | */ |
219 | public function getInputId(): string { |
220 | return $this->inputId; |
221 | } |
222 | |
223 | /** |
224 | * Check if the input field has a start icon. |
225 | * |
226 | * This method returns a boolean value indicating whether the input field has an icon at the start. |
227 | * |
228 | * @since 0.1.0 |
229 | * @return bool True if the input field has a start icon, false otherwise. |
230 | */ |
231 | public function hasStartIcon(): bool { |
232 | return $this->hasStartIcon; |
233 | } |
234 | |
235 | /** |
236 | * Check if the input field has an end icon. |
237 | * |
238 | * This method returns a boolean value indicating whether the input field has an icon at the end. |
239 | * |
240 | * @since 0.1.0 |
241 | * @return bool True if the input field has an end icon, false otherwise. |
242 | */ |
243 | public function hasEndIcon(): bool { |
244 | return $this->hasEndIcon; |
245 | } |
246 | |
247 | /** |
248 | * Get the CSS class for the start icon. |
249 | * |
250 | * This method returns the CSS class that is applied to the start icon, which can be used to style |
251 | * the icon or apply a background image. |
252 | * |
253 | * @since 0.1.0 |
254 | * @return string The CSS class for the start icon. |
255 | */ |
256 | public function getStartIconClass(): string { |
257 | return $this->startIconClass; |
258 | } |
259 | |
260 | /** |
261 | * Get the CSS class for the end icon. |
262 | * |
263 | * This method returns the CSS class that is applied to the end icon, which can be used to style |
264 | * the icon or apply a background image. |
265 | * |
266 | * @since 0.1.0 |
267 | * @return string The CSS class for the end icon. |
268 | */ |
269 | public function getEndIconClass(): string { |
270 | return $this->endIconClass; |
271 | } |
272 | |
273 | /** |
274 | * Check if the input field is disabled. |
275 | * |
276 | * This method returns a boolean value indicating whether the input field is disabled, making it uneditable. |
277 | * |
278 | * @since 0.1.0 |
279 | * @return bool True if the input field is disabled, false otherwise. |
280 | */ |
281 | public function isDisabled(): bool { |
282 | return $this->disabled; |
283 | } |
284 | |
285 | /** |
286 | * Get the validation status of the input. |
287 | * |
288 | * This method returns a string value indicating the current validation status, which is used to |
289 | * add a CSS class that can be used for special styles per status. |
290 | * |
291 | * @since 0.1.0 |
292 | * @return string Validation status, e.g. 'default' or 'error'. |
293 | */ |
294 | public function getStatus(): string { |
295 | return $this->status; |
296 | } |
297 | |
298 | /** |
299 | * Get additional HTML attributes for the input element. |
300 | * |
301 | * This method returns an associative array of custom HTML attributes that are applied to the input element, |
302 | * such as `data-*`, `aria-*`, or any other valid attributes that enhance functionality or accessibility. |
303 | * |
304 | * @since 0.1.0 |
305 | * @return array The associative array of HTML attributes for the input element. |
306 | */ |
307 | public function getInputAttributes(): array { |
308 | return $this->inputAttributes; |
309 | } |
310 | |
311 | /** |
312 | * Get additional HTML attributes for the outer wrapper element. |
313 | * |
314 | * This method returns an associative array of custom HTML attributes that are applied to the outer wrapper element, |
315 | * enhancing its behavior or styling. |
316 | * |
317 | * @since 0.1.0 |
318 | * @return array The associative array of HTML attributes for the wrapper element. |
319 | */ |
320 | public function getWrapperAttributes(): array { |
321 | return $this->wrapperAttributes; |
322 | } |
323 | |
324 | /** |
325 | * Get the component's HTML representation. |
326 | * |
327 | * This method generates the HTML markup for the component, incorporating relevant properties |
328 | * and any additional attributes. The component is structured using appropriate HTML elements |
329 | * as defined by the implementation. |
330 | * |
331 | * @since 0.1.0 |
332 | * @return string The generated HTML string for the component. |
333 | */ |
334 | public function getHtml(): string { |
335 | return $this->renderer->render( $this ); |
336 | } |
337 | } |