HtmlElement   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 512
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 68
c 2
b 0
f 1
dl 0
loc 512
ccs 90
cts 90
cp 1
rs 9.36
wmc 38

25 Methods

Rating   Name   Duplication   Size   Complexity  
A setAttrDraggable() 0 5 1
A getAttributes() 0 3 1
A removeClass() 0 13 5
A setAttrAccessKey() 0 5 1
A setAttrClass() 0 12 3
A setAttrDropZone() 0 5 1
A getAttribute() 0 3 1
A setAttrAria() 0 5 1
A setAttrData() 0 5 1
A setAttrContextMenu() 0 5 1
A addClasses() 0 17 4
A addClass() 0 19 5
A getId() 0 5 1
A setAttrDir() 0 5 1
A setAttrContentEditable() 0 5 1
A setAttrStyle() 0 5 1
A setAttrHidden() 0 5 1
A setAttrTabIndex() 0 5 1
A setAttrTranslate() 0 5 1
A setAttrLang() 0 5 1
A setAttrTitle() 0 5 1
A setAttrId() 0 5 1
A setAttrRole() 0 5 1
A setAttrSpellCheck() 0 5 1
A unsetClass() 0 5 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Plaisio\Helper;
5
6
/**
7
 * Trait for HTML elements.
8
 *
9
 * This class should be used for generation "heavy" HTML elements only. For lightweight elements use methods of
10
 * {@link \Plaisio\Helper\Html}.
11
 *
12
 * #### Global Attributes
13
 * This class defines methods for getting attributes and setting
14
 * [global attributes](http://www.w3schools.com/tags/ref_standardattributes.asp) only.
15
 *
16
 * Unless stated otherwise setting an attribute to null, false, or '' will unset the attribute.
17
 *
18
 * #### Event Attributes
19
 * This class does not define methods for setting event attributes. Events handlers must be set with JavaScript.
20
 */
