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