HtmlElement::setAttrDraggable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
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 $attributes = [];
35
36
  //--------------------------------------------------------------------------------------------------------------------
37
  /**
38
   * Adds a class to the list of classes.
39
   *
40
   * @param string|null $class The class.
41
   *
42
   * @since 1.0.0
43
   * @api
44
   */
45 5
  public function addClass(?string $class): void
46
  {
47
    // If class is empty return immediately.
48 5
    if ($class===null || $class==='') return;
49
50 3
    if (isset($this->attributes['class']))
51
    {
52 1
      $this->attributes['class'] .= ' ';
53 1
      $this->attributes['class'] .= $class;
54
    }
55
    else
56
    {
57 3
      $this->attributes['class'] = $class;
58
    }
59 3
  }
60
61
  //--------------------------------------------------------------------------------------------------------------------
62
  /**
63
   * Returns the value of an attribute.
64
   *
65
   * @param string $attributeName The name of the attribute.
66
   *
67
   * @return mixed
68
   *
69
   * @since 1.0.0
70
   * @api
71
   */
72 4
  public function getAttribute(string $attributeName)
73
  {
74 4
    return $this->attributes[$attributeName] ?? null;
75
  }
76
77
  //--------------------------------------------------------------------------------------------------------------------
78
  /**
79
   * Removes a class from the list of classes.
80
   *
81
   * @param string|null $class The class to be removed.
82
   *
83
   * @since 1.0.0
84
   * @api
85
   */
86 5
  public function removeClass(?string $class): void
87
  {
88
    // If class is empty or no classes are set return immediately.
89 5
    if ($class===null || $class==='' || !isset($this->attributes['class'])) return;
90
91
    // Remove the class from the list of classes.
92 3
    $this->attributes['class'] = implode(' ', array_diff(explode(' ', $this->attributes['class']), [$class]));
93 3
  }
94
95
  //--------------------------------------------------------------------------------------------------------------------
96
  /**
97
   * Sets the attribute [accesskey](http://www.w3schools.com/tags/att_global_accesskey.asp).
98
   *
99
   * @param string|null $value The attribute value.
100
   *
101
   * @since 1.0.0
102
   * @api
103
   */
104 1
  public function setAttrAccessKey(?string $value): void
105
  {
106 1
    $this->attributes['accesskey'] = $value;
107 1
  }
108
109
  //--------------------------------------------------------------------------------------------------------------------
110
  /**
111
   * Sets a [aria](http://w3c.github.io/html/infrastructure.html#element-attrdef-aria-aria) attribute.
112
   *
113
   * @param string      $name  The name of the attribute (without 'aria-').
114
   * @param string|null $value The attribute value.
115
   *
116
   * @since 1.3.0
117
   * @api
118
   */
119 1
  public function setAttrAria(string $name, ?string $value): void
120
  {
121 1
    $this->attributes['aria-'.$name] = $value;
122 1
  }
123
124
  //--------------------------------------------------------------------------------------------------------------------
125
  /**
126
   * Sets the attribute [class](https://www.w3schools.com/tags/att_global_class.asp).
127
   *
128
   * @param string|null $value The class or classes. Any value set by {@link addClass} will be overwritten.
129
   *
130
   * @since 1.4.0
131
   * @api
132
   */
133 1
  public function setAttrClass(?string $value): void
134
  {
135 1
    $this->attributes['class'] = $value;
136 1
  }
137
138
  //--------------------------------------------------------------------------------------------------------------------
139
  /**
140
   * Sets the attribute [contenteditable](http://www.w3schools.com/tags/att_global_contenteditable.asp).
141
   * <ul>
142
   * <li> Any value that evaluates to true will set the attribute to 'true'.
143
   * <li> Any value that evaluates to false will set the attribute to 'false'.
144
   * <li> Null will unset the attribute.
145
   * </ul>
146
   *
147
   * @param mixed $value The attribute value.
148
   *
149
   * @since 1.0.0
150
   * @api
151
   */
152 1
  public function setAttrContentEditable(?string $value): void
153
  {
154 1
    $this->attributes['contenteditable'] = $value;
155 1
  }
156
157
  //--------------------------------------------------------------------------------------------------------------------
