Completed
Push — master ( 579af5...b29473 )
by Oleg
07:53
created

Html::doctype()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 19
rs 9.4285
cc 2
eloc 13
nc 2
nop 1
1
<?php /** MicroHtml */
2
3
namespace Micro\Web;
4
5
/**
6
 * Html class file.
7
 *
8
 * @author Oleg Lunegov <[email protected]>
9
 * @link https://github.com/lugnsk/micro
10
 * @copyright Copyright &copy; 2013 Oleg Lunegov
11
 * @license /LICENSE
12
 * @package Micro
13
 * @subpackage Web
14
 * @version 1.0
15
 * @since 1.0
16
 */
17
class Html
18
{
19
    // BASIC Elements
20
21
    /**
22
     * Render meta tag
23
     *
24
     * @access public
25
     *
26
     * @param  string $name name of element
27
     * @param  string $content content of element
28
     * @param  array $attributes attributes tag
29
     *
30
     * @return string
31
     * @static
32
     */
33
    public static function meta($name, $content, array $attributes = [])
34
    {
35
        $attributes['name'] = $name;
36
        $attributes['content'] = $content;
37
38
        return self::tag('meta', $attributes);
39
    }
40
41
    /**
42
     * Render tag
43
     *
44
     * @access public
45
     *
46
     * @param  string $name tag name
47
     * @param  array $attributes tag attributes
48
     *
49
     * @return string
50
     * @static
51
     */
52 View Code Duplication
    public static function tag($name, array $attributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $result = '';
55
        foreach ($attributes AS $elem => $value) {
56
            $result .= ' ' . $elem . '="' . $value . '" ';
57
        }
58
59
        return '<' . $name . $result . '/>';
60
    }
61
62
    /**
63
     * Render link tag
64
     *
65
     * @access public
66
     *
67
     * @param  string $name name of element
68
     * @param  string $url url path
69
     * @param  array $attributes attributes tag
70
     *
71
     * @return string
72
     * @static
73
     */
74
    public static function link($name, $url, array $attributes = [])
75
    {
76
        $attributes['href'] = $url;
77
78
        return self::openTag('link', $attributes) . $name . self::closeTag('link');
79
    }
80
81
    // HEAD Elements
82
83
    /**
84
     * Render open tag
85
     *
86
     * @access public
87
     *
88
     * @param  string $name tag name
89
     * @param  array $attributes tag attributes
90
     *
91
     * @return string
92
     * @static
93
     */
94 View Code Duplication
    public static function openTag($name, array $attributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96
        $result = '';
97
        foreach ($attributes AS $key => $value) {
98
            $result .= ' ' . $key . '="' . $value . '"';
99
        }
100
101
        return '<' . $name . $result . '>';
102
    }
103
104
    /**
105
     * Render close tag
106
     *
107
     * @access public
108
     *
109
     * @param  string $name tag name
110
     *
111
     * @return string
112
     * @static
113
     */
114
    public static function closeTag($name)
115
    {
116
        return '</' . $name . '>';
117
    }
118
119
    /**
120
     * Render favicon file
121
     *
122
     * @access public
123
     *
124
     * @param string $url path to favicon
125
     *
126
     * @return string
127
     * @static
128
     */
129
    public static function favicon($url)
130
    {
131
        return self::tag('link', ['href' => $url, 'rel' => 'shortcut icon', 'type' => 'image/x-icon']);
132
    }
133
134
    /**
135
     * Render css file
136
     *
137
     * @access public
138
     *
139
     * @param  string $file path to css
140
     *
141
     * @return string
142
     * @static
143
     */
144
    public static function cssFile($file)
145
    {
146
        return self::tag('link', ['href' => $file, 'rel' => 'stylesheet']);
147
    }
148
149
    /**
150
     * Render script file
151
     *
152
     * @access public
153
     *
154
     * @param  string $file path to script
155
     *
156
     * @return string
157
     * @static
158
     */
159
    public static function scriptFile($file)
160
    {
161
        return self::openTag('script', ['src' => $file, 'type' => 'text/javascript']) . self::closeTag('script');
162
    }
163
164
    /**
165
     * Render style source
166
     *
167
     * @access public
168
     *
169
     * @param  string $text style
170
     * @param  array $attributes attributes tag
171
     *
172
     * @return string
173
     * @static
174
     */
175
    public static function css($text, array $attributes = [])
176
    {
177
        $attributes['type'] = 'text/css';
178
179
        return self::openTag('style', $attributes) . $text . self::closeTag('style');
180
    }
181
182
    /**
183
     * Render script source
184
     *
185
     * @access public
186
     *
187
     * @param  string $text script
188
     * @param  array $attributes attributes tag
189
     *
190
     * @return string
191
     * @static
192
     */
193
    public static function script($text, array $attributes = [])
194
    {
195
        $attributes['type'] = 'text/javascript';
196
197
        return self::openTag('script',
198
            $attributes) . ' /*<![CDATA[*/ ' . $text . ' /*]]>*/ ' . self::closeTag('script');
199
    }
200
201
    /**
202
     * Render docType tag
203
     *
204
     * @access public
205
     *
206
     * @param  string $name doctype name
207
     *
208
     * @return string|boolean
209
     * @static
210
     */
211
    public static function doctype($name)
212
    {
213
        $docTypes = array(
214
            'xhtml11' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
215
            'xhtml1-trans' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
216
            'xhtml1-strict' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
217
            'xhtml1-frame' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
218
            'html4-trans' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
219
            'html4-strict' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
220
            'html4-frame' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
221
            'html5' => '<!DOCTYPE html>'
222
        );
223
224
        if (empty($docTypes[$name])) {
225
            return false;
226
        }
227
228
        return $docTypes[$name];
229
    }
230
231
    /**
232
     * Render title tag
233
     *
234
     * @access public
235
     *
236
     * @param string $name title name
237
     *
238
     * @return string
239
     * @static
240
     */
241
    public static function title($name)
242
    {
243
        return self::openTag('title') . $name . self::closeTag('title');
244
    }
245
246
    /**
247
     * Render BR tag
248
     *
249
     * @access public
250
     *
251
     * @param integer $num number of render BR's
252
     * @param array $attributes attributes tag
253
     *
254
     * @return string
255
     * @static
256
     */
257
    public static function br($num = 1, array $attributes = [])
258
    {
259
        $str = '';
260
        for ($i = 0; $i < $num; $i++) {
261
            $str .= self::tag('br', $attributes);
262
        }
263
264
        return $str;
265
    }
266
267
    // BODY Elements
268
269
    /**
270
     * Render mail a tag
271
     *
272
     * @access public
273
     *
274
     * @param  string $name name of e-mail
275
     * @param  string $email e-mail path
276
     * @param  array $attributes attributes tag
277
     *
278
     * @return string
279
     * @static
280
     */
281
    public static function mailto($name, $email, array $attributes = [])
282
    {
283
        $attributes['href'] = 'mailto:' . $email;
284
285
        return self::openTag('a', $attributes) . $name . self::closeTag('a');
286
    }
287
288
    /**
289
     * Render anchor
290
     *
291
     * @access public
292
     *
293
     * @param string $name name to link
294
     * @param string $url path to link
295
     * @param array $attributes attributes tag
296
     *
297
     * @return string
298
     * @static
299
     */
300
    public static function href($name, $url, array $attributes = [])
301
    {
302
        $attributes['href'] = $url;
303
304
        return self::openTag('a', $attributes) . $name . self::closeTag('a');
305
    }
306
307
    /**
308
     * Render H{1-N} tag
309
     *
310
     * @access public
311
     *
312
     * @param  string $num H number
313
     * @param  string $value H value
314
     * @param  array $attributes attributes tag
315
     *
316
     * @return string
317
     * @static
318
     */
319
    public static function heading($num, $value = null, array $attributes = [])
320
    {
321
        return self::openTag('h' . $num, $attributes) . $value . self::closeTag('h' . $num);
322
    }
323
324
    /**
325
     * Render image map tag
326
     *
327
     * @access public
328
     *
329
     * @param string $alt Alternative text
330
     * @param string $source Path to image
331
     * @param string $name Map name
332
     * @param array $attributeImg Attributes for image
333
     * @param array $coordinates Coordinates for image
334
     *
335
     * @return string
336
     * @static
337
     */
338
    public static function imgmap($alt, $source, $name, array $attributeImg = [], array $coordinates = [])
339
    {
340
        $areas = '';
341
        foreach ($coordinates AS $coord) {
342
            $areas .= self::tag('area', $coord);
343
        }
344
345
        $attributeImg['usemap'] = $name;
346
347
        return self::image($alt, $source, $attributeImg) .
348
        self::openTag('map', ['name' => $name, 'id' => $name]) .
349
        $areas . self::closeTag('map');
350
    }
351
352
    /**
353
     * Render image file
354
     *
355
     * @access public
356
     *
357
     * @param  string $name name of image
358
     * @param  string $file path image file
359
     * @param  array $attributes attributes tag
360
     *
361
     * @return string
362
     * @static
363
     */
364
    public static function image($name, $file, array $attributes = [])
365
    {
366
        $attributes['src'] = $file;
367
        $attributes['alt'] = $name;
368
369
        return self::tag('img', $attributes);
370
    }
371
372
    /**
373
     * Render object tag
374
     *
375
     * @access public
376
     *
377
     * @param string $source Path to content
378
     * @param array $attributes Attributes for object
379
     * @param array $params Parameters for object
380
     *
381
     * @return string
382
     * @static
383
     */
384
    public static function object($source, array $attributes = [], array $params = [])
385
    {
386
        $attributes['data'] = $source;
387
        $paramsConverted = '';
388
389
        foreach ($params AS $key => $val) {
390
            $paramsConverted .= self::tag('param', ['name' => $key, 'value' => $val]);
391
        }
392
393
        return self::openTag('object', $attributes) . $paramsConverted . self::closeTag('object');
394
    }
395
396
    /**
397
     * Embedding objects (video, audio, flash, etc.)
398
     *
399
     * @access public
400
     *
401
     * @param string $source Path to content
402
     * @param array $attributes Attributes for embedding
403
     *
404
     * @return string
405
     */
406
    public static function embed($source, array $attributes = [])
407
    {
408
        $attributes['source'] = $source;
409
410
        return self::openTag('embed', $attributes) . self::closeTag('embed');
411
    }
412
413
    /**
414
     * List elements generator
415
     *
416
     * @access public
417
     *
418
     * @param array $items lists multiple array
419
     * @param array $attributes attributes tag
420
     * @param bool $isNumeric Is a numeric list?
421
     *
422
     * @return string
423
     * @static
424
     */
425
    public static function lists(array $items = [], array $attributes = [], $isNumeric = false)
426
    {
427
        $parentTag = ($isNumeric) ? 'ol' : 'ul';
428
429
        $result = null;
430
        foreach ($items AS $item) {
431
            $result .= Html::openTag('li', !empty($item['attr']) ? $item['attr'] : []);
432
            if (!empty($item['parents'])) {
433
                $result .= !empty($item['text']) ? $item['text'] : null;
434
                $result .= self::lists(
435
                    $item['parents'],
436
                    (!empty($item['parentsAttr']) ? $item['parentsAttr'] : []),
437
                    (!empty($item['parentsIsNumeric']) ? true : false)
438
                );
439
            } else {
440
                $result .= $item['text'];
441
            }
442
            $result .= Html::closeTag('li');
443
        }
444
445
        return self::openTag($parentTag, $attributes) . $result . self::closeTag($parentTag);
446
    }
447
448
    // LIST Elements
449
450
    /**
451
     * Render table element
452
     *
453
     * How to use $elements:
454
     * array(
455
     *     array( // row
456
     *         'cells'=>array( // cell
457
     *             'value'=>'text',
458
     *             'attributes'=>[]
459
     *         ),
460
     *         attributes'=>[]
461
     *     )
462
     * )
463
     *
464
     * @access public
465
     *
466
     * @param array $elements table elements
467
     * @param array $attributes attributes tag
468
     *
469
     * @return string
470
     * @static
471
     */
472
    public static function table(array $elements = [], array $attributes = [])
473
    {
474
        $output = null;
475
        foreach ($elements AS $value) {
476
            $output .= self::tableRow(
477
                !empty($value['cells']) ? $value['cells'] : [],
478
                !empty($value['header']) ? $value['header'] : false,
479
                !empty($value['attributes']) ? $value['attributes'] : []
480
            );
481
        }
482
483
        return self::beginTable($attributes) . $output . self::endTable();
484
    }
485
486
    // TABLE Elements
487
488
    /**
489
     * Render table row element
490
     *
491
     * @access public
492
     *
493
     * @param array $elements array(value, attributes)
494
     * @param boolean $isHeading row is heading?
495
     * @param array $attributes attributes tag
496
     *
497
     * @return string
498
     * @static
499
     */
500
    public static function tableRow(array $elements = [], $isHeading = false, array $attributes = [])
501
    {
502
        $output = null;
503
        foreach ($elements AS $value) {
504
            if ($isHeading === false) {
505
                $output .= self::tableCell(
506
                    !empty($value['value']) ? $value['value'] : [],
507
                    !empty($value['attributes']) ? $value['attributes'] : []
508
                );
509
            } else {
510
                $output .= self::tableHeading(
511
                    !empty($value['value']) ? $value['value'] : [],
512
                    !empty($value['attributes']) ? $value['attributes'] : []
513
                );
514
            }
515
        }
516
517
        return self::openTag('tr', $attributes) . $output . self::closeTag('tr');
518
    }
519
520
    /**
521
     * Render table cell element
522
     *
523
     * @access public
524
     *
525
     * @param string $text table cell text
526
     * @param array $attributes attributes tag
527
     *
528
     * @return string
529
     * @static
530
     */
531
    public static function tableCell($text, array $attributes = [])
532
    {
533
        return self::openTag('td', $attributes) . $text . self::closeTag('td');
534
    }
535
536
    /**
537
     * Render table heading tag
538
     *
539
     * @access public
540
     *
541
     * @param string $text table heading text
542
     * @param array $attributes attributes tag
543
     *
544
     * @return string
545
     * @static
546
     */
547
    public static function tableHeading($text, array $attributes = [])
548
    {
549
        return self::openTag('th', $attributes) . $text . self::closeTag('th');
550
    }
551
552
    /**
553
     * Render begin table element
554
     *
555
     * @access public
556
     *
557
     * @param array $attributes attributes tag
558
     *
559
     * @return string
560
     * @static
561
     */
562
    public static function beginTable(array $attributes = [])
563
    {
564
        return self::openTag('table', $attributes);
565
    }
566
567
    /**
568
     * Render end table element
569
     *
570
     * @access public
571
     * @return string
572
     * @static
573
     */
574
    public static function endTable()
575
    {
576
        return self::closeTag('table');
577
    }
578
579
    /**
580
     * Render table caption element
581
     *
582
     * @access public
583
     *
584
     * @param string $text table caption text
585
     * @param array $attributes attributes tag
586
     *
587
     * @return string
588
     * @static
589
     */
590
    public static function tableCaption($text, array $attributes = [])
591
    {
592
        return self::openTag('caption', $attributes) . $text . self::closeTag('caption');
593
    }
594
595
    /**
596
     * Render begin form tag
597
     *
598
     * @access public
599
     *
600
     * @param  string $action path to URL action
601
     * @param  string $method method of request
602
     * @param  array $attributes attributes tag
603
     *
604
     * @return string
605
     * @static
606
     */
607 View Code Duplication
    public static function beginForm($action, $method = 'POST', array $attributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
608
    {
609
        $attributes['action'] = $action;
610
        $attributes['method'] = $method;
611
612
        return self::openTag('form', $attributes);
613
    }
614
615
    // FORM Elements
616
617
    /**
618
     * Render end form tag
619
     *
620
     * @access public
621
     * @return string
622
     * @static
623
     */
624
    public static function endForm()
625
    {
626
        return self::closeTag('form');
627
    }
628
629
    /**
630
     * Render image button tag
631
     *
632
     * @access public
633
     *
634
     * @param  string $name image name
635
     * @param  string $file image file path
636
     * @param  array $attributesButton attributes for button
637
     * @param  array $attributesImage attributes for image
638
     *
639
     * @return string
640
     * @static
641
     */
642
    public static function imageButton($name, $file, array $attributesButton = [], array $attributesImage = [])
643
    {
644
        return self::button(self::image($name, $file, $attributesImage), $attributesButton);
645
    }
646
647
    /**
648
     * Render button tag
649
     *
650
     * @access public
651
     *
652
     * @param  string $text text for button
653
     * @param  array $attributes attributes tag
654
     *
655
     * @return string
656
     * @static
657
     */
658
    public static function button($text, array $attributes = [])
659
    {
660
        return self::openTag('button', $attributes) . $text . self::closeTag('button');
661
    }
662
663
    /**
664
     * Render textArea tag
665
     *
666
     * @access public
667
     *
668
     * @param  string $name textArea name
669
     * @param  string $text textArea text
670
     * @param  array $attributes attributes tag
671
     *
672
     * @return string
673
     * @static
674
     */
675 View Code Duplication
    public static function textArea($name, $text, array $attributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
676
    {
677
        $attributes['id'] = $name;
678
        $attributes['name'] = $name;
679
680
        return self::openTag('textarea', $attributes) . $text . self::closeTag('textarea');
681
    }
682
683
    /**
684
     * Render legend tag
685
     *
686
     * @access public
687
     *
688
     * @param  string $text legend text
689
     * @param  array $attributes attributes tag
690
     *
691
     * @return string
692
     * @static
693
     */
694
    public static function legend($text, array $attributes = [])
695
    {
696
        return self::openTag('legend', $attributes) . $text . self::closeTag('legend');
697
    }
698
699
    /**
700
     * Render label tag
701
     *
702
     * @access public
703
     *
704
     * @param string $name label name
705
     * @param string $elemId element ID
706
     * @param array $attributes attributes tag
707
     *
708
     * @return string
709
     * @static
710
     */
711
    public static function label($name, $elemId = '', array $attributes = [])
712
    {
713
        $attributes['for'] = $elemId;
714
715
        return self::openTag('label', $attributes) . $name . self::closeTag('label');
716
    }
717
718
    /**
719
     * Render dropDownList (select tag)
720
     *
721
     * @access public
722
     *
723
     * @param string $name dropDown name
724
     * @param array $options format array(value, text, attributes) OR array(label, options, attributes)
725
     * @param array $attributes attributes tag
726
     *
727
     * @return string
728
     * @static
729
     */
730
    public static function dropDownList($name, array $options = [], array $attributes = [])
731
    {
732
        $attributes['id'] = $name;
733
        $attributes['size'] = 1;
734
735
        return self::listBox($name, $options, $attributes);
736
    }
737
738
    /**
739
     * Render listBox (select tag)
740
     *
741
     * @access public
742
     *
743
     * @param string $name listBox name
744
     * @param array $options format array(value, text, attributes) OR array(label, options, attributes)
745
     * @param array $attributes attributes tag
746
     *
747
     * @return string
748
     * @static
749
     */
750
    public static function listBox($name, array $options = [], array $attributes = [])
751
    {
752
        if (!empty($attributes['selected'])) {
753
            $selected = $attributes['selected'];
754
            unset($attributes['selected']);
755
        } else {
756
            $selected = null;
757
        }
758
759
        $attributes['name'] = $name;
760
        $opts = '';
761
        foreach ($options AS $option) {
762
            if (!empty($option['label'])) {
763
                $opts .= self::optGroup($option['label'], $option['options'], $option['attributes']);
764
            } else {
765
                $attr = [];
766
                if (!empty($option['attributes'])) {
767
                    $attr = $option['attributes'];
768
                    unset($option['attributes']);
769
                }
770
771
                if (!empty($option['value']) && (string)$option['value'] === (string)$selected) {
772
                    $attr['selected'] = 'selected';
773
                }
774
775
                $text = '';
776
                if (!empty($option['text'])) {
777
                    $text = $option['text'];
778
                    unset($option['text']);
779
                }
780
781
                $opts .= self::option(!empty($option['value']) ? $option['value'] : '', $text, $attr);
782
            }
783
        }
784
785
        $attributes['name'] .= array_key_exists('multiple', $attributes) ? '[]' : '';
786
787
        return self::openTag('select', $attributes) . $opts . self::closeTag('select');
788
    }
789
790
    /**
791
     * Render optGroup tag
792
     *
793
     * @access public
794
     *
795
     * @param string $label label for options group
796
     * @param array $options format array(value, text, attributes) OR array(label, options, attributes)
797
     * @param array $attributes attributes tag
798
     *
799
     * @return string
800
     * @static
801
     */
802
    public static function optGroup($label, array $options = [], array $attributes = [])
803
    {
804
        $attributes['label'] = $label;
805
        $opts = '';
806
        foreach ($options AS $option) {
807
            if (!empty($option['label'])) {
808
                $opts .= self::optGroup($option['label'], $option['options'], $option['attributes']);
809
            } else {
810
                $opts .= self::option($option['value'], $option['text'], $option['attributes']);
811
            }
812
        }
813
814
        return self::openTag('optgroup', $attributes) . $opts . self::closeTag('optgroup');
815
    }
816
817
    /**
818
     * Render option tag
819
     *
820
     * @access public
821
     *
822
     * @param string $value option value
823
     * @param string $text label for option
824
     * @param array $attributes attributes tag
825
     *
826
     * @return string
827
     * @static
828
     */
829
    public static function option($value, $text, array $attributes = [])
830
    {
831
        $attributes['value'] = $value;
832
833
        return self::openTag('option', $attributes) . $text . self::closeTag('option');
834
    }
835
836
    /**
837
     * Converting array to options
838
     *
839
     * @param array $arr Input array
840
     *
841
     * @return array|null Output array
842
     */
843
    public static function arrayToOptions(array $arr = [])
844
    {
845
        $result = [];
846
        foreach ($arr AS $n => $m) {
847
            $result[] = ['value' => $n, 'text' => $m];
848
        }
849
850
        return $result;
851
    }
852
853
    /**
854
     * Render checkBoxList (input checkbox tags)
855
     *
856
     * @access public
857
     *
858
     * @param string $name name for checkBox'es in list
859
     * @param array $checkboxes format array(text, value, attributes)
860
     * @param string $format %check% - checkbox , %text% - text
861
     * @param string $selected name selected element
862
     *
863
     * @return string
864
     * @static
865
     */
866
    public static function checkBoxList(
867
        $name,
868
        array $checkboxes = [],
869
        $format = '<p>%check% %text%</p>',
870
        $selected = ''
871
    ) {
872
        $checks = '';
873
        foreach ($checkboxes AS $checkbox) {
874
            if ($checkbox['value'] === $selected) {
875
                $checkbox['attributes']['selected'] = 'selected';
876
            }
877
            $check = self::checkBoxField($name, $checkbox['value'], $checkbox['attributes']);
878
            $checks .= str_replace('%text%', $checkbox['text'], str_replace('%check%', $check, $format));
879
        }
880
881
        return $checks;
882
    }
883
884
    /**
885
     * Render input checkbox tag
886
     *
887
     * @access public
888
     *
889
     * @param  string $name checkBox name
890
     * @param  string $value checkBox value
891
     * @param  array $attributes attributes tag
892
     *
893
     * @return string
894
     * @static
895
     */
896
    public static function checkBoxField($name, $value = null, array $attributes = [])
897
    {
898
        return self::field('checkbox', $name, $value, $attributes);
899
    }
900
901
    /**
902
     * Base field tag
903
     *
904
     * @access private
905
     *
906
     * @param  string $type type of element
907
     * @param  string $name name of element
908
     * @param  string $value value of element
909
     * @param  array $attributes attributes tag
910
     *
911
     * @return string
912
     * @static
913
     */
914
    private static function field($type, $name, $value = null, array $attributes = [])
915
    {
916
        $attributes['id'] = !empty($attributes['id']) ? $attributes['id'] : $name;
917
        $attributes['type'] = $type;
918
        $attributes['name'] = $name;
919
        $attributes['value'] = $value;
920
921
        return self::tag('input', $attributes);
922
    }
923
924
    // INPUT Elements
925
926
    /**
927
     * Render radio button list tag
928
     *
929
     * @access public
930
     *
931
     * @param string $name radio name
932
     * @param array $radios format array(text, value, attributes)
933
     * @param string $format %radio% - radio , %text% - text
934
     * @param string $selected name selected element
935
     *
936
     * @return string
937
     * @static
938
     */
939
    public static function radioButtonList($name, array $radios = [], $format = '<p>%radio% %text%</p>', $selected = '')
940
    {
941
        $rads = '';
942
        foreach ($radios AS $radio) {
943
            if (strcmp($radio['value'], $selected) === 0) {
944
                $radio['attributes']['checked'] = 'checked';
945
            }
946
            $rad = self::radioField($name, $radio['value'],
947
                !empty($radio['attributes']) ? $radio['attributes'] : []);
948
            $rads .= str_replace(['%radio%', '%text%'], [$rad, $radio['text']], $format);
949
        }
950
951
        return $rads;
952
    }
953
954
    /**
955
     * Render input radio tag
956
     *
957
     * @access public
958
     *
959
     * @param  string $name radio name
960
     * @param  string $value radio value
961
     * @param  array $attributes attributes tag
962
     *
963
     * @return string
964
     * @static
965
     */
966
    public static function radioField($name, $value = null, array $attributes = [])
967
    {
968
        return self::field('radio', $name, $value, $attributes);
969
    }
970
971
    /**
972
     * Render reset button tag
973
     *
974
     * @access public
975
     *
976
     * @param  string $label text for label on button
977
     * @param  array $attributes attributes tag
978
     *
979
     * @return string
980
     * @static
981
     */
982 View Code Duplication
    public static function resetButton($label = 'Reset', array $attributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
983
    {
984
        $attributes['type'] = 'reset';
985
        $attributes['value'] = $label;
986
987
        return self::tag('input', $attributes);
988
    }
989
990
    /**
991
     * Render submit button tag
992
     *
993
     * @access public
994
     *
995
     * @param  string $label text for label on button
996
     * @param  array $attributes attributes tag
997
     *
998
     * @return string
999
     * @static
1000
     */
1001 View Code Duplication
    public static function submitButton($label = 'Submit', array $attributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1002
    {
1003
        $attributes['type'] = 'submit';
1004
        $attributes['value'] = $label;
1005
1006
        return self::tag('input', $attributes);
1007
    }
1008
1009
    /**
1010
     * Render input button tag
1011
     *
1012
     * @access public
1013
     *
1014
     * @param  string $name button name
1015
     * @param  string $value button value
1016
     * @param  array $attributes attributes tag
1017
     *
1018
     * @return string
1019
     * @static
1020
     */
1021
    public static function buttonField($name, $value = null, array $attributes = [])
1022
    {
1023
        return self::field('button', $name, $value, $attributes);
1024
    }
1025
1026
    /**
1027
     * Render input file tag
1028
     *
1029
     * @access public
1030
     *
1031
     * @param  string $name file name
1032
     * @param  string $value file value
1033
     * @param  array $attributes attributes tag
1034
     *
1035
     * @return string
1036
     * @static
1037
     */
1038
    public static function fileField($name, $value = null, array $attributes = [])
1039
    {
1040
        return self::field('file', $name, $value, $attributes);
1041
    }
1042
1043
    /**
1044
     * Render input hidden tag
1045
     *
1046
     * @access public
1047
     *
1048
     * @param  string $name hidden name
1049
     * @param  string $value hidden value
1050
     * @param  array $attributes attributes tag
1051
     *
1052
     * @return string
1053
     * @static
1054
     */
1055
    public static function hiddenField($name, $value = null, array $attributes = [])
1056
    {
1057
        return self::field('hidden', $name, $value, $attributes);
1058
    }
1059
1060
    /**
1061
     * Render input image tag
1062
     *
1063
     * @access public
1064
     *
1065
     * @param  string $name image name
1066
     * @param  string $value image value
1067
     * @param  string $srcFile path to image
1068
     * @param  array $attributes attributes tag
1069
     *
1070
     * @return string
1071
     * @static
1072
     */
1073
    public static function imageField($name, $value = null, $srcFile, array $attributes = [])
1074
    {
1075
        $attributes['src'] = $srcFile;
1076
1077
        return self::field('image', $name, $value, $attributes);
1078
    }
1079
1080
    /**
1081
     * Render input password tag
1082
     *
1083
     * @access public
1084
     *
1085
     * @param  string $name password name
1086
     * @param  string $value password value
1087
     * @param  array $attributes attributes tag
1088
     *
1089
     * @return string
1090
     * @static
1091
     */
1092
    public static function passwordField($name, $value = null, array $attributes = [])
1093
    {
1094
        return self::field('password', $name, $value, $attributes);
1095
    }
1096
1097
    /**
1098
     * Render input text tag
1099
     *
1100
     * @access public
1101
     *
1102
     * @param  string $name text name
1103
     * @param  string $value text value
1104
     * @param  array $attributes attributes tag
1105
     *
1106
     * @return string
1107
     * @static
1108
     */
1109
    public static function textField($name, $value = null, array $attributes = [])
1110
    {
1111
        return self::field('text', $name, $value, $attributes);
1112
    }
1113
1114
    /**
1115
     * Render input color tag
1116
     *
1117
     * @access public
1118
     *
1119
     * @param  string $name color name
1120
     * @param  string $value color value
1121
     * @param  array $attributes attributes tag
1122
     *
1123
     * @return string
1124
     * @static
1125
     */
1126
    public static function colorField($name, $value = null, array $attributes = [])
1127
    {
1128
        return self::field('color', $name, $value, $attributes);
1129
    }
1130
1131
    /**
1132
     * Render input date tag
1133
     *
1134
     * @access public
1135
     *
1136
     * @param  string $name date name
1137
     * @param  string $value date value
1138
     * @param  array $attributes attributes tag
1139
     *
1140
     * @return string
1141
     * @static
1142
     */
1143
    public static function dateField($name, $value = null, array $attributes = [])
1144
    {
1145
        return self::field('date', $name, $value, $attributes);
1146
    }
1147
1148
    /**
1149
     * Render input datetime tag
1150
     *
1151
     * @access public
1152
     *
1153
     * @param  string $name datetime name
1154
     * @param  string $value datetime value
1155
     * @param  array $attributes attributes tag
1156
     *
1157
     * @return string
1158
     * @static
1159
     */
1160
    public static function datetimeField($name, $value = null, array $attributes = [])
1161
    {
1162
        return self::field('datetime', $name, $value, $attributes);
1163
    }
1164
1165
    /**
1166
     * Render input datetime-local tag
1167
     *
1168
     * @access public
1169
     *
1170
     * @param  string $name datetime-local name
1171
     * @param  string $value datetime-local value
1172
     * @param  array $attributes attributes tag
1173
     *
1174
     * @return string
1175
     * @static
1176
     */
1177
    public static function datetimeLocalField($name, $value = null, array $attributes = [])
1178
    {
1179
        return self::field('datetime-local', $name, $value, $attributes);
1180
    }
1181
1182
    /**
1183
     * Render input email tag
1184
     *
1185
     * @access public
1186
     *
1187
     * @param  string $name email name
1188
     * @param  string $value email value
1189
     * @param  array $attributes attributes tag
1190
     *
1191
     * @return string
1192
     * @static
1193
     */
1194
    public static function emailField($name, $value = null, array $attributes = [])
1195
    {
1196
        return self::field('email', $name, $value, $attributes);
1197
    }
1198
1199
    /**
1200
     * Render input number tag
1201
     *
1202
     * @access public
1203
     *
1204
     * @param  string $name number name
1205
     * @param  string $value number value
1206
     * @param  array $attributes attributes tag
1207
     *
1208
     * @return string
1209
     * @static
1210
     */
1211
    public static function numberField($name, $value = null, array $attributes = [])
1212
    {
1213
        return self::field('number', $name, $value, $attributes);
1214
    }
1215
1216
    /**
1217
     * Render input range tag
1218
     *
1219
     * @access public
1220
     *
1221
     * @param  string $name range name
1222
     * @param  string $value range value
1223
     * @param  array $attributes attributes tag
1224
     *
1225
     * @return string
1226
     * @static
1227
     */
1228
    public static function rangeField($name, $value = null, array $attributes = [])
1229
    {
1230
        return self::field('range', $name, $value, $attributes);
1231
    }
1232
1233
    /**
1234
     * Render input search tag
1235
     *
1236
     * @access public
1237
     *
1238
     * @param  string $name search name
1239
     * @param  string $value search value
1240
     * @param  array $attributes attributes tag
1241
     *
1242
     * @return string
1243
     * @static
1244
     */
1245
    public static function searchField($name, $value = null, array $attributes = [])
1246
    {
1247
        return self::field('search', $name, $value, $attributes);
1248
    }
1249
1250
    /**
1251
     * Render input tel tag
1252
     *
1253
     * @access public
1254
     *
1255
     * @param  string $name telephone name
1256
     * @param  string $value telephone value
1257
     * @param  array $attributes attributes tag
1258
     *
1259
     * @return string
1260
     * @static
1261
     */
1262
    public static function telField($name, $value = null, array $attributes = [])
1263
    {
1264
        return self::field('tel', $name, $value, $attributes);
1265
    }
1266
1267
    /**
1268
     * Render input time tag
1269
     *
1270
     * @access public
1271
     *
1272
     * @param  string $name time name
1273
     * @param  string $value time value
1274
     * @param  array $attributes attributes tag
1275
     *
1276
     * @return string
1277
     * @static
1278
     */
1279
    public static function timeField($name, $value = null, array $attributes = [])
1280
    {
1281
        return self::field('time', $name, $value, $attributes);
1282
    }
1283
1284
    /**
1285
     * Render input url tag
1286
     *
1287
     * @access public
1288
     *
1289
     * @param  string $name url name
1290
     * @param  string $value url path
1291
     * @param  array $attributes attributes tag
1292
     *
1293
     * @return string
1294
     * @static
1295
     */
1296
    public static function urlField($name, $value = null, array $attributes = [])
1297
    {
1298
        return self::field('url', $name, $value, $attributes);
1299
    }
1300
1301
    /**
1302
     * Render input month tag
1303
     *
1304
     * @access public
1305
     *
1306
     * @param  string $name month name
1307
     * @param  string $value month value
1308
     * @param  array $attributes attributes tag
1309
     *
1310
     * @return string
1311
     * @static
1312
     */
1313
    public static function monthField($name, $value = null, array $attributes = [])
1314
    {
1315
        return self::field('month', $name, $value, $attributes);
1316
    }
1317
1318
    /**
1319
     * Render input week tag
1320
     *
1321
     * @access public
1322
     *
1323
     * @param  string $name week name
1324
     * @param  string $value week value
1325
     * @param  array $attributes attributes tag
1326
     *
1327
     * @return string
1328
     * @static
1329
     */
1330
    public static function weekField($name, $value = null, array $attributes = [])
1331
    {
1332
        return self::field('week', $name, $value, $attributes);
1333
    }
1334
1335
    // HTML5 Only
1336
    /**
1337
     * Render charset tag
1338
     *
1339
     * @access public
1340
     *
1341
     * @param  string $name charset name
1342
     *
1343
     * @return string
1344
     * @static
1345
     */
1346
    public static function charset($name)
1347
    {
1348
        return self::tag('meta', ['charset' => $name]);
1349
    }
1350
1351
    /**
1352
     * Render video tag
1353
     *
1354
     * @access public
1355
     *
1356
     * @param array $sources format type=>src
1357
     * @param array $tracks format array(kind, src, srclang, label)
1358
     * @param array $attributes attributes tag
1359
     * @param string $noCodec text
1360
     *
1361
     * @return string
1362
     * @static
1363
     */
1364 View Code Duplication
    public static function video(array $sources = [], array $tracks = [], array $attributes = [], $noCodec = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1365
    {
1366
        $srcs = '';
1367
        foreach ($sources AS $name => $value) {
1368
            $srcs .= self::tag('source', ['type' => $name, 'src' => $value]);
1369
        }
1370
        foreach ($tracks AS $track) {
1371
            $srcs .= self::tag('track', [
1372
                'kind' => $track['kind'],
1373
                'src' => $track['src'],
1374
                'srclang' => $track['srclang'],
1375
                'label' => $track['label']
1376
            ]);
1377
        }
1378
1379
        return self::openTag('video', $attributes) . $srcs . $noCodec . self::closeTag('video');
1380
    }
1381
1382
    /**
1383
     * Render audio tag
1384
     *
1385
     * @access public
1386
     *
1387
     * @param array $sources format type=>src
1388
     * @param array $tracks format array(kind, src, srclang, label)
1389
     * @param array $attributes attributes tag
1390
     * @param string $noCodec text
1391
     *
1392
     * @return string
1393
     * @static
1394
     */
1395 View Code Duplication
    public static function audio(array $sources = [], array $tracks = [], array $attributes = [], $noCodec = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1396
    {
1397
        $srcs = '';
1398
        foreach ($sources AS $name => $value) {
1399
            $srcs .= self::tag('audio', ['type' => $name, 'src' => $value]);
1400
        }
1401
        foreach ($tracks AS $track) {
1402
            $srcs .= self::tag('track', [
1403
                'kind' => $track['kind'],
1404
                'src' => $track['src'],
1405
                'srclang' => $track['srclang'],
1406
                'label' => $track['label']
1407
            ]);
1408
        }
1409
1410
        return self::openTag('audio', $attributes) . $srcs . $noCodec . self::closeTag('audio');
1411
    }
1412
1413
    /**
1414
     * Render canvas tag
1415
     *
1416
     * @access public
1417
     *
1418
     * @param array $attributes attributes tag
1419
     * @param string $noCodec text
1420
     *
1421
     * @return string
1422
     * @static
1423
     */
1424
    public static function canvas(array $attributes = [], $noCodec = '')
1425
    {
1426
        return self::openTag('canvas', $attributes) . $noCodec . self::closeTag('canvas');
1427
    }
1428
}
1429