|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Form; |
|
6
|
|
|
|
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use Yiisoft\Form\Field\Button; |
|
9
|
|
|
use Yiisoft\Form\Field\ButtonGroup; |
|
10
|
|
|
use Yiisoft\Form\Field\Checkbox; |
|
11
|
|
|
use Yiisoft\Form\Field\CheckboxList; |
|
12
|
|
|
use Yiisoft\Form\Field\Date; |
|
13
|
|
|
use Yiisoft\Form\Field\DateTime; |
|
14
|
|
|
use Yiisoft\Form\Field\DateTimeLocal; |
|
15
|
|
|
use Yiisoft\Form\Field\Email; |
|
16
|
|
|
use Yiisoft\Form\Field\ErrorSummary; |
|
17
|
|
|
use Yiisoft\Form\Field\Fieldset; |
|
18
|
|
|
use Yiisoft\Form\Field\Hidden; |
|
19
|
|
|
use Yiisoft\Form\Field\Image; |
|
20
|
|
|
use Yiisoft\Form\Field\Number; |
|
21
|
|
|
use Yiisoft\Form\Field\Part\Error; |
|
22
|
|
|
use Yiisoft\Form\Field\Part\Hint; |
|
23
|
|
|
use Yiisoft\Form\Field\Part\Label; |
|
24
|
|
|
use Yiisoft\Form\Field\Password; |
|
25
|
|
|
use Yiisoft\Form\Field\RadioList; |
|
26
|
|
|
use Yiisoft\Form\Field\Range; |
|
27
|
|
|
use Yiisoft\Form\Field\ResetButton; |
|
28
|
|
|
use Yiisoft\Form\Field\Select; |
|
29
|
|
|
use Yiisoft\Form\Field\SubmitButton; |
|
30
|
|
|
use Yiisoft\Form\Field\Telephone; |
|
31
|
|
|
use Yiisoft\Form\Field\Text; |
|
32
|
|
|
use Yiisoft\Form\Field\Textarea; |
|
33
|
|
|
use Yiisoft\Form\Field\Url; |
|
34
|
|
|
|
|
35
|
|
|
use function array_key_exists; |
|
36
|
|
|
|
|
37
|
|
|
final class Field |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* @psalm-var array<string,array> |
|
41
|
|
|
*/ |
|
42
|
|
|
private static array $configs = [ |
|
43
|
|
|
'default' => [], |
|
44
|
|
|
]; |
|
45
|
|
|
|
|
46
|
|
|
private static string $defaultConfigName = 'default'; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @psalm-var array<string,FieldFactory> |
|
50
|
|
|
*/ |
|
51
|
|
|
private static array $factories = []; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @psalm-param array<string,array> $configs |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function initialize(array $configs = [], string $defaultConfigName = 'default'): void |
|
57
|
|
|
{ |
|
58
|
|
|
self::$configs = array_merge(self::$configs, $configs); |
|
59
|
|
|
self::$defaultConfigName = $defaultConfigName; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
public static function button(array $config = []): Button |
|
63
|
|
|
{ |
|
64
|
1 |
|
return self::getFactory()->button($config); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
public static function buttonGroup(array $config = []): ButtonGroup |
|
68
|
|
|
{ |
|
69
|
1 |
|
return self::getFactory()->buttonGroup($config); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
public static function checkbox(FormModelInterface $formModel, string $attribute, array $config = []): Checkbox |
|
73
|
|
|
{ |
|
74
|
1 |
|
return self::getFactory()->checkbox($formModel, $attribute, $config); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
public static function checkboxList( |
|
78
|
|
|
FormModelInterface $formModel, |
|
79
|
|
|
string $attribute, |
|
80
|
|
|
array $config = [] |
|
81
|
|
|
): CheckboxList { |
|
82
|
1 |
|
return self::getFactory()->checkboxList($formModel, $attribute, $config); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
public static function date(FormModelInterface $formModel, string $attribute, array $config = []): Date |
|
86
|
|
|
{ |
|
87
|
1 |
|
return self::getFactory()->date($formModel, $attribute, $config); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
public static function dateTime(FormModelInterface $formModel, string $attribute, array $config = []): DateTime |
|
91
|
|
|
{ |
|
92
|
1 |
|
return self::getFactory()->dateTime($formModel, $attribute, $config); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
public static function dateTimeLocal( |
|
96
|
|
|
FormModelInterface $formModel, |
|
97
|
|
|
string $attribute, |
|
98
|
|
|
array $config = [] |
|
99
|
|
|
): DateTimeLocal { |
|
100
|
1 |
|
return self::getFactory()->dateTimeLocal($formModel, $attribute, $config); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
public static function email(FormModelInterface $formModel, string $attribute, array $config = []): Email |
|
104
|
|
|
{ |
|
105
|
1 |
|
return self::getFactory()->email($formModel, $attribute, $config); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
public static function errorSummary(FormModelInterface $formModel, array $config = []): ErrorSummary |
|
109
|
|
|
{ |
|
110
|
1 |
|
return self::getFactory()->errorSummary($formModel, $config); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
1 |
|
public static function fieldset(array $config = []): Fieldset |
|
114
|
|
|
{ |
|
115
|
1 |
|
return self::getFactory()->fieldset($config); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
public static function hidden(FormModelInterface $formModel, string $attribute, array $config = []): Hidden |
|
119
|
|
|
{ |
|
120
|
1 |
|
return self::getFactory()->hidden($formModel, $attribute, $config); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
public static function image(array $config = []): Image |
|
124
|
|
|
{ |
|
125
|
1 |
|
return self::getFactory()->image($config); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
1 |
|
public static function number(FormModelInterface $formModel, string $attribute, array $config = []): Number |
|
129
|
|
|
{ |
|
130
|
1 |
|
return self::getFactory()->number($formModel, $attribute, $config); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
1 |
|
public static function password(FormModelInterface $formModel, string $attribute, array $config = []): Password |
|
134
|
|
|
{ |
|
135
|
1 |
|
return self::getFactory()->password($formModel, $attribute, $config); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
1 |
|
public static function radioList(FormModelInterface $formModel, string $attribute, array $config = []): RadioList |
|
139
|
|
|
{ |
|
140
|
1 |
|
return self::getFactory()->radioList($formModel, $attribute, $config); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
1 |
|
public static function range(FormModelInterface $formModel, string $attribute, array $config = []): Range |
|
144
|
|
|
{ |
|
145
|
1 |
|
return self::getFactory()->range($formModel, $attribute, $config); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
1 |
|
public static function resetButton(array $config = []): ResetButton |
|
149
|
|
|
{ |
|
150
|
1 |
|
return self::getFactory()->resetButton($config); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
1 |
|
public static function select(FormModelInterface $formModel, string $attribute, array $config = []): Select |
|
154
|
|
|
{ |
|
155
|
1 |
|
return self::getFactory()->select($formModel, $attribute, $config); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
1 |
|
public static function submitButton(array $config = []): SubmitButton |
|
159
|
|
|
{ |
|
160
|
1 |
|
return self::getFactory()->submitButton($config); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
1 |
|
public static function telephone(FormModelInterface $formModel, string $attribute, array $config = []): Telephone |
|
164
|
|
|
{ |
|
165
|
1 |
|
return self::getFactory()->telephone($formModel, $attribute, $config); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
3 |
|
public static function text(FormModelInterface $formModel, string $attribute, array $config = []): Text |
|
169
|
|
|
{ |
|
170
|
3 |
|
return self::getFactory()->text($formModel, $attribute, $config); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
1 |
|
public static function textarea(FormModelInterface $formModel, string $attribute, array $config = []): Textarea |
|
174
|
|
|
{ |
|
175
|
1 |
|
return self::getFactory()->textarea($formModel, $attribute, $config); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
1 |
|
public static function url(FormModelInterface $formModel, string $attribute, array $config = []): Url |
|
179
|
|
|
{ |
|
180
|
1 |
|
return self::getFactory()->url($formModel, $attribute, $config); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
1 |
|
public static function label(FormModelInterface $formModel, string $attribute, array $config = []): Label |
|
184
|
|
|
{ |
|
185
|
1 |
|
return self::getFactory()->label($formModel, $attribute, $config); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
1 |
|
public static function hint(FormModelInterface $formModel, string $attribute, array $config = []): Hint |
|
189
|
|
|
{ |
|
190
|
1 |
|
return self::getFactory()->hint($formModel, $attribute, $config); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
1 |
|
public static function error(FormModelInterface $formModel, string $attribute, array $config = []): Error |
|
194
|
|
|
{ |
|
195
|
1 |
|
return self::getFactory()->error($formModel, $attribute, $config); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @psalm-template T |
|
200
|
|
|
* @psalm-param class-string<T> $class |
|
201
|
|
|
* @psalm-return T |
|
202
|
|
|
*/ |
|
203
|
|
|
public function input(string $class, FormModelInterface $formModel, string $attribute, array $config = []): object |
|
204
|
|
|
{ |
|
205
|
|
|
return self::getFactory()->input($class, $formModel, $attribute, $config); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @psalm-template T |
|
210
|
|
|
* @psalm-param class-string<T> $class |
|
211
|
|
|
* @psalm-return T |
|
212
|
|
|
*/ |
|
213
|
|
|
public function field(string $class, array $config = []): object |
|
214
|
|
|
{ |
|
215
|
|
|
return self::getFactory()->field($class, $config); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
28 |
|
public static function getFactory(?string $name = null): FieldFactory |
|
219
|
|
|
{ |
|
220
|
28 |
|
$name = $name ?? self::$defaultConfigName; |
|
221
|
|
|
|
|
222
|
28 |
|
if (!array_key_exists($name, self::$factories)) { |
|
223
|
1 |
|
if (!array_key_exists($name, self::$configs)) { |
|
224
|
|
|
throw new RuntimeException( |
|
225
|
|
|
sprintf('Configuration with name "%s" not found.', $name) |
|
226
|
|
|
); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** @psalm-suppress MixedArgument */ |
|
230
|
1 |
|
self::$factories[$name] = new FieldFactory(...self::$configs[$name]); |
|
|
|
|
|
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
28 |
|
return self::$factories[$name]; |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|