Completed
Push — develop ( 8dd5aa...fee724 )
by Freddie
14:52
created

AbstractBuilder::getPathTemplate()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of FlexPHP.
4
 *
5
 * (c) Freddie Gar <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace FlexPHP\Generator\Domain\Builders;
11
12
abstract class AbstractBuilder implements BuilderInterface
13
{
14
    /**
15
     * @var array<array|string>
16
     */
17
    private $data;
18
19
    /**
20
     * @param array<array|string> $data
21
     */
22
    public function __construct(array $data)
23 69
    {
24
        $this->data = $data;
25 69
    }
26 23
27
    public function __toString()
28
    {
29 69
        return $this->build();
30 69
    }
31 69
32
    public function build(): string
33 69
    {
34
        $loader = new \Twig\Loader\FilesystemLoader($this->getPathTemplate());
35 69
        $twig = new \Twig\Environment($loader);
36
37
        return $twig->render($this->getFileTemplate(), $this->data);
38 69
    }
39
40 69
    protected function getInflector(): Inflector
41 69
    {
42
        static $inflector;
43 69
44
        if ($inflector) {
45
            return $inflector;
46 4
        }
47
48 4
        return $inflector = new Inflector();
49
    }
50
51 64
    protected function getPathTemplate(): string
52
    {
53 64
        return \sprintf('%1$s/../BoilerPlates', __DIR__);
54
    }
55
56 18
    protected function getFkRelations(array $fkRelations): array
57
    {
58 18
        $fkRels = [];
59
60
        foreach ($fkRelations as $name => $fkRel) {
61 17
            $name = $this->getInflector()->camelProperty($fkRel['pkId']);
62
63 17
            $fkRels[$name] = [
64
                'fnPlural' => $this->getInflector()->fnPlural($fkRel['pkTable']),
65
                'fnSingular' => $this->getInflector()->fnSingular($fkRel['pkTable']),
66
                'item' => $this->getInflector()->item($fkRel['pkTable']),
67
                'items' => $this->getInflector()->items($fkRel['pkTable']),
68
                'route' => $this->getInflector()->route($fkRel['pkTable']),
69
                'table' => $fkRel['pkTable'],
70
                'pk' => $fkRel['pkId'],
71
                'pkName' => $name,
72
                'dataType' => $fkRel['pkDataType'],
73
                'typeHint' => $fkRel['pkTypeHint'],
74
                'id' => $this->getInflector()->camelProperty($fkRel['fkId']),
75
                'text' => $this->getInflector()->camelProperty($fkRel['fkName']),
76
                'fkRoute' => $this->getInflector()->route($fkRel['fkTable']),
77
                'required' => $fkRel['isRequired'],
78
                'blameBy' => $fkRel['isBlameBy'],
79
                'pkNamePascal' => $this->getInflector()->pascalProperty($fkRel['pkId']),
80
            ];
81
        }
82
83
        return $fkRels;
84
    }
85
86
    protected function getFkFunctions(array $fkRelations): array
87
    {
88
        $fkFunctions = [];
89
90
        \array_map(function (array $fkRel) use (&$fkFunctions): void {
91
            $fkFunctions[$fkRel['fnSingular']] = $fkRel;
92
        }, $this->getFkRelations($fkRelations));
93
94
        return $fkFunctions;
95
    }
96
97
    abstract protected function getFileTemplate(): string;
98
}
99