|
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
|
|
|
|