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->name(), $schema->fkRelations()); |
24
|
3 |
|
$fkSetters = $this->getFkSetters($schema->name(), $schema->fkRelations()); |
25
|
|
|
$_properties = $this->getProperties($schema->attributes()); |
26
|
3 |
|
$defaults = $this->getDefaults($schema->attributes()); |
27
|
|
|
$fkFns = $this->getFkFunctions($schema->fkRelations()); |
28
|
|
|
$fkRels = $this->getFkRelations($schema->fkRelations()); |
29
|
3 |
|
$header = self::getHeaderFile(); |
30
|
|
|
|
31
|
3 |
|
parent::__construct( |
32
|
|
|
\compact( |
33
|
|
|
'header', |
34
|
3 |
|
'name', |
35
|
|
|
'getters', |
36
|
3 |
|
'fkGetters', |
37
|
|
|
'setters', |
38
|
3 |
|
'fkSetters', |
39
|
3 |
|
'_properties', |
40
|
3 |
|
'fkFns', |
41
|
|
|
'fkRels', |
42
|
|
|
'defaults' |
43
|
|
|
) |
44
|
3 |
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
protected function getFileTemplate(): string |
48
|
|
|
{ |
49
|
3 |
|
return 'Entity.php.twig'; |
50
|
|
|
} |
51
|
3 |
|
|
52
|
3 |
|
protected function getPathTemplate(): string |
53
|
3 |
|
{ |
54
|
|
|
return \sprintf('%1$s/FlexPHP/Entity', parent::getPathTemplate()); |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
private function getProperties(array $properties): array |
58
|
|
|
{ |
59
|
|
|
return \array_values( |
60
|
|
|
\array_reduce( |
61
|
|
|
$properties, |
62
|
|
|
function (array $result, SchemaAttributeInterface $attribute): array { |
63
|
|
|
$result[] = $this->getInflector()->camelProperty($attribute->name()); |
64
|
|
|
|
65
|
|
|
return $result; |
66
|
|
|
}, |
67
|
|
|
[] |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function getDefaults(array $properties): array |
73
|
|
|
{ |
74
|
|
|
return \array_reduce( |
75
|
|
|
$properties, |
76
|
|
|
function (array $result, SchemaAttributeInterface $attribute): array { |
77
|
|
|
if ($attribute->default() !== null) { |
78
|
|
|
$name = $this->getInflector()->camelProperty($attribute->name()); |
79
|
|
|
$result[$name] = [ |
80
|
|
|
'type' => $attribute->typeHint(), |
81
|
|
|
'value' => $attribute->default(), |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
return $result; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $result; |
88
|
|
|
}, |
89
|
|
|
[] |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function getGetters(array $properties): array |
94
|
|
|
{ |
95
|
|
|
return \array_reduce( |
96
|
|
|
$properties, |
97
|
|
|
function (array $result, SchemaAttributeInterface $attribute): array { |
98
|
|
|
$result[] = new GetterBuilder($attribute); |
99
|
|
|
|
100
|
|
|
return $result; |
101
|
|
|
}, |
102
|
|
|
[] |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function getSetters(array $properties): array |
107
|
|
|
{ |
108
|
|
|
return \array_reduce( |
109
|
|
|
$properties, |
110
|
|
|
function (array $result, SchemaAttributeInterface $attribute): array { |
111
|
|
|
$result[] = new SetterBuilder($attribute); |
112
|
|
|
|
113
|
|
|
return $result; |
114
|
|
|
}, |
115
|
|
|
[] |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function getFkGetters(string $table, array $fkRelations): array |
120
|
|
|
{ |
121
|
|
|
return \array_reduce( |
122
|
|
|
$fkRelations, |
123
|
|
|
function (array $result, array $fkRel) use ($table): array { |
124
|
|
|
$result[] = new FkGetterBuilder( |
125
|
|
|
$fkRel['pkId'], |
126
|
|
|
$fkRel['pkTable'] === $table ? 'self' : $fkRel['pkTable'], |
127
|
|
|
$fkRel['isRequired'] |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
return $result; |
131
|
|
|
}, |
132
|
|
|
[] |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
private function getFkSetters(string $table, array $fkRelations): array |
137
|
|
|
{ |
138
|
|
|
return \array_reduce( |
139
|
|
|
$fkRelations, |
140
|
|
|
function (array $result, array $fkRel) use ($table): array { |
141
|
|
|
$result[] = new FkSetterBuilder( |
142
|
|
|
$fkRel['pkId'], |
143
|
|
|
$fkRel['pkTable'] === $table ? 'self' : $fkRel['pkTable'], |
144
|
|
|
$fkRel['isRequired'], |
145
|
|
|
$table |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
return $result; |
149
|
|
|
}, |
150
|
|
|
[] |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|