Passed
Push — master ( 779d84...3d5507 )
by Vincent
05:31
created

CustomFormBuilder::value()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
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\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
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...
39
     */
40
    private $formFactory;
41
42
43
    /**
44
     * CustomFormBuilder constructor.
45
     *
46
     * @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...
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