|
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
|
3 |
|
*/ |
|
10
|
|
|
namespace FlexPHP\Generator\Domain\Builders\Entity; |
|
11
|
3 |
|
|
|
12
|
3 |
|
use FlexPHP\Generator\Domain\Builders\AbstractBuilder; |
|
13
|
3 |
|
use FlexPHP\Schema\SchemaAttributeInterface; |
|
14
|
3 |
|
use FlexPHP\Schema\SchemaInterface; |
|
15
|
|
|
|
|
16
|
3 |
|
final class EntityBuilder extends AbstractBuilder |
|
17
|
|
|
{ |
|
18
|
3 |
|
public function __construct(SchemaInterface $schema) |
|
19
|
3 |
|
{ |
|
20
|
|
|
$name = $this->getInflector()->entity($schema->name()); |
|
21
|
3 |
|
$getters = $this->getGetters($schema->attributes()); |
|
22
|
3 |
|
$setters = $this->getSetters($schema->attributes()); |
|
23
|
|
|
$fkGetters = $this->getFkGetters($schema->fkRelations()); |
|
24
|
3 |
|
$fkSetters = $this->getFkSetters($schema->fkRelations()); |
|
25
|
|
|
$_properties = $this->getProperties($schema->attributes()); |
|
26
|
3 |
|
$fkFns = $this->getFkFunctions($schema->fkRelations()); |
|
27
|
|
|
$fkRels = $this->getFkRelations($schema->fkRelations()); |
|
28
|
|
|
|
|
29
|
3 |
|
parent::__construct( |
|
30
|
|
|
\compact('name', 'getters', 'fkGetters', 'setters', 'fkSetters', '_properties', 'fkFns', 'fkRels') |
|
31
|
3 |
|
); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
3 |
|
protected function getFileTemplate(): string |
|
35
|
|
|
{ |
|
36
|
3 |
|
return 'Entity.php.twig'; |
|
37
|
|
|
} |
|
38
|
3 |
|
|
|
39
|
3 |
|
protected function getPathTemplate(): string |
|
40
|
3 |
|
{ |
|
41
|
|
|
return \sprintf('%1$s/FlexPHP/Entity', parent::getPathTemplate()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
3 |
|
private function getProperties(array $properties): array |
|
45
|
|
|
{ |
|
46
|
|
|
return \array_values( |
|
47
|
3 |
|
\array_reduce( |
|
48
|
|
|
$properties, |
|
49
|
3 |
|
function (array $result, SchemaAttributeInterface $attributes): array { |
|
50
|
|
|
$result[] = $this->getInflector()->camelProperty($attributes->name()); |
|
51
|
3 |
|
|
|
52
|
3 |
|
return $result; |
|
53
|
3 |
|
}, |
|
54
|
|
|
[] |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
3 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function getGetters(array $properties): array |
|
60
|
|
|
{ |
|
61
|
|
|
return \array_reduce( |
|
62
|
|
|
$properties, |
|
63
|
|
|
function (array $result, SchemaAttributeInterface $attributes): array { |
|
64
|
|
|
$result[] = new GetterBuilder($attributes); |
|
65
|
|
|
|
|
66
|
|
|
return $result; |
|
67
|
|
|
}, |
|
68
|
|
|
[] |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function getSetters(array $properties): array |
|
73
|
|
|
{ |
|
74
|
|
|
return \array_reduce( |
|
75
|
|
|
$properties, |
|
76
|
|
|
function (array $result, SchemaAttributeInterface $attribute): array { |
|
77
|
|
|
$result[] = new SetterBuilder($attribute); |
|
78
|
|
|
|
|
79
|
|
|
return $result; |
|
80
|
|
|
}, |
|
81
|
|
|
[] |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private function getFkGetters(array $fkRelations): array |
|
86
|
|
|
{ |
|
87
|
|
|
return \array_reduce( |
|
88
|
|
|
$fkRelations, |
|
89
|
|
|
function (array $result, array $fkRel): array { |
|
90
|
|
|
$result[] = new FkGetterBuilder($fkRel['pkId'], $fkRel['pkTable']); |
|
91
|
|
|
|
|
92
|
|
|
return $result; |
|
93
|
|
|
}, |
|
94
|
|
|
[] |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function getFkSetters(array $fkRelations): array |
|
99
|
|
|
{ |
|
100
|
|
|
return \array_reduce( |
|
101
|
|
|
$fkRelations, |
|
102
|
|
|
function (array $result, array $fkRel): array { |
|
103
|
|
|
$result[] = new FkSetterBuilder($fkRel['pkId'], $fkRel['pkTable'], $fkRel['isRequired']); |
|
104
|
|
|
|
|
105
|
|
|
return $result; |
|
106
|
|
|
}, |
|
107
|
|
|
[] |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|