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