Completed
Push — develop ( d42c3f...ac22da )
by Freddie
02:15
created

AbstractBuilder::build()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 2
b 0
f 0
nc 6
nop 0
dl 0
loc 25
rs 9.4555
1
<?php
2
3
namespace FlexPHP\Inputs\Builder;
4
5
use Symfony\Component\Form\FormBuilderInterface;
6
7
abstract class AbstractBuilder implements BuilderInterface
8
{
9
    protected $name;
10
    protected $options;
11
12
    public function getName(): string
13
    {
14
        return preg_replace('/(\s)+/', '_', trim($this->name)) ?? $this->name;
15
    }
16
17
    public function getOptions(): array
18
    {
19
        return $this->options;
20
    }
21
22
    abstract protected function factory(): FormBuilderInterface;
23
24
    protected function twig()
25
    {
26
        $appVariableReflection = new \ReflectionClass('\Symfony\Bridge\Twig\AppVariable');
27
        $vendorTwigBridgeDirectory = dirname((string)$appVariableReflection->getFileName());
28
29
        $loader = new \Twig\Loader\FilesystemLoader([
30
            $vendorTwigBridgeDirectory . '/Resources/views/Form',
31
        ]);
32
33
        $twig = new \Twig\Environment($loader);
34
        $twig->addExtension(new \Symfony\Bridge\Twig\Extension\FormExtension());
35
        $twig->addExtension(new \Symfony\Bridge\Twig\Extension\TranslationExtension(
36
            new \Symfony\Component\Translation\Translator('en')
37
        ));
38
39
        $formEngine = new \Symfony\Bridge\Twig\Form\TwigRendererEngine(['bootstrap_4_layout.html.twig'], $twig);
40
        $twig->addRuntimeLoader(new \Twig\RuntimeLoader\FactoryRuntimeLoader([
41
            \Symfony\Component\Form\FormRenderer::class => function () use ($formEngine) {
42
                return new \Symfony\Component\Form\FormRenderer($formEngine);
43
            },
44
        ]));
45
46
        return $twig;
47
    }
48
}
49