@@ -38,6 +38,37 @@ class Element
38
38
const LIST_ORDERED = 'ol ' ;
39
39
const LIST_UNORDERED = 'ul ' ;
40
40
41
+ // Define form methods.
42
+ const FORM_METHOD_GET = 'get ' ;
43
+ const FORM_METHOD_POST = 'post ' ;
44
+
45
+ // Define form enctypes.
46
+ const FORM_TYPE_URLENCODED = 'application/x-www-form-urlencoded ' ;
47
+ const FORM_TYPE_FORM_DATA = 'multipart/form-data ' ;
48
+ const FORM_TYPE_TEXT = 'text/plain ' ;
49
+
50
+ // Define input types.
51
+ const INPUT_BUTTON = 'button ' ;
52
+ const INPUT_CHECKBOX = 'checkbox ' ;
53
+ const INPUT_COLOR = 'color ' ;
54
+ const INPUT_DATE = 'date ' ;
55
+ const INPUT_EMAIL = 'email ' ;
56
+ const INPUT_FILE = 'file ' ;
57
+ const INPUT_HIDDEN = 'hidden ' ;
58
+ const INPUT_MONTH = 'month ' ;
59
+ const INPUT_NUMBER = 'number ' ;
60
+ const INPUT_PASSWORD = 'password ' ;
61
+ const INPUT_RADIO = 'radio ' ;
62
+ const INPUT_RANGE = 'range ' ;
63
+ const INPUT_RESET = 'reset ' ;
64
+ const INPUT_SEARCH = 'search ' ;
65
+ const INPUT_SUBMIT = 'submit ' ;
66
+ const INPUT_TEL = 'tel ' ;
67
+ const INPUT_TEXT = 'text ' ;
68
+ const INPUT_TIME = 'time ' ;
69
+ const INPUT_URL = 'url ' ;
70
+ const INPUT_WEEK = 'week ' ;
71
+
41
72
/**
42
73
* HTML Content.
43
74
*
@@ -373,6 +404,97 @@ public static function createEm(
373
404
return $ return ;
374
405
}
375
406
407
+ /**
408
+ * Create form tag.
409
+ *
410
+ * @param string $content Form content.
411
+ * @param string $action Form action.
412
+ * @param string|null $method Form HTTP method.
413
+ * @param string|null $enctype Form type
414
+ * @param string $target Form target.
415
+ * @param string|null $name Form name.
416
+ * @param array $classes HTML Classes.
417
+ * @param string|null $id Element ID.
418
+ *
419
+ * @return string
420
+ *
421
+ * @throws \ReflectionException
422
+ * @throws InvalidDocumentException
423
+ */
424
+ public static function createForm (
425
+ string $ content ,
426
+ string $ action ,
427
+ string $ method = self ::FORM_METHOD_GET ,
428
+ string $ enctype = self ::FORM_TYPE_TEXT ,
429
+ string $ target = self ::TARGET_SELF ,
430
+ string $ name = null ,
431
+ array $ classes = [],
432
+ string $ id = null
433
+ ): string {
434
+ $ template = <<<EOF
435
+ <form{action_area}{method_area}{type_area}{name_area}{target_area}{classes_area}{id_area}>
436
+ {content}
437
+ </form> \n\t
438
+ EOF ;
439
+ // Validation.
440
+
441
+ $ validTargets = array_filter (
442
+ self ::_getConstants (), function ($ key ) {
443
+ return strpos ($ key , 'TARGET_ ' ) === 0 ;
444
+ }, ARRAY_FILTER_USE_KEY
445
+ );
446
+
447
+ $ validFormTypes = array_filter (
448
+ self ::_getConstants (), function ($ key ) {
449
+ return strpos ($ key , 'FORM_TYPE_ ' ) === 0 ;
450
+ }, ARRAY_FILTER_USE_KEY
451
+ );
452
+
453
+ $ validFormMethods = array_filter (
454
+ self ::_getConstants (), function ($ key ) {
455
+ return strpos ($ key , 'FORM_METHOD_ ' ) === 0 ;
456
+ }, ARRAY_FILTER_USE_KEY
457
+ );
458
+
459
+ // Check if given target is valid.
460
+ if (!in_array ($ target , array_values ($ validTargets ))) {
461
+ throw new InvalidDocumentException (
462
+ 'Target " ' . $ target . '" is invalid! ' , 5
463
+ );
464
+ }
465
+
466
+ // Check if given enctype is valid.
467
+ if (!in_array ($ enctype , array_values ($ validFormTypes ))) {
468
+ throw new InvalidDocumentException (
469
+ 'Enctype " ' . $ enctype . '" is invalid! ' , 8
470
+ );
471
+ }
472
+
473
+ // Check if given method is valid.
474
+ if (!in_array ($ method , array_values ($ validFormMethods ))) {
475
+ throw new InvalidDocumentException (
476
+ 'Form method " ' . $ method . '" is invalid! ' , 9
477
+ );
478
+ }
479
+
480
+ $ return = self ::_createBaseFromTemplate (
481
+ $ template , $ content , $ classes , $ id
482
+ );
483
+
484
+ $ nameArea = null ;
485
+ if (null !== $ name ) {
486
+ $ nameArea = " name= \"{$ name }\"" ;
487
+ }
488
+
489
+ $ return = str_replace ('{action_area} ' , " action= \"{$ action }\"" , $ return );
490
+ $ return = str_replace ('{method_area} ' , " method= \"{$ method }\"" , $ return );
491
+ $ return = str_replace ('{type_area} ' , " enctype= \"{$ enctype }\"" , $ return );
492
+ $ return = str_replace ('{name_area} ' , $ nameArea , $ return );
493
+ $ return = str_replace ('{target_area} ' , " target= \"{$ target }\"" , $ return );
494
+
495
+ return $ return ;
496
+ }
497
+
376
498
/**
377
499
* Create footer tag.
378
500
*
@@ -551,6 +673,102 @@ public static function createImg(
551
673
return $ template ;
552
674
}
553
675
676
+ /**
677
+ * Create input tag.
678
+ *
679
+ * @param string $type Input type.
680
+ * @param string|null $name Input name.
681
+ * @param string|null $value Input value
682
+ * @param string|null $placeholder Input placeholder.
683
+ * @param array $classes HTML Classes.
684
+ * @param string|null $id Element ID.
685
+ *
686
+ * @return string
687
+ *
688
+ * @throws InvalidDocumentException
689
+ * @throws \ReflectionException
690
+ */
691
+ public static function createInput (
692
+ string $ type = self ::INPUT_TEXT ,
693
+ string $ name = null ,
694
+ string $ value = null ,
695
+ string $ placeholder = null ,
696
+ array $ classes = [],
697
+ string $ id = null
698
+ ): string {
699
+
700
+ // Validation.
701
+ $ validTypes = array_filter (
702
+ self ::_getConstants (), function ($ key ) {
703
+ return strpos ($ key , 'INPUT_ ' ) === 0 ;
704
+ }, ARRAY_FILTER_USE_KEY
705
+ );
706
+
707
+ // Check if given type is valid.
708
+ if (!in_array ($ type , array_values ($ validTypes ))) {
709
+ throw new InvalidDocumentException (
710
+ 'Input type " ' . $ type . '" is invalid! ' , 10
711
+ );
712
+ }
713
+
714
+ $ template = "<input type= \"{content} \"{name_area}{value_area} " .
715
+ "{placeholder_area}{classes_area}{id_area}> \n\t" ;
716
+
717
+ $ return = self ::_createBaseFromTemplate (
718
+ $ template , $ type , $ classes , $ id
719
+ );
720
+
721
+ $ nameArea = null ;
722
+ $ valueArea = null ;
723
+ $ placeholderArea = null ;
724
+
725
+ if (null !== $ name ) {
726
+ $ nameArea = " name= \"{$ name }\"" ;
727
+ }
728
+ if (null !== $ value ) {
729
+ $ valueArea = " value= \"{$ value }\"" ;
730
+ }
731
+ if (null !== $ placeholder ) {
732
+ $ placeholderArea = " placeholder= \"{$ placeholder }\"" ;
733
+ }
734
+
735
+ $ return = str_replace ('{name_area} ' , $ nameArea , $ return );
736
+ $ return = str_replace ('{value_area} ' , $ valueArea , $ return );
737
+ $ return = str_replace (
738
+ '{placeholder_area} ' , $ placeholderArea , $ return
739
+ );
740
+
741
+ return $ return ;
742
+ }
743
+
744
+ /**
745
+ * Create label tag.
746
+ *
747
+ * @param string $content I content.
748
+ * @param string $for Related element ID.
749
+ * @param array $classes HTML Classes.
750
+ * @param string|null $id Element ID.
751
+ *
752
+ * @return string
753
+ */
754
+ public static function createLabel (
755
+ string $ content ,
756
+ string $ for ,
757
+ array $ classes = [],
758
+ string $ id = null
759
+ ): string {
760
+ $ template = "<label for= \"{for} \"{classes_area}{id_area}> " .
761
+ "{content}</label> \n\t" ;
762
+
763
+ $ return = self ::_createBaseFromTemplate (
764
+ $ template , $ content , $ classes , $ id
765
+ );
766
+
767
+ $ return = str_replace ('{for} ' , $ for , $ return );
768
+
769
+ return $ return ;
770
+ }
771
+
554
772
/**
555
773
* Create ol or ul elements.
556
774
*
@@ -740,6 +958,47 @@ public static function createSection(
740
958
return $ return ;
741
959
}
742
960
961
+ /**
962
+ * Create select tag.
963
+ *
964
+ * @param array $data Options data.
965
+ * @param string|null $name Select name.
966
+ * @param array $classes HTML Classes.
967
+ * @param string|null $id Element ID.
968
+ *
969
+ * @return string
970
+ */
971
+ public static function createSelect (
972
+ array $ data ,
973
+ string $ name = null ,
974
+ array $ classes = [],
975
+ string $ id = null
976
+ ): string {
977
+ $ template = <<<EOF
978
+ <select{name_area}{classes_area}{id_area}>
979
+ {content}
980
+ </select> \n\t
981
+ EOF ;
982
+
983
+ $ content = '' ;
984
+ foreach ($ data as $ k => $ v ) {
985
+ $ content .= "<option value= \"{$ k }\"> {$ v }</option> \n\t\t" ;
986
+ }
987
+
988
+ $ return = self ::_createBaseFromTemplate (
989
+ $ template , $ content , $ classes , $ id
990
+ );
991
+
992
+ $ nameArea = null ;
993
+ if (null !== $ name ) {
994
+ $ nameArea = " name= \"{$ name }\"" ;
995
+ }
996
+
997
+ $ return = str_replace ('{name_area} ' , $ nameArea , $ return );
998
+
999
+ return $ return ;
1000
+ }
1001
+
743
1002
/**
744
1003
* Create span tag.
745
1004
*
0 commit comments