1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace HexMakina\Marker; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Provides a convenient way to generate form input elements with minimal code, |
9
|
|
|
* making it easier and faster to build forms in PHP. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
class Form |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Used as a shortcut for creating form input elements with a single line of code |
17
|
|
|
* |
18
|
|
|
* When called on the Form class with a name that matches an HTML input element type (such as hidden, date, time, radio, datetime, etc.), |
19
|
|
|
* it intercepts the call and dynamically creates an input element of that type with the provided attributes. |
20
|
|
|
*/ |
21
|
|
|
public static function __callStatic(string $element_type, array $arguments): string |
22
|
|
|
{ |
23
|
|
|
// arguments [name, value, [attributes]] |
24
|
|
|
$i = 0; |
25
|
|
|
$name = $arguments[$i++] ?? null; |
26
|
|
|
$value = $arguments[$i++] ?? null; |
27
|
|
|
$attributes = (array)($arguments[$i++] ?? []); |
28
|
|
|
|
29
|
|
|
$attributes['type'] = $element_type; |
30
|
|
|
$attributes['name'] ??= $name; |
31
|
|
|
$attributes['value'] ??= $value; |
32
|
|
|
|
33
|
|
|
if ($attributes['type'] == 'datetime') { |
34
|
|
|
$attributes['type'] = 'datetime-local'; |
35
|
|
|
} elseif ($attributes['type'] == 'password') { |
36
|
|
|
$attributes['value'] = ''; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return self::input($name, $value, $attributes); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Generates an HTML input element with the provided attributes. |
44
|
|
|
* |
45
|
|
|
* @param string|null $name The name of the input element |
46
|
|
|
* @param mixed|null $value The value of the input element |
47
|
|
|
* @param array $attributes An array containing the attributes of the input element |
48
|
|
|
* @return string The generated HTML code for the input element |
49
|
|
|
*/ |
50
|
|
|
public static function input(string $name = null, $value = null, array $attributes = []): string |
51
|
|
|
{ |
52
|
|
|
$attributes['name'] ??= $name; |
53
|
|
|
$attributes['value'] ??= $value; |
54
|
|
|
|
55
|
|
|
if ( |
56
|
|
|
!isset($attributes['type']) |
57
|
|
|
|| isset($attributes['disabled']) |
58
|
|
|
|| in_array('disabled', $attributes, true) |
59
|
|
|
) { |
60
|
|
|
// why are disabled field textual ? |
61
|
|
|
// radio or checkbox can be disabled too.. |
62
|
|
|
$attributes['type'] = 'text'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return self::labelledField('input', null, $attributes); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Generates an HTML textarea element with the provided attributes. |
70
|
|
|
* |
71
|
|
|
* @param string $name The name of the textarea element |
72
|
|
|
* @param mixed|null $value The value of the textarea element |
73
|
|
|
* @param array $attributes An array containing the attributes of the textarea element |
74
|
|
|
* @return string The generated HTML code for the textarea element |
75
|
|
|
*/ |
76
|
|
|
public static function textarea(string $name, $value = null, array $attributes = []): string |
77
|
|
|
{ |
78
|
|
|
$attributes['name'] ??= $name; |
79
|
|
|
return self::labelledField('textarea', $value, $attributes); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Generates an HTML select element with the provided options and attributes. |
84
|
|
|
* |
85
|
|
|
* @param string $name The name of the select element |
86
|
|
|
* @param array $option_list An array containing the options of the select element |
87
|
|
|
* @param mixed|null $selected The selected option of the select element |
88
|
|
|
* @param array $attributes An array containing the attributes of the select element |
89
|
|
|
* @return string The generated HTML code for the select element |
90
|
|
|
*/ |
91
|
|
|
public static function select(string $name, array $option_list, $selected = null, array $attributes = []): string |
92
|
|
|
{ |
93
|
|
|
$attributes['name'] ??= $name; |
94
|
|
|
$options = self::options($option_list, $selected); |
95
|
|
|
return self::labelledField('select', $options, $attributes); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Generates an HTML options string for the provided option list and selected value. |
100
|
|
|
* |
101
|
|
|
* @param array $list An array containing the options of the select element |
102
|
|
|
* @param mixed|null $selected The selected option of the select element |
103
|
|
|
* @return string The generated HTML options string |
104
|
|
|
*/ |
105
|
|
|
public static function options(array $list, $selected = null): string |
106
|
|
|
{ |
107
|
|
|
$options = ''; |
108
|
|
|
foreach ($list as $value => $label) { |
109
|
|
|
$option_attributes = ['value' => $value]; |
110
|
|
|
if ($selected == $value) { |
111
|
|
|
$option_attributes['selected'] = 'selected'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$options .= new Element('option', "$label", $option_attributes); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $options; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Generates an HTML legend element with the provided label and attributes. |
122
|
|
|
* |
123
|
|
|
* @param string $label The label of the legend element |
124
|
|
|
* @param array $attributes An array containing the attributes of the legend element |
125
|
|
|
* @return string The generated HTML code for the legend element |
126
|
|
|
*/ |
127
|
|
|
public static function legend(string $label, array $attributes = []): string |
128
|
|
|
{ |
129
|
|
|
return '' . (new Element('legend', $label, $attributes)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Generates an HTML label element with the provided for attribute, label text, and attributes. |
134
|
|
|
* |
135
|
|
|
* @param string $for The ID of the input element associated with the label |
136
|
|
|
* @param string $label The text of the label element |
137
|
|
|
* @param array $attributes An array containing the attributes of the label element |
138
|
|
|
* @return string The generated HTML code for the label element |
139
|
|
|
*/ |
140
|
|
|
public static function label(string $for, string $label, array $attributes = []): string |
141
|
|
|
{ |
142
|
|
|
$attributes['for'] = $for; |
143
|
|
|
unset($attributes['label']); |
144
|
|
|
|
145
|
|
|
return self::labelledField('label', $label, $attributes); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public static function button(string $label, array $attributes = []): string |
149
|
|
|
{ |
150
|
|
|
return '' . (new Element('button', $label, $attributes)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Generates a submit button element for a form. |
155
|
|
|
* |
156
|
|
|
* @param string $id The ID of the button element. |
157
|
|
|
* @param string $label The label for the button element. |
158
|
|
|
* @param array $attributes An optional array of attributes for the button element. |
159
|
|
|
* The 'type' attribute is set to 'submit' by default and the 'name' attribute is omitted. |
160
|
|
|
* The 'id' and 'value' attributes are automatically set based on the provided arguments. |
161
|
|
|
* If the 'tag' attribute is set to 'input', an input element will be generated instead of a button element. |
162
|
|
|
* @return string The generated HTML for the button element. |
163
|
|
|
*/ |
164
|
|
|
public static function submit(string $id, string $label, array $attributes = []): string |
165
|
|
|
{ |
166
|
|
|
$ret = ''; |
167
|
|
|
|
168
|
|
|
$attributes['type'] = 'submit'; |
169
|
|
|
unset($attributes['name']); |
170
|
|
|
|
171
|
|
|
$attributes['id'] ??= $id; |
172
|
|
|
$attributes['value'] ??= $label; |
173
|
|
|
|
174
|
|
|
if (isset($attributes['tag']) && $attributes['tag'] === 'input') { |
175
|
|
|
unset($attributes['tag']); |
176
|
|
|
$ret .= new Element('input', '', $attributes); |
177
|
|
|
} else { |
178
|
|
|
unset($attributes['tag']); |
179
|
|
|
unset($attributes['value']); |
180
|
|
|
$ret .= self::button($label, $attributes); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $ret; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Generates a labelled HTML form field, combining a form field with a label element. |
188
|
|
|
* |
189
|
|
|
* @param string $tag The HTML tag of the form field element |
190
|
|
|
* @param string|null $content The content of the form field element |
191
|
|
|
* @param array $attributes Additional attributes to add to the form field element |
192
|
|
|
* @return string The HTML for the labelled form field |
193
|
|
|
*/ |
194
|
|
|
private static function labelledField(string $tag, string $content = null, array $attributes = []): string |
195
|
|
|
{ |
196
|
|
|
$attributes['id'] ??= $attributes['name'] ?? ''; |
197
|
|
|
|
198
|
|
|
$label = ''; |
199
|
|
|
if (isset($attributes['label'])) { |
200
|
|
|
$label = self::label($attributes['id'], $attributes['label']); |
201
|
|
|
unset($attributes['label']); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $label . (new Element($tag, $content, $attributes)); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|