AbstractBuilder::getFkRelations()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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