21
trait HtmlElement
22
{
23
  //--------------------------------------------------------------------------------------------------------------------
24
  /**
25
   * The attributes of this HTML element.
26
   *
27
   * @var array
28
   *
29
   * @since 1.0.0
30
   * @api
31
   */
32
  protected array $attributes = [];
33
34
  //--------------------------------------------------------------------------------------------------------------------
35
  /**
36
   * Adds a class to the list of classes.
37
   *
38
   * @param string|null $class The class.
39
   *
40
   * @return $this
41
   *
42
   * @since 1.0.0
43
   * @api
44
   */
45 7
  public function addClass(?string $class): self
46
  {
47 7
    if ($class===null || $class==='')
48
    {
49 2
      return $this;
50
    }
51
52 5
    if (empty($this->attributes['class']))
53
    {
54 3
      $this->attributes['class'] = [];
55
    }
56 3
    elseif (is_string($this->attributes['class']))
57
    {
58 1
      $this->attributes['class'] = explode(' ', $this->attributes['class']);
59
    }
60
61 5
    $this->attributes['class'][] = $class;
62
63 5
    return $this;
64
  }
65
66
  //--------------------------------------------------------------------------------------------------------------------
67
  /**
68
   * Adds classes to the list of classes.
69
   *
70
   * @param array $classes The classes.
71
   *
72
   * @return $this
73
   *
74
   * @since 1.5.0
75
   * @api
76
   */
77 4
  public function addClasses(array $classes): self
78
  {
79 4
    if (empty($this->attributes['class']))
80
    {
81 3
      $this->attributes['class'] = [];
82
    }
83 2
    elseif (is_string($this->attributes['class']))
84
    {
85 1
      $this->attributes['class'] = explode(' ', $this->attributes['class']);
86
    }
87
88 4
    foreach ($classes as $class)
89
    {
90 4
      $this->attributes['class'][] = $class;
91
    }
92
93 4
    return $this;
94
  }
95
96
  //--------------------------------------------------------------------------------------------------------------------
97
  /**
98
   * Returns the value of an attribute.
99
   *
100
   * @param string $attributeName The name of the attribute.
101
   *
102
   * @return mixed
103
   *
104
   * @since 1.0.0
105
   * @api
106
   */
107 2
  public function getAttribute(string $attributeName): mixed
108
  {
109 2
    return $this->attributes[$attributeName] ?? null;
110
  }
111
112
  //--------------------------------------------------------------------------------------------------------------------
113
  /**
114
   * Returns the attributes of this HTML element.
115
   *
116
   * @return array
117
   */
118 1
  public function getAttributes(): array
119
  {
120 1
    return $this->attributes;
121
  }
122
123
  //--------------------------------------------------------------------------------------------------------------------
124
  /**
125
   * Returns the mandatory attribute [id](http://www.w3schools.com/tags/att_global_id.asp) of this element. If the id
126
   * attribute was not set prior calling this method the id attribute will set with an automatically generate value.
127
   *
128
   * @return string
129
   *
130
   * @since 1.4.0
131
   * @api
132
   */
133 1
  public function getId(): string
134
  {
135 1
    $this->attributes['id'] ??= Html::getAutoId();
136
137 1
    return $this->attributes['id'];
138
  }
139
140
  //--------------------------------------------------------------------------------------------------------------------
141
  /**
142
   * Removes a class from the list of classes.
143
   *
144
   * @param string|null $class The class to be removed.
145
   *
146
   * @return $this
147
   *
148
   * @since 1.0.0
149
   * @api
150
   */
151 5
  public function removeClass(?string $class): self
152
  {
153
    // If class is empty or no classes are set return immediately.
154 5
    if ($class===null || $class==='' || !isset($this->attributes['class'])) return $this;
155
156 3
    $this->attributes['class'] = array_unique($this->attributes['class']);
157 3
    $key                       = array_search($class, $this->attributes['class']);
158 3
    if ($key!==false)
159
    {
160 3
      unset($this->attributes['class'][$key]);
161
    }
162
163 3
    return $this;
164
  }
165
166
  //--------------------------------------------------------------------------------------------------------------------
167
  /**
168
   * Sets the attribute [accesskey](http://www.w3schools.com/tags/att_global_accesskey.asp).
169
   *
170
   * @param string|null $value The attribute value.
171
   *
172
   * @return $this
173
   *
174
   * @api
175
   * @since 1.0.0
176
   */
177 1
  public function setAttrAccessKey(?string $value): self
178
  {
179 1
    $this->attributes['accesskey'] = $value;
180
181 1
    return $this;
182
  }
183
184
  //--------------------------------------------------------------------------------------------------------------------
185
  /**
186
   * Sets a [aria](http://w3c.github.io/html/infrastructure.html#element-attrdef-aria-aria) attribute.
187
   *
188
   * @param string      $name  The name of the attribute (without 'aria-').
189
   * @param string|null $value The attribute value.
190
   *
191
   * @return $this
192
   *
193
   * @since 1.3.0
194
   * @api
195
   */
196 1
  public function setAttrAria(string $name, ?string $value): self
197
  {
198 1
    $this->attributes['aria-'.$name] = $value;
199
200 1
    return $this;
201
  }
202
203
  //--------------------------------------------------------------------------------------------------------------------
204
  /**
205
   * Sets the attribute [class](https://www.w3schools.com/tags/att_global_class.asp).
206
   *
207
   * @param string|null $value The class or classes. Any value set by {@link addClass} will be overwritten.
208
   *
209
   * @return $this
210
   *
211
   * @since 1.4.0
212
   * @api
213
   */
214 4
  public function setAttrClass(?string $value): self
215
  {
216 4
    if ($value===null || $value==='')
217
    {
218 1
      unset($this->attributes['class']);
219
    }
220
    else
221
    {
222 4
      $this->attributes['class'] = [$value];
223
    }
224
225 4
    return $this;
226
  }
227
228
  //--------------------------------------------------------------------------------------------------------------------
229
  /**
230
   * Sets the attribute [contenteditable](http://www.w3schools.com/tags/att_global_contenteditable.asp).
231
   * <ul>
232
   * <li> True will set the attribute to 'true'.
233
   * <li> False will set the attribute to 'false'.
234
   * <li> Null will unset the attribute.
235
   * </ul>
236
   *
237
   * @param bool|null $value The attribute value.
238
   *
239
   * @return $this
240
   *
241
   * @since 1.0.0
242
   * @api
243
   */
244 1
  public function setAttrContentEditable(?bool $value): self
245
  {
246 1
    $this->attributes['contenteditable'] = $value;
247
248 1
    return $this;
249
  }
250
251
  //--------------------------------------------------------------------------------------------------------------------
252
  /**
253
   * Sets the attribute [contextmenu](http://www.w3schools.com/tags/att_global_contextmenu.asp).
254
   *
255
   * @param string|null $value The attribute value.
256
   *
257
   * @return $this
258
   *
259
   * @since 1.0.0
260
   * @api
261
   */
262 1
  public function setAttrContextMenu(?string $value): self
263
  {
264 1
    $this->attributes['contextmenu'] = $value;
265
266 1
    return $this;
267
  }
268
269
  //--------------------------------------------------------------------------------------------------------------------
270
  /**
271
   * Sets a [data](http://www.w3schools.com/tags/att_global_data.asp) attribute.
272
   *
273
   * @param string      $name  The name of the attribute (without 'data-').
274
   * @param string|null $value The attribute value.
275
   *
276
   * @return $this
277
   *
278
   * @since 1.0.0
279
   * @api
280
   */
281 1
  public function setAttrData(string $name, ?string $value): self
282
  {
283 1
    $this->attributes['data-'.$name] = $value;
284
285 1
    return $this;
286
  }
287
288
  //--------------------------------------------------------------------------------------------------------------------
289
  /**
290
   * Sets the attribute [dir](http://www.w3schools.com/tags/att_global_dir.asp). Possible values:
291
   * <ul>
292
   * <li> ltr
293
   * <li> rtl
294
   * <li> auto
295
   * </ul>
296
   *
297
   * @param string|null $value The attribute value.
298
   *
299
   * @return $this
300
   *
301
   * @since 1.0.0
302
   * @api
303
   */
304 1
  public function setAttrDir(?string $value): self
305
  {
306 1
    $this->attributes['dir'] = $value;
307
308 1
    return $this;
309
  }
310
311
  //--------------------------------------------------------------------------------------------------------------------
312
  /**
313
   * Sets the attribute [draggable](http://www.w3schools.com/tags/att_global_draggable.asp). Possible values:
314
   * <ul>
315
   * <li> true
316
   * <li> false
317
   * <li> auto
318
   * </ul>
319
   *
320
   * @param string|null $value The attribute value.
321
   *
322
   * @return $this
323
   *
324
   * @since 1.0.0
325
   * @api
326
   */
327 1
  public function setAttrDraggable(?string $value): self
328
  {
329 1
    $this->attributes['draggable'] = $value;
330
331 1
    return $this;
332
  }
333
334
  //--------------------------------------------------------------------------------------------------------------------
335
  /**
336
   * Sets the attribute [dropzone](http://www.w3schools.com/tags/att_global_dropzone.asp).
337
   *
338
   * @param string|null $value The attribute value.
339
   *
340
   * @return $this
341
   *
342
   * @since 1.0.0
343
   * @api
344
   */
345 1
  public function setAttrDropZone(?string $value): self
346
  {
347 1
    $this->attributes['dropzone'] = $value;
348
349 1
    return $this;
350
  }
351
352
  //--------------------------------------------------------------------------------------------------------------------
353
  /**
354
   * Sets the attribute [hidden](http://www.w3schools.com/tags/att_global_hidden.asp).
355
   *
356
   * @param ?bool $value The attribute value.
357
   *
358
   * @return $this
359
   *
360
   * @since 1.0.0
361
   * @api
362
   */
363 1
  public function setAttrHidden(?bool $value): self
364
  {
365 1
    $this->attributes['hidden'] = $value;
366
367 1
    return $this;
368
  }
369
370
  //--------------------------------------------------------------------------------------------------------------------
371
  /**
372
   * Sets the attribute [id](http://www.w3schools.com/tags/att_global_id.asp).
373
   *
374
   * @param string|null $value The attribute value.
375
   *
376
   * @return $this
377
   *
378
   * @since 1.0.0
379
   * @api
380
   */
381 2
  public function setAttrId(?string $value): self
382
  {
383 2
    $this->attributes['id'] = $value;
384
385 2
    return $this;
386
  }
387
388
  //--------------------------------------------------------------------------------------------------------------------
389
  /**
390
   * Sets the attribute [lang](http://www.w3schools.com/tags/att_global_lang.asp).
391
   *
392
   * @param string|null $value The attribute value.
393
   *
394
   * @return $this
395
   *
396
   * @since 1.0.0
397
   * @api
398
   */
399 1
  public function setAttrLang(?string $value): self
400
  {
401 1
    $this->attributes['lang'] = $value;
402
403 1
    return $this;
404
  }
405
406
  //--------------------------------------------------------------------------------------------------------------------
407
  /**
408
   * Sets the attribute [role](http://w3c.github.io/html/infrastructure.html#element-attrdef-aria-role).
409
   *
410
   * @param string|null $value The attribute value.
411
   *
412
   * @return $this
413
   *
414
   * @since 1.3.0
415
   * @api
416
   */
417 1
  public function setAttrRole(?string $value): self
418
  {
419 1
    $this->attributes['role'] = $value;
420
421 1
    return $this;
422
  }
423
424
  //--------------------------------------------------------------------------------------------------------------------
425
  /**
426
   * Sets the attribute [spellcheck](http://www.w3schools.com/tags/att_global_spellcheck.asp).
427
   *
428
   * @param bool|null $value The attribute value.
429
   *
430
   * @return $this
431
   *
432
   * @since 1.0.0
433
   * @api
434
   */
435 1
  public function setAttrSpellCheck(?bool $value): self
436
  {
437 1
    $this->attributes['spellcheck'] = $value;
438
439 1
    return $this;
440
  }
441
442
  //--------------------------------------------------------------------------------------------------------------------
443
  /**
444
   * Sets the attribute [style](http://www.w3schools.com/tags/att_global_style.asp)
445
   *
446
   * @param string|null $value The attribute value.
447
   *
448
   * @return $this
449
   *
450
   * @since 1.0.0
451
   * @api
452
   */
453 1
  public function setAttrStyle(?string $value): self
454
  {
455 1
    $this->attributes['style'] = $value;
456
457 1
    return $this;
458
  }
459
460
  //--------------------------------------------------------------------------------------------------------------------
461
  /**
462
   * Sets the attribute [tabindex](http://www.w3schools.com/tags/att_global_tabindex.asp).
463
   *
464
   * @param int|null $value The attribute value.
465
   *
466
   * @return $this
467
   *
468
   * @since 1.0.0
469
   * @api
470
   */
471 1
  public function setAttrTabIndex(?int $value): self
472
  {
473 1
    $this->attributes['tabindex'] = $value;
474
475 1
    return $this;
476
  }
477
478
  //--------------------------------------------------------------------------------------------------------------------
479
  /**
480
   * Sets the attribute [title](http://www.w3schools.com/tags/att_global_title.asp).
481
   *
482
   * @param string|null $value The attribute value.
483
   *
484
   * @return $this
485
   *
486
   * @since 1.0.0
487
   * @api
488
   */
489 1
  public function setAttrTitle(?string $value): self
490
  {
491 1
    $this->attributes['title'] = $value;
492
493 1
    return $this;
494
  }
495
496
  //--------------------------------------------------------------------------------------------------------------------
497
  /**
498
   * Sets the attribute [translate](http://www.w3schools.com/tags/att_global_translate.asp).
499
   * <ul>
500
   * <li> True will set the attribute to 'yes'.
501
   * <li> False will set the attribute to 'no'.
502
   * <li> Null will unset the attribute.
503
   * </ul>
504
   *
505
   * @param bool|null $value The attribute value.
506
   *
507
   * @return $this
508
   *
509
   * @since 1.0.0
510
   * @api
511
   */
512 1
  public function setAttrTranslate(?bool $value): self
513
  {
514 1
    $this->attributes['translate'] = $value;
515
516 1
    return $this;
517
  }
518
519
  //--------------------------------------------------------------------------------------------------------------------
520
  /**
521
   * Removes all classes for the list of classes.
522
   *
523
   * @return $this
524
   *
525
   * @since 1.0.0
526
   * @api
527
   */
528 1
  public function unsetClass(): self
529
  {
530 1
    unset($this->attributes['class']);
531
532 1
    return $this;
533
  }
534
535
  //--------------------------------------------------------------------------------------------------------------------
536
}
537
538
//----------------------------------------------------------------------------------------------------------------------
539