-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm.php
More file actions
361 lines (316 loc) · 9.74 KB
/
Form.php
File metadata and controls
361 lines (316 loc) · 9.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/**
* @package Flextype Components
*
* @author Sergey Romanenko <awilum@yandex.ru>
* @link http://components.flextype.org
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flextype\Component\Form;
use Flextype\Component\Html\Html;
class Form
{
/**
* The registered custom macros.
*
* @var array
*/
public static $macros = [];
/**
* Registers a custom macro.
*
*
* // Registering a Form macro
* Form::macro('my_field', function() {
* return '<input type="text" name="my_field">';
* });
*
* // Calling a custom Form macro
* echo Form::my_field();
*
*
* // Registering a Form macro with parameters
* Form::macro('my_field', function($value = '') {
* return '<input type="text" name="my_field" value="'.$value.'">';
* });
*
* // Calling a custom Form macro with parameters
* echo Form::my_field('Flextype');
*
* @param string $name Name
* @param Closure $macro Macro
* @return void
*/
public static function macro(string $name, $macro) : void
{
Form::$macros[$name] = $macro;
}
/**
* Create an opening HTML form tag.
*
* // Form will submit back to the current page using POST
* echo Form::open();
*
* // Form will submit to 'search' using GET
* echo Form::open('search', array('method' => 'get'));
*
* // When "file" inputs are present, you must include the "enctype"
* echo Form::open(null, array('enctype' => 'multipart/form-data'));
*
* @param string $action Form action, defaults to the current request URI.
* @param array $attributes HTML attributes.
* @return string
*/
public static function open($action = '', array $attributes = null) : string
{
// Add the form action to the attributes
$attributes['action'] = $action;
if ( ! isset($attributes['method'])) {
// Use POST method
$attributes['method'] = 'post';
}
return '<form'.Html::attributes($attributes).'>';
}
/**
* Create a form input.
* Text is default input type.
*
* echo Form::input('username', $username);
*
* @param string $name Input name
* @param string $value Input value
* @param array $attributes HTML attributes
* @return string
*/
public static function input(string $name, string $value = '', array $attributes = null) : string
{
// Set the input name
$attributes['name'] = $name;
// Set the input id
$attributes['id'] = (isset($attributes['id'])) ? $attributes['id'] : $name;
// Set the input value
$attributes['value'] = $value;
if ( ! isset($attributes['type'])) {
// Default type is text
$attributes['type'] = 'text';
}
return '<input'.Html::attributes($attributes).'>';
}
/**
* Create a hidden form input.
*
* echo Form::hidden('user_id', $user_id);
*
* @param string $name Input name
* @param string $value Input value
* @param array $attributes HTML attributes
* @return string
*/
public static function hidden(string $name, string $value = '', array $attributes = null) : string
{
// Set the input type
$attributes['type'] = 'hidden';
return Form::input($name, $value, $attributes);
}
/**
* Creates a password form input.
*
* echo Form::password('password');
*
* @param string $name Input name
* @param string $value Input value
* @param array $attributes HTML attributes
* @return string
*/
public static function password(string $name, string $value = '', array $attributes = null) : string
{
// Set the input type
$attributes['type'] = 'password';
return Form::input($name, $value, $attributes);
}
/**
* Creates a file upload form input.
*
* echo Form::file('image');
*
* @param string $name Input name
* @param array $attributes HTML attributes
* @return string
*/
public static function file(string $name, array $attributes = null) : string
{
// Set the input type
$attributes['type'] = 'file';
return Form::input($name, '', $attributes);
}
/**
* Creates a checkbox form input.
*
* echo Form::checkbox('i_am_not_a_robot');
*
* @param string $name Input name
* @param string $input Input value
* @param boolean $checked Checked status
* @param array $attributes HTML attributes
* @uses Form::input
* @return string
*/
public static function checkbox($name, $value = '', $checked = false, array $attributes = null)
{
// Set the input type
$attributes['type'] = 'checkbox';
if ($checked === true) {
// Make the checkbox active
$attributes['checked'] = 'checked';
}
return Form::input($name, $value, $attributes);
}
/**
* Creates a radio form input.
*
* echo Form::radio('i_am_not_a_robot');
*
* @param string $name Input name
* @param string $value Input value
* @param boolean $checked Checked status
* @param array $attributes HTML attributes
* @uses Form::input
* @return string
*/
public static function radio($name, $value = '', $checked = null, array $attributes = null)
{
// Set the input type
$attributes['type'] = 'radio';
if ($checked === true) {
// Make the radio active
$attributes['checked'] = 'checked';
}
return Form::input($name, $value, $attributes);
}
/**
* Creates a textarea form input.
*
* echo Form::textarea('text', $text);
*
* @param string $name Name
* @param string $body Body
* @param array $attributes HTML attributes
* @uses Html::attributes
* @return string
*/
public static function textarea($name, $body = '', array $attributes = null)
{
// Set the input name
$attributes['name'] = $name;
// Set the input id
$attributes['id'] = (isset($attributes['id']))?$attributes['id']:$name;
return '<textarea'.Html::attributes($attributes).'>'.$body.'</textarea>';
}
/**
* Creates a select form input.
*
* <code>
* echo Form::select('themes', array('default', 'classic', 'modern'));
* </code>
*
* @param string $name Name
* @param array $options Options array
* @param string $selected Selected option
* @param array $attributes HTML attributes
* @uses Html::attributes
* @return string
*/
public static function select($name, array $options = null, $selected = null, array $attributes = null)
{
// Set the input name
$attributes['name'] = $name;
// Set the input id
$attributes['id'] = (isset($attributes['id']))?$attributes['id']:$name;
$options_output = '';
foreach ($options as $value => $name) {
if ($selected == $value) $current = ' selected '; else $current = '';
$options_output .= '<option value="'.$value.'" '.$current.'>'.$name.'</option>';
}
return '<select'.Html::attributes($attributes).'>'.$options_output.'</select>';
}
/**
* Creates a submit form input.
*
* <code>
* echo Form::submit('save', 'Save');
* </code>
*
* @param string $name Input name
* @param string $value Input value
* @param array $attributes HTML attributes
* @uses Form::input
* @return string
*/
public static function submit($name, $value, array $attributes = null)
{
// Set the input type
$attributes['type'] = 'submit';
return Form::input($name, $value, $attributes);
}
/**
* Creates a button form input.
*
* echo Form::button('save', 'Save Profile', array('type' => 'submit'));
*
* @param string $name Input name
* @param string $value Input value
* @param array $attributes HTML attributes
* @uses Html::attributes
* @return string
*/
public static function button($name, $body, array $attributes = null)
{
// Set the input name
$attributes['name'] = $name;
return '<button'.Html::attributes($attributes).'>'.$body.'</button>';
}
/**
* Creates a form label.
*
* echo Form::label('username', 'Username');
*
* @param string $input Target input
* @param string $text Label text
* @param array $attributes HTML attributes
* @uses Html::attributes
* @return string
*/
public static function label($input, $text, array $attributes = null)
{
// Set the label target
$attributes['for'] = $input;
return '<label'.Html::attributes($attributes).'>'.$text.'</label>';
}
/**
* Create closing form tag.
*
* echo Form::close();
*
* @return string
*/
public static function close()
{
return '</form>';
}
/**
* Dynamically handle calls to custom macros.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public static function __callStatic($method, $parameters)
{
if (isset(Form::$macros[$method])) {
return call_user_func_array(Form::$macros[$method], $parameters);
}
throw new RuntimeException("Method [$method] does not exist.");
}
}