Completed
Push — master ( f18ab2...132208 )
by P.R.
06:31
created

HtmlElement::unsetClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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