Passed
Push — master ( 1bc594...fed342 )
by Bruno
07:08
created

Framework::editableCompose()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 11
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Frontend\Blade;
4
5
use Formularium\HTMLElement;
6
use Formularium\Model;
7
8
class Framework extends \Formularium\Framework
9
{
10
    /**
11
     * The viewable template.
12
     *
13
     * @var string
14
     */
15
    protected $viewableTemplate = '';
16
17
    /**
18
     * @var string
19
     */
20
    protected $editableTemplate = '';
21
22
    public function __construct(string $name = 'Blade')
23
    {
24
        parent::__construct($name);
25
26
        $this->editableTemplate = <<<EOF
27
{!! Form::open(['route' => '{{modelName}}.store']) !!}
28
29
{{form}}
30
31
{!! Form::submit('Submit', ['class' => 'btn btn-info']) !!}
32
33
{!! Form::close() !!}
34
EOF;
35
    }
36
37
    public function editableCompose(Model $m, array $elements, string $previousCompose): string
38
    {
39
        $editableForm = join('', $elements);
40
        $templateData = [
41
            'form' => $editableForm,
42
        ];
43
   
44
        return $this->fillTemplate(
45
            $this->editableTemplate,
46
            $templateData,
47
            $m
48
        );
49
    }
50
51
    protected function fillTemplate(string $template, array $data, Model $m): string
52
    {
53
        foreach ($data as $name => $value) {
54
            $template = str_replace(
55
                '{{' . $name . '}}',
56
                $value,
57
                $template
58
            );
59
        }
60
61
        $template = str_replace(
62
            '{{modelName}}',
63
            $m->getName(),
64
            $template
65
        );
66
        $template = str_replace(
67
            '{{modelNameLower}}',
68
            mb_strtolower($m->getName()),
69
            $template
70
        );
71
        return $template;
72
    }
73
}
74