158
  /**
159
   * Sets the attribute [contextmenu](http://www.w3schools.com/tags/att_global_contextmenu.asp).
160
   *
161
   * @param string|null $value The attribute value.
162
   *
163
   * @since 1.0.0
164
   * @api
165
   */
166 1
  public function setAttrContextMenu(?string $value): void
167
  {
168 1
    $this->attributes['contextmenu'] = $value;
169 1
  }
170
171
  //--------------------------------------------------------------------------------------------------------------------
172
  /**
173
   * Sets a [data](http://www.w3schools.com/tags/att_global_data.asp) attribute.
174
   *
175
   * @param string      $name  The name of the attribute (without 'data-').
176
   * @param string|null $value The attribute value.
177
   *
178
   * @since 1.0.0
179
   * @api
180
   */
181 1
  public function setAttrData(string $name, ?string $value): void
182
  {
183 1
    $this->attributes['data-'.$name] = $value;
184 1
  }
185
186
  //--------------------------------------------------------------------------------------------------------------------
187
  /**
188
   * Sets the attribute [dir](http://www.w3schools.com/tags/att_global_dir.asp). Possible values:
189
   * <ul>
190
   * <li> ltr
191
   * <li> rtl
192
   * <li> auto
193
   * </ul>
194
   *
195
   * @param string|null $value The attribute value.
196
   *
197
   * @since 1.0.0
198
   * @api
199
   */
200 1
  public function setAttrDir(?string $value): void
201
  {
202 1
    $this->attributes['dir'] = $value;
203 1
  }
204
205
  //--------------------------------------------------------------------------------------------------------------------
206
  /**
207
   * Sets the attribute [draggable](http://www.w3schools.com/tags/att_global_draggable.asp). Possible values:
208
   * <ul>
209
   * <li> true
210
   * <li> false
211
   * <li> auto
212
   * </ul>
213
   *
214
   * @param string|null $value The attribute value.
215
   *
216
   * @since 1.0.0
217
   * @api
218
   */
219 1
  public function setAttrDraggable(?string $value): void
220
  {
221 1
    $this->attributes['draggable'] = $value;
222 1
  }
223
224
  //--------------------------------------------------------------------------------------------------------------------
225
  /**
226
   * Sets the attribute [dropzone](http://www.w3schools.com/tags/att_global_dropzone.asp).
227
   *
228
   * @param string|null $value The attribute value.
229
   *
230
   * @since 1.0.0
231
   * @api
232
   */
233 1
  public function setAttrDropZone(?string $value): void
234
  {
235 1
    $this->attributes['dropzone'] = $value;
236 1
  }
237
238
  //--------------------------------------------------------------------------------------------------------------------
239
  /**
240
   * Sets the attribute [hidden](http://www.w3schools.com/tags/att_global_hidden.asp).
241
   * This is a boolean attribute. Any none [empty](http://php.net/manual/function.empty.php) value will set the
242
   * attribute to 'hidden'. Any other value will unset the attribute.
243
   *
244
   * @param mixed $value The attribute value.
245
   *
246
   * @since 1.0.0
247
   * @api
248
   */
249 1
  public function setAttrHidden(?string $value): void
250
  {
251 1
    $this->attributes['hidden'] = $value;
252 1
  }
253
254
  //--------------------------------------------------------------------------------------------------------------------
255
  /**
256
   * Sets the attribute [id](http://www.w3schools.com/tags/att_global_id.asp).
257
   *
258
   * @param string|null $value The attribute value.
259
   *
260
   * @since 1.0.0
261
   * @api
262
   */
263 1
  public function setAttrId(?string $value): void
264
  {
265 1
    $this->attributes['id'] = $value;
266 1
  }
267
268
  //--------------------------------------------------------------------------------------------------------------------
269
  /**
270
   * Sets the attribute [lang](http://www.w3schools.com/tags/att_global_lang.asp).
271
   *
272
   * @param string|null $value The attribute value.
273
   *
274
   * @since 1.0.0
275
   * @api
276
   */
277 1
  public function setAttrLang(?string $value): void
278
  {
279 1
    $this->attributes['lang'] = $value;
280 1
  }
281
282
  //--------------------------------------------------------------------------------------------------------------------
283
  /**
284
   * Sets the attribute [role](http://w3c.github.io/html/infrastructure.html#element-attrdef-aria-role).
285
   *
286
   * @param string|null $value The attribute value.
287
   *
288
   * @since 1.3.0
289
   * @api
290
   */
