Passed
Push — master ( fbdae9...66097f )
by Divine Niiquaye
10:37
created

createElement()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.0283

Importance

Changes 0
Metric Value
cc 7
eloc 12
c 0
b 0
f 0
nc 15
nop 3
dl 0
loc 24
ccs 11
cts 12
cp 0.9167
crap 7.0283
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.2 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Biurad\UI\Html {
19
20
    /**
21
     * Import/Use a script.
22
     *
23
     * @param string|array<int,string> $src        The internal/external scripts to apply
24
     * @param array<string,mixed>      $attributes The attributes for script tag
25
     *
26
     * @link https://www.w3.org/TR/html52/semantics-scripting.html#the-script-element
27
     */
28
    function script($src, array $attributes = []): string
29
    {
30
        if (\is_string($src)) {
31
            $attributes['src'] = $src;
32
        } else {
33
            $attributes['children'] = $src;
34
            $attributes += ['type' => 'text/javascript'];
35
        }
36
37
        return createElement('script', $attributes);
38
    }
39
40
    /**
41
     * NoScript Element.
42
     *
43
     * @link https://html.spec.whatwg.org/multipage/scripting.html#the-noscript-element
44
     */
45
    function noscript(string $children): string
46
    {
47
        return createElement('noscript', ['children' => $children]);
48
    }
49
50
    /**
51
     * The `<html>` HTML element represents the root (top-level element) of an HTML document.
52
     *
53
     * @param array<int,string>   $children   The Element children
54
     * @param array<string,mixed> $attributes Attributes for the element
55
     *
56
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/html
57
     */
58
    function html(array $children, array $attributes = []): string
59
    {
60 1
        $attributes['children'] = $children;
61
62 1
        return createElement('!Doctype html', [], false) . createElement('html', $attributes);
63
    }
64
65
    /**
66
     * The `<head>` HTML element contains machine-readable information (metadata) about the document.
67
     *
68
     * @param array<int,string>   $children   The Element children
69
     * @param array<string,mixed> $attributes Attributes for the element
70
     *
71
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head
72
     */
73
    function head(array $children, array $attributes = []): string
74
    {
75 1
        $attributes['children'] = $children;
76
77 1
        return createElement('head', $attributes);
78
    }
79
80
    /**
81
     * The `<base>` HTML element specifies the base URL to use for all relative URLs in a document.
82
     *
83
     * This element must come before other elements with attribute values of URLs,
84
     * such as `<link>`’s href attribute.
85
     *
86
     * @param string              $href       The base URL to be used throughout the document for relative URLs
87
     * @param array<string,mixed> $attributes Attributes for the element
88
     *
89
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
90
     */
91
    function base(string $href, array $attributes = []): string
92
    {
93
        $attributes += ['target' => '__self', 'href' => $href];
94
95
        return createElement('base', $attributes, false);
96
    }
97
98
    /**
99
     * The `<title>` HTML element defines the document's title that is shown in a Browser's title bar.
100
     *
101
     * @param string              $content    The Document Title
102
     * @param array<string,mixed> $attributes Attributes for the element
103
     *
104
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title
105
     */
106
    function title(string $content, array $attributes = []): string
107
    {
108 1
        $attributes['children'] = $content;
109
110 1
        return createElement('title', $attributes);
111
    }
112
113
    /**
114
     * The `<link>` HTML element specifies relationships between the current document and an external resource.
115
     *
116
     * @param string              $href       This attribute specifies the URL of the linked resource
117
     * @param array<string,mixed> $attributes Attributes for the element
118
     *
119
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link
120
     */
121
    function link(string $href, array $attributes = []): string
122
    {
123
        $attributes += ['href' => $href, 'rel' => 'stylesheet'];
124
125
        return createElement('link', $attributes, false);
126
    }
127
128
    /**
129
     * The `<meta>` HTML element represents metadata that cannot be represented
130
     * by other HTML meta-related elements.
131
     *
132
     * @param string              $content    the charset value if empty $attributes
133
     *                                        else passed as the content attribute of the (meta) tag
134
     * @param array<string,mixed> $attributes Attributes for the element
135
     *
136
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta
137
     */
138
    function meta(string $content, array $attributes = []): string
139
    {
140 1
        $attributes[empty($attributes) ? 'charset' : 'content'] = $content;
141
142 1
        return createElement('meta', $attributes, false);
143
    }
144
145
    /**
146
     * Import stylesheet or Use a css styling for the `<style>` HTML Element.
147
     *
148
     * @param string|array<string,string> $children   The styles/stylesheet to apply
149
     * @param array<string,mixed>         $attributes Attributes for the element
150
     *
151
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/style
152
     */
153
    function style($children, array $attributes = []): string
154
    {
155 1
        if (\is_array($children)) {
156 1
            $children = HtmlElement::cssFromArray($children);
157
        } elseif (\file_exists($children)) {
158
            $children = \file_get_contents($children);
159
        }
160
161 1
        $attributes['children'] = $children;
162 1
        $attributes += ['type' => 'text/javascript'];
163
164 1
        return createElement('style', $attributes);
165
    }
166
167
    /**
168
     * The `<body>` HTML element represents the content of an HTML document.
169
     *
170
     * @param array<int,string>   $children   The Element children
171
     * @param array<string,mixed> $attributes Attributes for the element
172
     *
173
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/body
174
     */
175
    function body(array $children, array $attributes = []): string
176
    {
177 1
        $attributes['children'] = $children;
178
179 1
        return createElement('body', $attributes);
180
    }
181
182
    /**
183
     * The `<address>` HTML element indicates that the enclosed HTML provides contact
184
     * information for a person or people, or for an organization.
185
     *
186
     * @param string|array<int,string> $children   The Element children
187
     * @param array<string,mixed>      $attributes Attributes for the element
188
     *
189
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/address
190
     */
191
    function address($children, array $attributes = []): string
192
    {
193
        $attributes['children'] = $children;
194
195
        return createElement('address', $attributes);
196
    }
197
198
    /**
199
     * The `<article>` HTML element represents a self-contained composition in a
200
     * document, page, application, or site, which is intended to be independently
201
     * distributable or reusable (e.g., in syndication).
202
     *
203
     * @param string|array<int,string> $children   The Element children
204
     * @param array<string,mixed>      $attributes Attributes for the element
205
     *
206
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/article
207
     */
208
    function article($children, array $attributes = []): string
209
    {
210
        $attributes['children'] = $children;
211
212
        return createElement('article', $attributes);
213
    }
214
215
    /**
216
     * The `<aside>` HTML element represents a portion of a document whose content
217
     * is only indirectly related to the document's main content.
218
     *
219
     * @param string|array<int,string> $children   The Element children
220
     * @param array<string,mixed>      $attributes Attributes for the element
221
     *
222
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/aside
223
     */
224
    function aside($children, array $attributes = []): string
225
    {
226
        $attributes['children'] = $children;
227
228
        return createElement('aside', $attributes);
229
    }
230
231
    /**
232
     * The `<header>` HTML element represents introductory content,
233
     * typically a group of introductory or navigational aids.
234
     *
235
     * @param string|array<int,string> $children   The Element children
236
     * @param array<string,mixed>      $attributes Attributes for the element
237
     *
238
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/header
239
     */
240
    function header($children, array $attributes = []): string
241
    {
242
        $attributes['children'] = $children;
243
244
        return createElement('header', $attributes);
245
    }
246
247
    /**
248
     * The `<footer>` HTML element represents a footer for its nearest
249
     * sectioning content or sectioning root element.
250
     *
251
     * @param string|array<int,string> $children   The Element children
252
     * @param array<string,mixed>      $attributes Attributes for the element
253
     *
254
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/footer
255
     */
256
    function footer($children, array $attributes = []): string
257
    {
258
        $attributes['children'] = $children;
259
260
        return createElement('footer', $attributes);
261
    }
262
263
    /**
264
     * The `<h6>` HTML Section Heading element.
265
     *
266
     * @param string|array<int,string> $children   The Element children
267
     * @param array<string,mixed>      $attributes Attributes for the element
268
     *
269
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Heading_Elements
270
     */
271
    function h6($children, array $attributes = []): string
272
    {
273
        $attributes['children'] = $children;
274
275
        return createElement('h6', $attributes);
276
    }
277
278
    /**
279
     * The `<h5>` HTML Section Heading element.
280
     *
281
     * @param string|array<int,string> $children   The Element children
282
     * @param array<string,mixed>      $attributes Attributes for the element
283
     *
284
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Heading_Elements
285
     */
286
    function h5($children, array $attributes = []): string
287
    {
288
        $attributes['children'] = $children;
289
290
        return createElement('h5', $attributes);
291
    }
292
293
    /**
294
     * The `<h4>` HTML Section Heading element.
295
     *
296
     * @param string|array<int,string> $children   The Element children
297
     * @param array<string,mixed>      $attributes Attributes for the element
298
     *
299
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Heading_Elements
300
     */
301
    function h4($children, array $attributes = []): string
302
    {
303
        $attributes['children'] = $children;
304
305
        return createElement('h4', $attributes);
306
    }
307
308
    /**
309
     * The `<h3>` HTML Section Heading element.
310
     *
311
     * @param string|array<int,string> $children   The Element children
312
     * @param array<string,mixed>      $attributes Attributes for the element
313
     *
314
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Heading_Elements
315
     */
316
    function h3($children, array $attributes = []): string
317
    {
318
        $attributes['children'] = $children;
319
320
        return createElement('h3', $attributes);
321
    }
322
323
    /**
324
     * The `<h2>` HTML Section Heading element.
325
     *
326
     * @param string|array<int,string> $children   The Element children
327
     * @param array<string,mixed>      $attributes Attributes for the element
328
     *
329
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Heading_Elements
330
     */
331
    function h2($children, array $attributes = []): string
332
    {
333
        $attributes['children'] = $children;
334
335
        return createElement('h2', $attributes);
336
    }
337
338
    /**
339
     * The `<h1>` HTML Section Heading element.
340
     *
341
     * @param string|array<int,string> $children   The Element children
342
     * @param array<string,mixed>      $attributes Attributes for the element
343
     *
344
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Heading_Elements
345
     */
346
    function h1($children, array $attributes = []): string
347
    {
348 1
        $attributes['children'] = $children;
349
350 1
        return createElement('h1', $attributes);
351
    }
352
353
    /**
354
     * The `<nav>` HTML element represents a section of a page whose purpose is to provide
355
     * navigation links, either within the current document or to other documents.
356
     *
357
     * @param string|array<int,string> $children   The Element children
358
     * @param array<string,mixed>      $attributes Attributes for the element
359
     *
360
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/nav
361
     */
362
    function nav($children, array $attributes = []): string
363
    {
364
        $attributes['children'] = $children;
365
366
        return createElement('nav', $attributes);
367
    }
368
369
    /**
370
     * The `<main>` HTML element represents the dominant content of the body of a document.
371
     *
372
     * @param string|array<int,string> $children   The Element children
373
     * @param array<string,mixed>      $attributes Attributes for the element
374
     *
375
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/main
376
     */
377
    function main($children, array $attributes = []): string
378
    {
379
        $attributes['children'] = $children;
380
381
        return createElement('main', $attributes);
382
    }
383
384
    /**
385
     * The `<section>` HTML element represents a generic standalone section of a document,
386
     * which doesn't have a more specific semantic element to represent it.
387
     *
388
     * @param string|array<int,string> $children   The Element children
389
     * @param array<string,mixed>      $attributes Attributes for the element
390
     *
391
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/section
392
     */
393
    function section($children, array $attributes = []): string
394
    {
395
        $attributes['children'] = $children;
396
397
        return createElement('section', $attributes);
398
    }
399
400
    /**
401
     * The `<blockquote>` HTML element indicates that the enclosed text is an extended quotation.
402
     *
403
     * @param string|array<int,string> $children   The Element children
404
     * @param array<string,mixed>      $attributes Attributes for the element
405
     *
406
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/blockquote
407
     */
408
    function blockquote($children, array $attributes = []): string
409
    {
410
        $attributes['children'] = $children;
411
412
        return createElement('blockquote', $attributes);
413
    }
414
415
    /**
416
     * The `<dd>` HTML element provides the description, definition,
417
     * or value for the preceding term (dt) in a description list (dl).
418
     *
419
     * @param string|array<int,string> $children   The Element children
420
     * @param array<string,mixed>      $attributes Attributes for the element
421
     *
422
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dd
423
     */
424
    function dd($children, array $attributes = []): string
425
    {
426
        $attributes['children'] = $children;
427
428
        return createElement('dd', $attributes);
429
    }
430
431
    /**
432
     * The `<div>` HTML element is the generic container for flow content.
433
     *
434
     * @param string|array<int,string> $children   The Element children
435
     * @param array<string,mixed>      $attributes Attributes for the element
436
     *
437
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div
438
     */
439
    function div($children, array $attributes = []): string
440
    {
441 1
        $attributes['children'] = $children;
442
443 1
        return createElement('div', $attributes);
444
    }
445
446
    /**
447
     * The `<dl>` HTML element represents a description list.
448
     *
449
     * @param string|array<int,string> $children   The Element children
450
     * @param array<string,mixed>      $attributes Attributes for the element
451
     *
452
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dl
453
     */
454
    function dl($children, array $attributes = []): string
455
    {
456
        $attributes['children'] = $children;
457
458
        return createElement('dl', $attributes);
459
    }
460
461
    /**
462
     * The `<dt>` HTML element specifies a term in a description or definition list,
463
     * and as such must be used inside a dl element.
464
     *
465
     * @param string|array<int,string> $children   The Element children
466
     * @param array<string,mixed>      $attributes Attributes for the element
467
     *
468
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dt
469
     */
470
    function dt($children, array $attributes = []): string
471
    {
472
        $attributes['children'] = $children;
473
474
        return createElement('dt', $attributes);
475
    }
476
477
    /**
478
     * The `<figcaption>` HTML element represents a caption or legend describing
479
     * the rest of the contents of its parent figure element.
480
     *
481
     * @param string|array<int,string> $children   The Element children
482
     * @param array<string,mixed>      $attributes Attributes for the element
483
     *
484
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figcaption
485
     */
486
    function figcaption($children, array $attributes = []): string
487
    {
488
        $attributes['children'] = $children;
489
490
        return createElement('figcaption', $attributes);
491
    }
492
493
    /**
494
     * The `<figure>` HTML element represents self-contained content, potentially
495
     * with an optional caption, which is specified using the figcaption element.
496
     *
497
     * @param string|array<int,string> $children   The Element children
498
     * @param array<string,mixed>      $attributes Attributes for the element
499
     *
500
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figcaption
501
     */
502
    function figure($children, array $attributes = []): string
503
    {
504
        $attributes['children'] = $children;
505
506
        return createElement('figure', $attributes);
507
    }
508
509
    /**
510
     * The `<hr>` HTML element represents a thematic break between paragraph-level elements.
511
     *
512
     * @param array<string,mixed> $attributes Attributes for the element
513
     *
514
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/hr
515
     */
516
    function hr(array $attributes = []): string
517
    {
518 1
        return createElement('hr', $attributes, false);
519
    }
520
521
    /**
522
     * The `<li>` HTML element is used to represent an item in a list.
523
     *
524
     * @param string|array<int,string> $children   The Element children
525
     * @param array<string,mixed>      $attributes Attributes for the element
526
     *
527
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/li
528
     */
529
    function li($children, array $attributes = []): string
530
    {
531 1
        $attributes['children'] = $children;
532
533 1
        return createElement('li', $attributes);
534
    }
535
536
    /**
537
     * The `<ol>` HTML element represents an ordered list of items — typically rendered as a numbered list.
538
     *
539
     * @param string|array<int,string> $children   The Element children
540
     * @param array<string,mixed>      $attributes Attributes for the element
541
     *
542
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ol
543
     */
544
    function ol($children, array $attributes = []): string
545
    {
546
        $attributes['children'] = $children;
547
548
        return createElement('ol', $attributes);
549
    }
550
551
    /**
552
     * The `<p>` HTML element represents a paragraph.
553
     *
554
     * @param string|array<int,string> $children   The Element children
555
     * @param array<string,mixed>      $attributes Attributes for the element
556
     *
557
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/p
558
     */
559
    function p($children, array $attributes = []): string
560
    {
561 1
        $attributes['children'] = $children;
562
563 1
        return createElement('p', $attributes);
564
    }
565
    /**
566
     * The <pre> HTML element represents preformatted text which is to be presented exactly as written in the HTML file.
567
     *
568
     * @param string|array<int,string> $children   The Element children
569
     * @param array<string,mixed>      $attributes Attributes for the element
570
     *
571
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre
572
     */
573
    function pre($children, array $attributes = []): string
574
    {
575
        $attributes['children'] = $children;
576
577
        return createElement('pre', $attributes);
578
    }
579
580
    /**
581
     * The `<ul>` HTML element represents an unordered list of items, typically rendered as a bulleted list.
582
     *
583
     * @param string|array<int,string> $children   The Element children
584
     * @param array<string,mixed>      $attributes Attributes for the element
585
     *
586
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ul
587
     */
588
    function ul($children, array $attributes = []): string
589
    {
590 1
        $attributes['children'] = $children;
591
592 1
        return createElement('ul', $attributes);
593
    }
594
595
    /**
596
     * The `<a>` HTML element (or anchor element), with its href attribute, creates a hyperlink.
597
     *
598
     * @param string                   $href       The URL that the hyperlink points to
599
     * @param string|array<int,string> $children   The Element children
600
     * @param array<string,mixed>      $attributes Attributes for the element
601
     *
602
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a
603
     */
604
    function a(string $href, $children = [], array $attributes = []): string
605
    {
606 1
        $attributes += ['href' => $href, 'children' => $children];
607
608 1
        return createElement('a', $attributes);
609
    }
610
611
    /**
612
     * The `<abbr>` HTML element represents an abbreviation or acronym;
613
     * the optional title attribute can provide an expansion or description for the abbreviation.
614
     *
615
     * @param string|array<int,string> $children   The Element children
616
     * @param string|null              $title      if present, must contain this full description
617
     * @param array<string,mixed>      $attributes Attributes for the element
618
     *
619
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/abbr
620
     */
621
    function abbr($children = [], string $title = null, array $attributes = []): string
622
    {
623
        if (null !== $title) {
624
            $attributes['title'] = $title;
625
        }
626
627
        $attributes['children'] = $children;
628
629
        return createElement('abbr', $attributes);
630
    }
631
632
    /**
633
     * The `<b>` HTML element is used to draw the reader's attention to the element's contents,
634
     * which are not otherwise granted special importance.
635
     *
636
     * @param string|array<int,string> $children   The Element children
637
     * @param array<string,mixed>      $attributes Attributes for the element
638
     *
639
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/b
640
     */
641
    function b($children = [], array $attributes = []): string
642
    {
643
        $attributes['children'] = $children;
644
645
        return createElement('b', $attributes);
646
    }
647
648
    /**
649
     * The `<bdi>` HTML element tells the browser's bidirectional algorithm to treat
650
     * the text it contains in isolation from its surrounding text.
651
     *
652
     * @param string|array<int,string> $children   The Element children
653
     * @param array<string,mixed>      $attributes Attributes for the element
654
     *
655
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/bdi
656
     */
657
    function bdi($children, array $attributes = []): string
658
    {
659
        $attributes['children'] = $children;
660
661
        return createElement('bdi', $attributes);
662
    }
663
664
    /**
665
     * The `<bdi>` HTML element tells the browser's bidirectional algorithm to treat
666
     * the text it contains in isolation from its surrounding text.
667
     *
668
     * @param string|array<int,string> $children   The Element children
669
     * @param array<string,mixed>      $attributes Attributes for the element
670
     *
671
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/bdo
672
     */
673
    function bdo($children, array $attributes = []): string
674
    {
675
        $attributes['children'] = $children;
676
677
        return createElement('bdo', $attributes);
678
    }
679
680
    /**
681
     * The `<br>` HTML element produces a line break in text (carriage-return).
682
     *
683
     * @param array<string,mixed> $attributes Attributes for the element
684
     *
685
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/br
686
     */
687
    function br(array $attributes = []): string
688
    {
689 1
        return createElement('br', $attributes, true);
690
    }
691
692
    /**
693
     * The `<cite>` HTML element is used to describe a reference to a cited creative work,
694
     * and must include the title of that work.
695
     *
696
     * @param string|array<int,string> $children   The Element children
697
     * @param array<string,mixed>      $attributes Attributes for the element
698
     *
699
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/cite
700
     */
701
    function cite($children, array $attributes = []): string
702
    {
703
        $attributes['children'] = $children;
704
705
        return createElement('cite', $attributes);
706
    }
707
708
    /**
709
     * The `<code>` HTML element displays its contents styled in a fashion intended to
710
     * indicate that the text is a short fragment of computer code.
711
     *
712
     * @param string|array<int,string> $children   The Element children
713
     * @param array<string,mixed>      $attributes Attributes for the element
714
     *
715
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/code
716
     */
717
    function code($children, array $attributes = []): string
718
    {
719
        $attributes['children'] = $children;
720
721
        return createElement('code', $attributes);
722
    }
723
724
    /**
725
     * The `<code>` HTML element displays its contents styled in a fashion intended to
726
     * indicate that the text is a short fragment of computer code.
727
     *
728
     * @param string                   $value      specifies the machine-readable translation
729
     *                                             of the content of the element
730
     * @param string|array<int,string> $children   The Element children
731
     * @param array<string,mixed>      $attributes Attributes for the element
732
     *
733
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/data
734
     */
735
    function data(string $value, $children, array $attributes = []): string
736
    {
737
        $attributes['value'] = $value;
738
        $attributes['children'] = $children;
739
740
        return createElement('data', $attributes);
741
    }
742
743
    /**
744
     * The `<dfn>` HTML element is used to indicate the term being defined
745
     * within the context of a definition phrase or sentence.
746
     *
747
     * @param string|array<int,string> $children   The Element children
748
     * @param string|null              $title      id preset, it must contain the term being
749
     *                                             defined and no other text
750
     * @param array<string,mixed>      $attributes Attributes for the element
751
     *
752
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dfn
753
     */
754
    function dfn($children, string $title = null, array $attributes = []): string
755
    {
756
        if (null !== $title) {
757
            $attributes['title'] = $title;
758
        }
759
760
        $attributes['children'] = $children;
761
762
        return createElement('dfn', $attributes);
763
    }
764
765
    /**
766
     * The `<em>` HTML element marks text that has stress emphasis.
767
     *
768
     * @param string|array<int,string> $children   The Element children
769
     * @param array<string,mixed>      $attributes Attributes for the element
770
     *
771
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/em
772
     */
773
    function em($children, array $attributes = []): string
774
    {
775
        $attributes['children'] = $children;
776
777
        return createElement('em', $attributes);
778
    }
779
780
    /**
781
     * The `<i>` HTML element represents a range of text that is set off from the normal
782
     * text for some reason, such as idiomatic text, technical terms, taxonomical designations, among others.
783
     *
784
     * @param string|array<int,string> $children   The Element children
785
     * @param array<string,mixed>      $attributes Attributes for the element
786
     *
787
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/i
788
     */
789
    function i($children, array $attributes = []): string
790
    {
791
        $attributes['children'] = $children;
792
793
        return createElement('i', $attributes);
794
    }
795
796
    /**
797
     * The `<kbd>` HTML element represents a span of inline text denoting textual user
798
     * input from a keyboard, voice input, or any other text entry device.
799
     *
800
     * @param string|array<int,string> $children   The Element children
801
     * @param array<string,mixed>      $attributes Attributes for the element
802
     *
803
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/kbd
804
     */
805
    function kbd($children, array $attributes = []): string
806
    {
807
        $attributes['children'] = $children;
808
809
        return createElement('kbd', $attributes);
810
    }
811
812
    /**
813
     * The `<mark>` HTML element represents text which is marked or highlighted for reference or
814
     * notation purposes, due to the marked passage's relevance or importance in the enclosing context.
815
     *
816
     * @param string|array<int,string> $children   The Element children
817
     * @param array<string,mixed>      $attributes Attributes for the element
818
     *
819
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/mark
820
     */
821
    function mark($children, array $attributes = []): string
822
    {
823
        $attributes['children'] = $children;
824
825
        return createElement('mark', $attributes);
826
    }
827
828
    /**
829
     * The `<q>` HTML element indicates that the enclosed text is a short inline quotation.
830
     *
831
     * @param string|array<int,string> $children   The Element children
832
     * @param array<string,mixed>      $attributes Attributes for the element
833
     *
834
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/q
835
     */
836
    function q($children, array $attributes = []): string
837
    {
838
        $attributes['children'] = $children;
839
840
        return createElement('q', $attributes);
841
    }
842
    /**
843
     * The  `<rp>` HTML element is used to provide fall-back parentheses for browsers
844
     * that do not support display of ruby annotations using the ruby element.
845
     *
846
     * @param string|array<int,string> $children   The Element children
847
     * @param array<string,mixed>      $attributes Attributes for the element
848
     *
849
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/rp
850
     */
851
    function rp($children, array $attributes = []): string
852
    {
853
        $attributes['children'] = $children;
854
855
        return createElement('rp', $attributes);
856
    }
857
    /**
858
     * The `<rt>` HTML element specifies the ruby text component of a ruby annotation,
859
     * which is used to provide pronunciation, translation, or transliteration
860
     * information for East Asian typography.
861
     *
862
     * @param string|array<int,string> $children   The Element children
863
     * @param array<string,mixed>      $attributes Attributes for the element
864
     *
865
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/rt
866
     */
867
    function rt($children, array $attributes = []): string
868
    {
869
        $attributes['children'] = $children;
870
871
        return createElement('rt', $attributes);
872
    }
873
    /**
874
     * The `<ruby>` HTML element represents small annotations that are rendered above, below,
875
     * or next to base text, usually used for showing the pronunciation of East Asian characters.
876
     *
877
     * @param string|array<int,string> $children   The Element children
878
     * @param array<string,mixed>      $attributes Attributes for the element
879
     *
880
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ruby
881
     */
882
    function ruby($children, array $attributes = []): string
883
    {
884
        $attributes['children'] = $children;
885
886
        return createElement('ruby', $attributes);
887
    }
888
    /**
889
     * The `<s>` HTML element renders text with a strikethrough, or a line through it.
890
     *
891
     * @param string|array<int,string> $children   The Element children
892
     * @param array<string,mixed>      $attributes Attributes for the element
893
     *
894
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/s
895
     */
896
    function s($children, array $attributes = []): string
897
    {
898
        $attributes['children'] = $children;
899
900
        return createElement('s', $attributes);
901
    }
902
    /**
903
     * The `<samp>` HTML element is used to enclose inline text which represents
904
     * sample (or quoted) output from a computer program.
905
     *
906
     * @param string|array<int,string> $children   The Element children
907
     * @param array<string,mixed>      $attributes Attributes for the element
908
     *
909
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/samp
910
     */
911
    function samp($children, array $attributes = []): string
912
    {
913
        $attributes['children'] = $children;
914
915
        return createElement('samp', $attributes);
916
    }
917
    /**
918
     * The `<small>` HTML element represents side-comments and small print,
919
     * like copyright and legal text, independent of its styled presentation.
920
     *
921
     * @param string|array<int,string> $children   The Element children
922
     * @param array<string,mixed>      $attributes Attributes for the element
923
     *
924
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/small
925
     */
926
    function small($children, array $attributes = []): string
927
    {
928
        $attributes['children'] = $children;
929
930
        return createElement('small', $attributes);
931
    }
932
933
    /**
934
     * The `<span>` HTML element is a generic inline container for phrasing content,
935
     * which does not inherently represent anything.
936
     *
937
     * @param string|array<int,string> $children   The Element children
938
     * @param array<string,mixed>      $attributes Attributes for the element
939
     *
940
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/span
941
     */
942
    function span($children, array $attributes = []): string
943
    {
944 1
        $attributes['children'] = $children;
945
946 1
        return createElement('span', $attributes);
947
    }
948
949
    /**
950
     * The `<strong>` HTML element indicates that its contents have strong importance, seriousness, or urgency.
951
     *
952
     * @param string|array<int,string> $children   The Element children
953
     * @param array<string,mixed>      $attributes Attributes for the element
954
     *
955
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/strong
956
     */
957
    function strong($children, array $attributes = []): string
958
    {
959
        $attributes['children'] = $children;
960
961
        return createElement('strong', $attributes);
962
    }
963
964
    /**
965
     * The `<sub>` HTML element specifies inline text which should be displayed as
966
     * subscript for solely typographical reasons.
967
     *
968
     * @param string|array<int,string> $children   The Element children
969
     * @param array<string,mixed>      $attributes Attributes for the element
970
     *
971
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/sub
972
     */
973
    function sub($children, array $attributes = []): string
974
    {
975
        $attributes['children'] = $children;
976
977
        return createElement('sub', $attributes);
978
    }
979
980
    /**
981
     * The `<sup>` HTML element specifies inline text which is to be displayed as
982
     * superscript for solely typographical reasons.
983
     *
984
     * @param string|array<int,string> $children   The Element children
985
     * @param array<string,mixed>      $attributes Attributes for the element
986
     *
987
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/sup
988
     */
989
    function sup($children, array $attributes = []): string
990
    {
991
        $attributes['children'] = $children;
992
993
        return createElement('sub', $attributes);
994
    }
995
996
    /**
997
     * The `<time>` HTML element represents a specific period in time.
998
     *
999
     * @param string|array<int,string> $children   The Element children
1000
     * @param array<string,mixed>      $attributes Attributes for the element
1001
     *
1002
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/time
1003
     */
1004
    function time(string $datetime, $children, array $attributes = []): string
1005
    {
1006
        $attributes['datetime'] = $datetime;
1007
        $attributes['children'] = $children;
1008
1009
        return createElement('time', $attributes);
1010
    }
1011
1012
    /**
1013
     * The `<u>` HTML element represents an unarticulated annotation (Underline).
1014
     *
1015
     * @param string|array<int,string> $children   The Element children
1016
     * @param array<string,mixed>      $attributes Attributes for the element
1017
     *
1018
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/u
1019
     */
1020
    function u($children, array $attributes = []): string
1021
    {
1022
        $attributes['children'] = $children;
1023
1024
        return createElement('u', $attributes);
1025
    }
1026
1027
    /**
1028
     * The `<var>` HTML element represents the name of a variable in a mathematical expression or a programming context.
1029
     *
1030
     * @param string|array<int,string> $children   The Element children
1031
     * @param array<string,mixed>      $attributes Attributes for the element
1032
     *
1033
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/var
1034
     */
1035
    function _var($children, array $attributes = []): string
1036
    {
1037
        $attributes['children'] = $children;
1038
1039
        return createElement('var', $attributes);
1040
    }
1041
1042
    /**
1043
     * The `<wbr>` HTML element represents a word break opportunity.
1044
     *
1045
     * @param array<string,mixed> $attributes Attributes for the element
1046
     *
1047
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/wbr
1048
     */
1049
    function wbr(array $attributes = []): string
1050
    {
1051
        return createElement('wbr', $attributes, true);
1052
    }
1053
1054
    /**
1055
     * The `<area>` HTML element represents an image map area element.
1056
     *
1057
     * @param string              $shape      defines the values rect, which defines a rectangular region
1058
     * @param string|null         $coord      the coords attribute details the coordinates of the shape attribute in
1059
     *                                        size, shape, and placement of an area
1060
     * @param array<string,mixed> $attributes Attributes for the element
1061
     *
1062
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/area
1063
     */
1064
    function area(string $shape, string $coord = null, array $attributes = []): string
1065
    {
1066
        $attributes += ['shape' => $shape, 'coord' => $coord];
1067
1068
        return createElement('area', $attributes, true);
1069
    }
1070
1071
    /**
1072
     * The `<audio>` HTML element represents an embed audio element.
1073
     *
1074
     * @param string|array<int,string> $children   The Element children
1075
     * @param array<string,mixed>      $attributes Attributes for the element
1076
     *
1077
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio
1078
     */
1079
    function audio($children, array $attributes = []): string
1080
    {
1081
        $attributes['children'] = $children;
1082
1083
        return createElement('audio', $attributes);
1084
    }
1085
1086
    /**
1087
     * The `<img>` HTML element represents an image embed element.
1088
     *
1089
     * @param string              $src        is required, and contains the path to the image
1090
     * @param string              $alt        holds a text description of the image
1091
     * @param array<string,mixed> $attributes Attributes for the element
1092
     *
1093
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img
1094
     */
1095
    function img(string $src, string $alt = '', array $attributes = []): string
1096
    {
1097
        $attributes += ['src' => $src, 'alt' => $alt];
1098
1099
        return createElement('img', $attributes, true);
1100
    }
1101
1102
    /**
1103
     * The `<map>` HTML element represents an image map element.
1104
     *
1105
     * @param string                   $name       gives the map a name so that it can be referenced
1106
     * @param string|array<int,string> $children   The Element children
1107
     * @param array<string,mixed>      $attributes Attributes for the element
1108
     *
1109
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/map
1110
     */
1111
    function map(string $name, $children, array $attributes = []): string
1112
    {
1113
        $attributes += ['name' => $name, 'children' => $children];
1114
1115
        return createElement('map', $attributes);
1116
    }
1117
1118
    /**
1119
     * The `<track>` HTML element represents an embed text track element.
1120
     *
1121
     * @param string              $src        Address of the track (.vtt file). Must be a valid URL
1122
     * @param bool                $default    indicates that the track should be enabled
1123
     * @param string              $kind       How the text track is meant to be used
1124
     * @param array<string,mixed> $attributes Attributes for the element
1125
     *
1126
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/track
1127
     */
1128
    function track(string $src, bool $default = true, string $kind, array $attributes = []): string
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
1129
    {
1130
        $attributes += ['default' => $default, 'kind' => $kind, 'src' => $src];
1131
1132
        return createElement('track', $attributes, true);
1133
    }
1134
1135
    /**
1136
     * The `<video>` HTML element represents an image map element.
1137
     *
1138
     * @param string                   $src        The URL of the video to embed
1139
     * @param string|array<int,string> $children   The Element children
1140
     * @param array<string,mixed>      $attributes Attributes for the element
1141
     *
1142
     * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video
1143
     */
1144
    function video(string $src, bool $autoplay = true, $children, array $attributes = []): string
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
1145
    {
1146
        $attributes += ['src' => $src, 'children' => $children, 'autoplay' => $autoplay ? 'true' : 'false'];
1147
1148
        return createElement('video', $attributes);
1149
    }
1150
1151
    /**
1152
     * Create a grouped.
1153
     */
1154
    function elements(array $children): string
1155
    {
1156
        return \implode('', $children);
1157
    }
1158
1159
    /**
1160
     * Renders content as html comment. (eg: `<!-- Hello World -->`).
1161
     *
1162
     * @param string|array<int,string> $content The content for rendering
1163
     */
1164
    function comment($content): string
1165
    {
1166 1
        if (\is_array($content)) {
1167
            return \implode('', \array_map(__FUNCTION__, $content));
1168
        }
1169
1170 1
        return '<!-- ' . $content . ' -->';
1171
    }
1172
1173
    /**
1174
     * Loop over an array of items.
1175
     *
1176
     * @param array<int|string,mixed> $array   The array to loop through
1177
     * @param callable                $handler Call back function to run per iteration
1178
     */
1179
    function loop(array $array, callable $handler): string
1180
    {
1181 1
        $element = '';
1182
1183 1
        foreach ($array as $key => $value) {
1184 1
            $element .= $handler($value, $key);
1185
        }
1186
1187 1
        return $element;
1188
    }
1189
1190
    /**
1191
     * Create an HTML element.
1192
     *
1193
     * @param array<string,mixed> $attributes Attributes for the element
1194
     */
1195
    function createElement(string $tag, $attributes = [], bool $selfClosing = null): string
1196
    {
1197 1
        $html = '<' . $tag;
1198
1199 1
        if (!empty($attributes)) {
1200 1
            if (\is_string($attributes)) {
0 ignored issues
show
introduced by
The condition is_string($attributes) is always false.
Loading history...
1201
                $attributes = ['children' => $attributes];
1202
            }
1203
1204 1
            $children = $attributes['children'] ?? '';
1205 1
            unset($attributes['children']);
1206
1207 1
            $html .= HtmlElement::renderAttributes($attributes);
1208
        }
1209
1210 1
        if (null !== $selfClosing) {
1211 1
            return $html . ($selfClosing ? '/' : '') . '>';
1212
        }
1213
1214 1
        if (isset($children)) {
1215 1
            $children = (\is_array($children) ? \implode('', $children) : $children);
1216
        }
1217
1218 1
        return $html . '>' . ($children ?? null) . '</' . $tag . '>';
1219
    }
1220
}
1221