Skip to content

Commit 6179169

Browse files
committed
Add label, input, select and form tags.
1 parent 065dcd2 commit 6179169

File tree

3 files changed

+302
-2
lines changed

3 files changed

+302
-2
lines changed

examples/example.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
->add2Content(Element::createBreak())
3939
->add2Content(Element::createBreak())
4040
->add2Content(Element::createCode('print ("Hello. World")'))
41-
->add2Content(Element::createI('<p>I\'m alternate voice</p>'))
41+
->add2Content(Element::createI('I\'m alternate voice'))
4242
->add2Content(Element::createImg('../images/code.jpg', 'Alternative text'))
4343
->add2Content(
4444
Element::createList(
@@ -88,6 +88,44 @@
8888
], ['table'], 'table1'
8989
)
9090
)
91+
->add2Content(
92+
Element::createForm(
93+
Element::concatenateElements(
94+
Element::createLabel('Username', 'username'),
95+
Element::createInput(
96+
Element::INPUT_TEXT, 'username', null,
97+
'Enter your username', [], 'username'
98+
),
99+
Element::createBreak(),
100+
Element::createLabel('Password', 'pass'),
101+
Element::createInput(
102+
Element::INPUT_PASSWORD, 'password', null,
103+
'Enter your password', [], 'pass'
104+
),
105+
Element::createBreak(),
106+
Element::createLabel('remember me', 'remember'),
107+
Element::createInput(
108+
Element::INPUT_CHECKBOX, 'remember', null,
109+
null, [], 'remember'
110+
),
111+
Element::createBreak(),
112+
Element::createBreak(),
113+
Element::createInput(Element::INPUT_RESET, null, 'Reset'),
114+
Element::createInput(Element::INPUT_SUBMIT, null, 'Submit'),
115+
),
116+
'/bootstrap.php'
117+
)
118+
)
119+
->add2Content(
120+
Element::createSelect(
121+
[
122+
'car1' => 'Mercedes',
123+
'car2' => 'Audi',
124+
'car3' => 'BMW',
125+
'car4' => 'Tesla',
126+
], 'car'
127+
)
128+
)
91129
->add2Content(Element::createFooter('<p>I\'m Paragraph in Footer</p>'));
92130

93131
$document

src/HTMLGenerator/Element.php

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,37 @@ class Element
3838
const LIST_ORDERED = 'ol';
3939
const LIST_UNORDERED = 'ul';
4040

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+
4172
/**
4273
* HTML Content.
4374
*
@@ -373,6 +404,97 @@ public static function createEm(
373404
return $return;
374405
}
375406

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+
376498
/**
377499
* Create footer tag.
378500
*
@@ -551,6 +673,102 @@ public static function createImg(
551673
return $template;
552674
}
553675

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+
554772
/**
555773
* Create ol or ul elements.
556774
*
@@ -740,6 +958,47 @@ public static function createSection(
740958
return $return;
741959
}
742960

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+
7431002
/**
7441003
* Create span tag.
7451004
*

src/HTMLGenerator/Exception/InvalidDocumentException.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ class InvalidDocumentException extends \Exception
3939
* 4: Invalid Charset,
4040
* 5: Invalid link target,
4141
* 6: Invalid heading size,
42-
* 7: Invalid list type.
42+
* 7: Invalid list type,
43+
* 8: Invalid form enctype,
44+
* 9: Invalid form method,
45+
* 10: Invalid input type.
4346
* @param \Exception $previous [optional] The previous throwable
4447
* used for the exception chaining.
4548
*/

0 commit comments

Comments
 (0)