|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Form\Custom; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Form\Aggregate\FormBuilder; |
|
6
|
|
|
use Bdf\Form\Aggregate\FormBuilderInterface; |
|
7
|
|
|
use Bdf\Form\ElementBuilderInterface; |
|
8
|
|
|
use Bdf\Form\ElementInterface; |
|
9
|
|
|
use Bdf\Form\Registry\RegistryInterface; |
|
10
|
|
|
use Bdf\Form\Util\DelegateElementBuilderTrait; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Builder for extends a custom form |
|
14
|
|
|
* All build calls are forwarded to the inner form builder |
|
15
|
|
|
* The inner builder is used as custom form's builder on the `configure()` method |
|
16
|
|
|
* |
|
17
|
|
|
* <code> |
|
18
|
|
|
* $embedded = $builder->add('embd', MyCustomForm::class); |
|
19
|
|
|
* $embedded->string('foo'); // Add a new field |
|
20
|
|
|
* </code> |
|
21
|
|
|
* |
|
22
|
|
|
* @see FormBuilderInterface::add() With custom form class name as second parameter |
|
23
|
|
|
* @see RegistryInterface::elementBuilder() With custom form class name as parameter |
|
24
|
|
|
* @see CustomForm::configure() |
|
25
|
|
|
* |
|
26
|
|
|
* @mixin FormBuilderInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
class CustomFormBuilder implements ElementBuilderInterface |
|
29
|
|
|
{ |
|
30
|
|
|
use DelegateElementBuilderTrait; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var FormBuilderInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $builder; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var class-string<CustomForm>|callable(FormBuilderInterface):CustomForm |
|
|
|
|
|
|
39
|
|
|
*/ |
|
40
|
|
|
private $formFactory; |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* CustomFormBuilder constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param class-string<CustomForm>|callable(FormBuilderInterface):CustomForm $formFactory |
|
|
|
|
|
|
47
|
|
|
* @param FormBuilderInterface|null $builder |
|
48
|
|
|
*/ |
|
49
|
13 |
|
public function __construct($formFactory, ?FormBuilderInterface $builder = null) |
|
50
|
|
|
{ |
|
51
|
13 |
|
$this->formFactory = $formFactory; |
|
52
|
13 |
|
$this->builder = $builder ?: new FormBuilder(); |
|
53
|
13 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
* |
|
58
|
|
|
* @return CustomForm |
|
59
|
|
|
*/ |
|
60
|
13 |
|
public function buildElement(): ElementInterface |
|
61
|
|
|
{ |
|
62
|
13 |
|
if (is_string($this->formFactory)) { |
|
63
|
|
|
/** @var class-string<CustomForm> $className */ |
|
64
|
12 |
|
$className = $this->formFactory; |
|
65
|
|
|
|
|
66
|
12 |
|
return new $className($this->builder); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
return ($this->formFactory)($this->builder); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
6 |
|
protected function getElementBuilder(): ElementBuilderInterface |
|
76
|
|
|
{ |
|
77
|
6 |
|
return $this->builder; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|