1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace HexMakina\Marker; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @method static string hidden(string $name, $value=null) |
8
|
|
|
* @method static string date() |
9
|
|
|
* @method static string time() |
10
|
|
|
* @method static string datetime() |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
class Form |
14
|
|
|
{ |
15
|
|
|
public static function __callStatic(string $element_type, array $arguments): string |
16
|
|
|
{ |
17
|
|
|
// arguments [name, value, [attributes], [errors]] |
18
|
|
|
$i = 0; |
19
|
|
|
$name = $arguments[$i++] ?? null; |
20
|
|
|
$value = $arguments[$i++] ?? null; |
21
|
|
|
$attributes = (array)($arguments[$i++] ?? []); |
22
|
|
|
$errors = (array)($arguments[$i++] ?? []); |
23
|
|
|
|
24
|
|
|
$attributes['type'] = $element_type; |
25
|
|
|
$attributes['name'] ??= $name; |
26
|
|
|
$attributes['value'] ??= $value; |
27
|
|
|
|
28
|
|
|
if ($attributes['type'] == 'datetime') { |
29
|
|
|
$attributes['type'] = 'datetime-local'; |
30
|
|
|
} elseif ($attributes['type'] == 'password') { |
31
|
|
|
$attributes['value'] = ''; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return self::input($name, $value, $attributes, $errors); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function input(string $name, mixed $value = null, array $attributes = [], array $errors = []): string |
38
|
|
|
{ |
39
|
|
|
$attributes['name'] ??= $name; |
40
|
|
|
$attributes['value'] ??= $value; |
41
|
|
|
|
42
|
|
|
if ( |
43
|
|
|
!isset($attributes['type']) |
44
|
|
|
|| isset($attributes['disabled']) |
45
|
|
|
|| in_array('disabled', $attributes, true) |
46
|
|
|
) { |
47
|
|
|
// why are disabled field textual ? |
48
|
|
|
// radio or checkbox can be disabled too.. |
49
|
|
|
$attributes['type'] = 'text'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return self::elementWithErrors('input', null, $attributes, isset($errors[$name])); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function textarea(string $name, mixed $value = null, array $attributes = [], array $errors = []): string |
56
|
|
|
{ |
57
|
|
|
$attributes['name'] ??= $name; |
58
|
|
|
return self::elementWithErrors('textarea', $value, $attributes, isset($errors[$name])); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public static function select(string $name, array $option_list, mixed $selected = null, array $attributes = [], array $errors = []): string |
62
|
|
|
{ |
63
|
|
|
$attributes['name'] ??= $name; |
64
|
|
|
$options = self::options($option_list, $selected); |
65
|
|
|
return self::elementWithErrors('select', $options, $attributes, isset($errors[$name])); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function options(array $list, mixed $selected = null): string |
69
|
|
|
{ |
70
|
|
|
$options = ''; |
71
|
|
|
foreach ($list as $value => $label) { |
72
|
|
|
$option_attributes = ['value' => $value]; |
73
|
|
|
if ($selected == $value) { |
74
|
|
|
$option_attributes['selected'] = 'selected'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$options .= new Element('option', $label, $option_attributes); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $options; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public static function legend(string $label, array $attributes = []): string |
84
|
|
|
{ |
85
|
|
|
return '' . (new Element('legend', $label, $attributes)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public static function label(string $for, string $label, array $attributes = [], array $errors = []): string |
89
|
|
|
{ |
90
|
|
|
$attributes['for'] = $for; |
91
|
|
|
unset($attributes['label']); |
92
|
|
|
|
93
|
|
|
return self::elementWithErrors('label', $label, $attributes, isset($errors[$for])); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
public static function submit(string $id, string $label, array $attributes = []): string |
98
|
|
|
{ |
99
|
|
|
$ret = ''; |
100
|
|
|
|
101
|
|
|
$attributes['type'] = 'submit'; |
102
|
|
|
unset($attributes['name']); |
103
|
|
|
|
104
|
|
|
$attributes['id'] ??= $id; |
105
|
|
|
$attributes['value'] ??= $label; |
106
|
|
|
|
107
|
|
|
if (isset($attributes['tag']) && $attributes['tag'] === 'input') { |
108
|
|
|
unset($attributes['tag']); |
109
|
|
|
$ret .= new Element('input', '', $attributes); |
110
|
|
|
} else { |
111
|
|
|
unset($attributes['tag']); |
112
|
|
|
unset($attributes['value']); |
113
|
|
|
$ret .= new Element('button', $label, $attributes); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $ret; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private static function elementWithErrors(string $tag, string $content = null, array $attributes = [], bool $hasErrors = false): string |
120
|
|
|
{ |
121
|
|
|
$attributes['id'] ??= $attributes['name'] ?? ''; |
122
|
|
|
|
123
|
|
|
if ($hasErrors) { |
124
|
|
|
$attributes['class'] ??= ''; |
125
|
|
|
$attributes['class'] .= ' error'; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$label = ''; |
129
|
|
|
if (isset($attributes['label'])) { |
130
|
|
|
$label = self::label($attributes['id'], $attributes['label'], [], ['' . $attributes['id'] => 'error']); |
131
|
|
|
unset($attributes['label']); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $label . (new Element($tag, $content, $attributes)); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|