291 1
  public function setAttrRole(?string $value): void
292
  {
293 1
    $this->attributes['role'] = $value;
294 1
  }
295
296
  //--------------------------------------------------------------------------------------------------------------------
297
  /**
298
   * Sets the attribute [spellcheck](http://www.w3schools.com/tags/att_global_spellcheck.asp).
299
   * <ul>
300
   * <li> Any value that evaluates to true will set the attribute to 'true'.
301
   * <li> Any value that evaluates to false will set the attribute to 'false'.
302
   * <li> Null will unset the attribute.
303
   * <ul>
304
   *
305
   * @param string|null $value The attribute value.
306
   *
307
   * @since 1.0.0
308
   * @api
309
   */
310 1
  public function setAttrSpellCheck(?string $value): void
311
  {
312 1
    $this->attributes['spellcheck'] = $value;
313 1
  }
314
315
  //--------------------------------------------------------------------------------------------------------------------
316
  /**
317
   * Sets the attribute [style](http://www.w3schools.com/tags/att_global_style.asp)
318
   *
319
   * @param string|null $value The attribute value.
320
   *
321
   * @since 1.0.0
322
   * @api
323
   */
324 1
  public function setAttrStyle(?string $value): void
325
  {
326 1
    $this->attributes['style'] = $value;
327 1
  }
328
329
  //--------------------------------------------------------------------------------------------------------------------
330
  /**
331
   * Sets the attribute [tabindex](http://www.w3schools.com/tags/att_global_tabindex.asp).
332
   *
333
   * @param int|null $value The attribute value.
334
   *
335
   * @since 1.0.0
336
   * @api
337
   */
338 1
  public function setAttrTabIndex(?int $value): void
339
  {
340 1
    $this->attributes['tabindex'] = $value;
341 1
  }
342
343
  //--------------------------------------------------------------------------------------------------------------------
344
  /**
345
   * Sets the attribute [title](http://www.w3schools.com/tags/att_global_title.asp).
346
   *
347
   * @param string|null $value The attribute value.
348
   *
349
   * @since 1.0.0
350
   * @api
351
   */
352 1
  public function setAttrTitle(?string $value): void
353
  {
354 1
    $this->attributes['title'] = $value;
355 1
  }
356
357
  //--------------------------------------------------------------------------------------------------------------------
358
  /**
359
   * Sets the attribute [translate](http://www.w3schools.com/tags/att_global_translate.asp).
360
   * <ul>
361
   * <li> Any value that evaluates to true will set the attribute to 'yes'.
362
   * <li> Any value that evaluates to false will set the attribute to 'no'.
363
   * <li> Null will unset the attribute.
364
   * </ul>
365
   *
366
   * @param mixed $value The attribute value.
367
   *
368
   * @since 1.0.0
369
   * @api
370
   */
371 1
  public function setAttrTranslate(?string $value): void
372
  {
373 1
    $this->attributes['translate'] = $value;
374 1
  }
375
376
  //--------------------------------------------------------------------------------------------------------------------
377
  /**
378
   * Sets a fake attribute. A fake attribute has a name that starts with an underscore. Fake attributes will not be
379
   * included in the generated HTML code.
380
   *
381
   * @param string $name  The name of the fake attribute.
382
   * @param mixed  $value The value of the fake attribute.
383
   *
384
   * @since 1.0.0
385
   * @api
386
   */
387 2
  public function setFakeAttribute(string $name, $value): void
388
  {
389 2
    if (strpos($name, '_')!==0)
390
    {
391 1
      throw new LogicException("Attribute '%s' is not a valid fake attribute.", $name);
392
    }
393
394 1
    $this->attributes[$name] = $value;
395 1
  }
396
397
  //--------------------------------------------------------------------------------------------------------------------
398
  /**
399
   * Removes all classes for the list of classes.
400
   *
401
   * @since 1.0.0
402
   * @api
403
   */
404 1
  public function unsetClass(): void
405
  {
406 1
    unset($this->attributes['class']);
407 1
  }
408
409
  //--------------------------------------------------------------------------------------------------------------------
410
}
411
412
//----------------------------------------------------------------------------------------------------------------------
413