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