Completed
Push — develop ( 2b89a1...8f9d97 )
by Freddie
04:51 queued 10s
created

AbstractBuilder::getDefaultOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace FlexPHP\Inputs\Builder;
4
5
use Symfony\Component\Form\Extension\Core\Type\FormType;
6
use Symfony\Component\Form\FormBuilderInterface;
7
use Symfony\Component\Form\Forms;
8
use Symfony\Component\Form\FormInterface;
9
10
abstract class AbstractBuilder implements BuilderInterface
11
{
12
    private $name;
13
    private $properties;
14
    private $options;
15
    protected $build = null;
16
17
    public function __construct(string $name, array $properties, array $options)
18
    {
19
        $this->name = $name;
20
        $this->properties = $properties;
21
        $this->options = $options;
22
    }
23
24
    abstract protected function getType(): string;
25
26
    abstract protected function getFileTemplate(): string;
27
28
    public function getName(): string
29
    {
30
        return $this->name;
31
    }
32
33
    public function getProperties(): array
34
    {
35
        return $this->properties;
36
    }
37
38
    public function getOptions(): array
39
    {
40
        return $this->options;
41
    }
42
43
    public function build(): FormInterface
44
    {
45
        return $this->factory()
46
            ->add($this->getName(), $this->getType(), $this->getDefaultOptions($this->getOptions()))
47
            ->getForm();
48
    }
49
50
    public function render(): string
51
    {
52
        $appVariableReflection = new \ReflectionClass('\Symfony\Bridge\Twig\AppVariable');
53
        $vendorTwigBridgeDirectory = dirname((string)$appVariableReflection->getFileName());
54
55
        $loader = new \Twig\Loader\FilesystemLoader([
56
            $this->getPathTemplate(),
57
            $vendorTwigBridgeDirectory . '/Resources/views/Form',
58
        ]);
59
60
        $twig = new \Twig\Environment($loader);
61
        $twig->addExtension(new \Symfony\Bridge\Twig\Extension\FormExtension());
62
        $twig->addExtension(new \Symfony\Bridge\Twig\Extension\TranslationExtension(
63
            new \Symfony\Component\Translation\Translator('en')
64
        ));
65
66
        $formEngine = new \Symfony\Bridge\Twig\Form\TwigRendererEngine(['bootstrap_4_layout.html.twig'], $twig);
67
        $twig->addRuntimeLoader(new \Twig\RuntimeLoader\FactoryRuntimeLoader([
68
            \Symfony\Component\Form\FormRenderer::class => function () use ($formEngine) {
69
                return new \Symfony\Component\Form\FormRenderer($formEngine);
70
            },
71
        ]));
72
73
        return $twig->render($this->getFileTemplate(), [
74
            'form' => $this->build()->createView(),
75
            'field' => $this->getName(),
76
        ]);
77
    }
78
79
    protected function factory(): FormBuilderInterface
80
    {
81
        return Forms::createFormFactory()->createBuilder(FormType::class, null);
82
    }
83
84
    protected function getDefaultOptions(array $options = []): array
85
    {
86
        return [
87
            'mapped' => false,
88
        ] + $options;
89
    }
90
91
    protected function getPathTemplate(): string
92
    {
93
        return \sprintf('%1$s/../Template', __DIR__);
94
    }
95
}
96