-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomForm.php
More file actions
347 lines (295 loc) · 8.25 KB
/
CustomForm.php
File metadata and controls
347 lines (295 loc) · 8.25 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
<?php
namespace Bdf\Form\Custom;
use Bdf\Form\Aggregate\FormBuilder;
use Bdf\Form\Aggregate\FormBuilderInterface;
use Bdf\Form\Aggregate\FormInterface;
use Bdf\Form\Aggregate\View\FormView;
use Bdf\Form\Child\ChildInterface;
use Bdf\Form\Child\Http\HttpFieldPath;
use Bdf\Form\Csrf\CsrfValueValidator;
use Bdf\Form\ElementInterface;
use Bdf\Form\Error\FormError;
use Bdf\Form\RootElementInterface;
use Bdf\Form\View\ElementViewInterface;
use Iterator;
use Override;
use WeakReference;
use function method_exists;
/**
* Utility class for simply create a custom form element
*
* <code>
* // Declaration
* class MyForm extends CustomForm
* {
* public function configure(FormBuilderInterface $builder)
* {
* $builder->generates(MyEntity::class);
* $builder->string('foo')->setter();
* }
* }
*
* // Usage
* $form = new MyForm(); // Directly instantiate the form
* $form = $this->registry->elementBuilder(MyForm::class)->buildElement(); // Use registry and builder
*
* if (!$form->submit($request->post())->valid()) {
* return new JsonResponse($form->error()->print(new FormErrorFormat()), 400);
* }
*
* $entity = $form->value();
* $this->service->handle($entity);
*
* return new Response('OK', 200);
* </code>
*
* @todo implements root form interface ?
* @template T as array|object
* @implements FormInterface<T>
*/
abstract class CustomForm implements FormInterface
{
private readonly FormBuilderInterface $builder;
/**
* The inner form
*
* @var FormInterface<T>|null
*/
private ?FormInterface $form = null;
/**
* @var WeakReference<ChildInterface>|null
*/
private ?WeakReference $container = null;
/**
* @var list<callable(static, FormBuilderInterface): void>
*/
private array $preConfigureHooks = [];
/**
* @var list<callable(static, FormInterface<T>): void>
*/
private array $postConfigureHooks = [];
/**
* CustomForm constructor.
*
* @param FormBuilderInterface|null $builder
*/
public function __construct(?FormBuilderInterface $builder = null)
{
$this->builder = $builder ?? new FormBuilder();
}
#[Override]
public function offsetGet(mixed $offset): ChildInterface
{
return $this->form()[$offset];
}
#[Override]
public function offsetExists(mixed $offset): bool
{
return isset($this->form()[$offset]);
}
#[Override]
public function offsetSet(mixed $offset, mixed $value): void
{
$this->form()[$offset] = $value;
}
#[Override]
public function offsetUnset(mixed $offset): void
{
unset($this->form()[$offset]);
}
#[Override]
public function getIterator(): Iterator
{
return $this->form()->getIterator();
}
#[Override]
public function submit(mixed $data): static
{
$this->submitTarget()->submit($data);
return $this;
}
#[Override]
public function patch(mixed $data): static
{
$this->submitTarget()->patch($data);
return $this;
}
#[Override]
public function import($entity): static
{
$this->form()->import($entity);
return $this;
}
#[Override]
public function value(): array|object|null
{
return $this->form()->value();
}
#[Override]
public function httpValue(): mixed
{
return $this->form()->httpValue();
}
#[Override]
public function valid(): bool
{
return $this->form()->valid();
}
#[Override]
public function failed(): bool
{
// Do not use $this->form()->failed() because it may be not implemented
return !$this->valid();
}
#[Override]
public function error(?HttpFieldPath $field = null): FormError
{
return $this->form()->error($field);
}
#[Override]
public function container(): ?ChildInterface
{
return $this->container?->get();
}
#[Override]
public function setContainer(ChildInterface $container): ElementInterface
{
$form = clone $this;
$form->container = WeakReference::create($container);
$form->form = null; // Reset the form to ensure that $this references will be regenerated
return $form;
}
#[Override]
public function root(): RootElementInterface
{
// @todo bad root form ?
return $this->form()->root();
}
#[Override]
public function attach(mixed $entity): FormInterface
{
$this->form()->attach($entity);
return $this;
}
#[Override]
public function view(?HttpFieldPath $field = null): FormView
{
$form = $this->form();
/** @var FormView $view */
$view = $form->container() === null
? $form->root()->view($field)
: $form->view($field)
;
$view->setType(static::class);
return $view;
}
/**
* Configure the form using the builder
*
* @param FormBuilder $builder
*/
abstract protected function configure(FormBuilderInterface $builder): void;
/**
* Override this method to hook the inner form build
*
* <code>
* class MyForm extends CustomForm
* {
* public $foo;
* public function configure(FormBuilderInterface $builder): void
* {
* $builder->string('foo');
* }
*
* public function postConfigure(FormInterface $form): void
* {
* // Get the "foo" children
* $this->foo = $form['foo'];
* }
* }
* </code>
*
* @param FormInterface $form The inner form built instance
*/
public function postConfigure(FormInterface $form): void
{
// to overrides
}
/**
* Define hooks called before the form is built
* @param list<callable(static, FormBuilderInterface):void> $hooks
* @return void
* @internal This method should be called by the {@see CustomFormBuilder}
*/
final public function setPreConfigureHooks(array $hooks): void
{
$this->preConfigureHooks = $hooks;
}
/**
* Define hooks hook called after the form is built
* @param list<callable(static, FormInterface<T>):void> $hooks
* @return void
* @internal This method should be called by the {@see CustomFormBuilder}
*/
final public function setPostConfigureHooks(array $hooks): void
{
$this->postConfigureHooks = $hooks;
}
/**
* Disable the CSRF validation
* The CSRF token will be still generated, and the element will be still present on the form
*
* Note: the CSRF will be disabled on the root form, so all the sub-forms will be affected
*
* @return void
*/
final public function disableCsrfValidation(): void
{
$this->root()->set(CsrfValueValidator::FLAG_DISABLE_CSRF_VALIDATION, true);
}
/**
* Get (or build) the inner form
*
* @return FormInterface<T>
*/
final protected function form(): FormInterface
{
if ($this->form) {
return $this->form;
}
// Form can be rebuilt, so we need to clone the builder to avoid side effects
$builder = clone $this->builder;
foreach ($this->preConfigureHooks as $hook) {
$hook($this, $builder);
}
/** @psalm-suppress ArgumentTypeCoercion */
$this->configure($builder);
/** @var FormInterface<T> $form */
$form = $builder->buildElement();
if ($this->container && $container = $this->container->get()) {
$form = $form->setContainer($container);
}
$this->form = $form;
$this->postConfigure($form);
foreach ($this->postConfigureHooks as $hook) {
$hook($this, $form);
}
return $form;
}
/**
* Get the submit target element
* This element must be used for all submit or patch call
* Handle submit button if the current form is the root element
*
* @return ElementInterface
*/
final protected function submitTarget(): ElementInterface
{
$form = $this->form();
// The form is the root form
if ($form->container() === null) {
return $form->root();
}
return $form;
}
}