Passed
Push — master ( ea4e6c...05ab19 )
by Vincent
02:14 queued 34s
created

CustomFormBuilder::getElementBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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\Aggregate\FormInterface;
8
use Bdf\Form\ElementBuilderInterface;
9
use Bdf\Form\ElementInterface;
10
use Bdf\Form\Registry\RegistryInterface;
11
use Bdf\Form\Util\DelegateElementBuilderTrait;
12
13
use function is_string;
14
15
/**
16
 * Builder for extends a custom form
17
 * All build calls are forwarded to the inner form builder
18
 * The inner builder is used as custom form's builder on the `configure()` method
19
 *
20
 * <code>
21
 * $embedded = $builder->add('embd', MyCustomForm::class);
22
 * $embedded->string('foo'); // Add a new field
23
 * </code>
24
 *
25
 * @see FormBuilderInterface::add() With custom form class name as second parameter
26
 * @see RegistryInterface::elementBuilder() With custom form class name as parameter
27
 * @see CustomForm::configure()
28
 *
29
 * @mixin FormBuilderInterface
30
 */
31
class CustomFormBuilder implements ElementBuilderInterface
32
{
33
    use DelegateElementBuilderTrait;
34
35
    /**
36
     * @var FormBuilderInterface
37
     */
38
    private $builder;
39
40
    /**
41
     * @var class-string<CustomForm>|callable(FormBuilderInterface):CustomForm
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<CustomForm>...erInterface):CustomForm at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<CustomForm>|callable(FormBuilderInterface):CustomForm.
Loading history...
42
     */
43
    private $formFactory;
44
45
    /**
46
     * @var list<callable(CustomForm, FormBuilderInterface): void>
0 ignored issues
show
Bug introduced by
The type Bdf\Form\Custom\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
47
     */
48
    private $preConfigureHooks = [];
49
50
    /**
51
     * @var list<callable(CustomForm, FormInterface): void>
52
     */
53
    private $postConfigureHooks = [];
54
55
56
    /**
57
     * CustomFormBuilder constructor.
58
     *
59
     * @param class-string<CustomForm>|callable(FormBuilderInterface):CustomForm $formFactory
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<CustomForm>...erInterface):CustomForm at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<CustomForm>|callable(FormBuilderInterface):CustomForm.
Loading history...
60
     * @param FormBuilderInterface|null $builder
61
     */
62 15
    public function __construct($formFactory, ?FormBuilderInterface $builder = null)
63
    {
64 15
        $this->formFactory = $formFactory;
65 15
        $this->builder = $builder ?: new FormBuilder();
66 15
    }
67
68
    /**
69
     * {@inheritdoc}
70
     *
71
     * @return CustomForm
72
     */
73 15
    public function buildElement(): ElementInterface
74
    {
75 15
        if (is_string($this->formFactory)) {
76
            /** @var class-string<CustomForm> $className */
77 14
            $className = $this->formFactory;
78
79 14
            $form = new $className($this->builder);
80
        } else {
81 1
            $form = ($this->formFactory)($this->builder);
82
        }
83
84 15
        $form->setPreConfigureHooks($this->preConfigureHooks);
85 15
        $form->setPostConfigureHooks($this->postConfigureHooks);
86
87 15
        return $form;
88
    }
89
90
    /**
91
     * Add a hook called before the form is built
92
     *
93
     * This hook can be used to call setters on the custom form, or to add fields on the inner form builder.
94
     * It will be called before the {@see CustomForm::configure()} method, and takes as parameter the custom form instance and the inner form builder.
95
     *
96
     * Usage:
97
     * <code>
98
     * $builder->preConfigure(function (MyCustomForm $form, FormBuilderInterface $builder) {
99
     *    $form->setFoo('bar');
100
     *    $builder->string('foo');
101
     * });
102
     * </code>
103
     *
104
     * @param callable(CustomForm, FormBuilderInterface):void $hook
105
     *
106
     * @return $this
107
     */
108 1
    public function preConfigure(callable $hook): self
109
    {
110 1
        $this->preConfigureHooks[] = $hook;
111
112 1
        return $this;
113
    }
114
115
    /**
116
     * Add a hook called after the form is built
117
     * It will be called after the {@see CustomForm::configure()} and {@see CustomForm::postConfigure()} methods, and takes as parameter the custom form instance and the inner form.
118
     *
119
     * @param callable(CustomForm, FormInterface):void $hook
120
     *
121
     * @return $this
122
     */
123 1
    public function postConfigure(callable $hook): self
124
    {
125 1
        $this->postConfigureHooks[] = $hook;
126
127 1
        return $this;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 6
    protected function getElementBuilder(): ElementBuilderInterface
134
    {
135 6
        return $this->builder;
136
    }
137
}
138