Completed
Push — develop ( 7b16e5...b5dddc )
by Freddie
14:27
created

AbstractBuilder::getPathTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
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 1
cts 1
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
use FlexPHP\Generator\Domain\Traits\InflectorTrait;
13
14
abstract class AbstractBuilder implements BuilderInterface
15
{
16
    use InflectorTrait;
17
18
    /**
19
     * @var array<array|string>
20
     */
21
    private $data;
22
23 69
    /**
24
     * @param array<array|string> $data
25 69
     */
26 23
    public function __construct(array $data)
27
    {
28
        $this->data = $data;
29 69
    }
30 69
31 69
    public function __toString()
32
    {
33 69
        return $this->build();
34
    }
35 69
36
    public function build(): string
37
    {
38 69
        $loader = new \Twig\Loader\FilesystemLoader($this->getPathTemplate());
39
        $twig = new \Twig\Environment($loader);
40 69
41 69
        return $twig->render($this->getFileTemplate(), $this->data);
42
    }
43 69
44
    protected function getPathTemplate(): string
45
    {
46 4
        return \sprintf('%1$s/../BoilerPlates', __DIR__);
47
    }
48 4
49
    protected function getFkRelations(array $fkRelations): array
50
    {
51 64
        return \array_reduce($fkRelations, function (array $result, array $fkRel): array {
52
            $result[] = [
53 64
                'fnPlural' => $this->getPascalCase($this->getPluralize($fkRel['table'])),
54
                'fnSingular' => $this->getPascalCase($this->getSingularize($fkRel['table'])),
55
                'route' => $this->getDashCase($this->getPluralize($fkRel['table'])),
56 18
                'table' => $fkRel['table'],
57
                'id' => $fkRel['id'],
58 18
                'text' => $fkRel['name'],
59
            ];
60
61 17
            return $result;
62
        }, []);
63 17
    }
64
65
    abstract protected function getFileTemplate(): string;
66
}
67