1
|
|
|
<?php |
2
|
|
|
/******************************************************************************** |
3
|
|
|
* Apache License, Version 2.0 * |
4
|
|
|
* * |
5
|
|
|
* Copyright [2020] [Nurlan Mukhanov <[email protected]>] * |
6
|
|
|
* * |
7
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); * |
8
|
|
|
* you may not use this file except in compliance with the License. * |
9
|
|
|
* You may obtain a copy of the License at * |
10
|
|
|
* * |
11
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 * |
12
|
|
|
* * |
13
|
|
|
* Unless required by applicable law or agreed to in writing, software * |
14
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, * |
15
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * |
16
|
|
|
* See the License for the specific language governing permissions and * |
17
|
|
|
* limitations under the License. * |
18
|
|
|
* * |
19
|
|
|
********************************************************************************/ |
20
|
|
|
|
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace DBD\Entity; |
24
|
|
|
|
25
|
|
|
use DBD\Entity\Common\EntityException; |
26
|
|
|
use DBD\Entity\Interfaces\EntityMapper; |
27
|
|
|
use ReflectionClass; |
28
|
|
|
use ReflectionException; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class MapperAttributed |
33
|
|
|
* |
34
|
|
|
* @package DBD\Entity |
35
|
|
|
*/ |
36
|
|
|
class MapperAttributed implements EntityMapper |
37
|
|
|
{ |
38
|
|
|
use MapperTrait; |
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @throws ReflectionException |
42
|
|
|
*/ |
43
|
|
|
public function __construct( |
44
|
|
|
protected string $entityClass |
45
|
|
|
) { |
46
|
|
|
$this->getAllVariables(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function name(): string |
50
|
|
|
{ |
51
|
|
|
return substr($this->entityClass, strrpos($this->entityClass, '\\') + 1). 'Map'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getEntityClass(): string |
55
|
|
|
{ |
56
|
|
|
return $this->entityClass; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
* @throws ReflectionException |
62
|
|
|
* @throws EntityException |
63
|
|
|
*/ |
64
|
|
|
public function getScheme(): string |
65
|
|
|
{ |
66
|
|
|
$reflection = new ReflectionClass($this->entityClass); |
67
|
|
|
|
68
|
|
|
$attributes = $reflection->getAttributes(EntityTable::class); |
69
|
|
|
|
70
|
|
|
foreach ($attributes as $attribute) { |
71
|
|
|
return $attribute->newInstance()->scheme; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
throw new EntityException('Missing attribute scheme for ' . $this->entityClass); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
* @throws EntityException |
80
|
|
|
* @throws ReflectionException |
81
|
|
|
*/ |
82
|
|
|
public function getTableName(): string |
83
|
|
|
{ |
84
|
|
|
$reflection = new ReflectionClass($this->entityClass); |
85
|
|
|
|
86
|
|
|
$attributes = $reflection->getAttributes(EntityTable::class); |
87
|
|
|
|
88
|
|
|
foreach ($attributes as $attribute) { |
89
|
|
|
return $attribute->newInstance()->name; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
throw new EntityException('Missing attribute name for ' . $this->entityClass); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
* @throws EntityException |
98
|
|
|
* @throws ReflectionException |
99
|
|
|
*/ |
100
|
|
|
public function getAnnotation(): string |
101
|
|
|
{ |
102
|
|
|
$reflection = new ReflectionClass($this->entityClass); |
103
|
|
|
|
104
|
|
|
$attributes = $reflection->getAttributes(EntityTable::class); |
105
|
|
|
|
106
|
|
|
foreach ($attributes as $attribute) { |
107
|
|
|
return $attribute->newInstance()->annotation; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
throw new EntityException('Missing attribute annotation for ' . $this->entityClass); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return MapperVariables |
115
|
|
|
* @throws ReflectionException |
116
|
|
|
*/ |
117
|
|
|
public function getAllVariables(): MapperVariables |
118
|
|
|
{ |
119
|
|
|
$thisName = $this->name(); |
120
|
|
|
|
121
|
|
|
if (!isset(MapperCache::me()->allVariables[$thisName])) { |
122
|
|
|
$reflectionClass = new ReflectionClass($this->entityClass); |
123
|
|
|
|
124
|
|
|
$properties = $reflectionClass->getProperties(); |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @var Constraint[] $constraints |
128
|
|
|
* @var Embedded[] $embedded |
129
|
|
|
* @var Complex[] $complexes |
130
|
|
|
* @var Column[] $columns |
131
|
|
|
*/ |
132
|
|
|
$constraints = $embedded = $complexes = $columns = []; |
133
|
|
|
|
134
|
|
|
foreach ($properties as $property) { |
135
|
|
|
$attributes = $property->getAttributes(); |
136
|
|
|
|
137
|
|
|
foreach ($attributes as $attribute) { |
138
|
|
|
if ($attribute->getName() === Column::class || is_subclass_of($attribute->getName(), Column::class)) { |
139
|
|
|
$columns[$property->getName()] = $attribute->newInstance(); |
140
|
|
|
} |
141
|
|
|
else if ($attribute->getName() === Constraint::class || is_subclass_of($attribute->getName(), Constraint::class)) { |
142
|
|
|
$constraints[$property->getName()] = $attribute->newInstance(); |
143
|
|
|
} |
144
|
|
|
else if ($attribute->getName() === Embedded::class || is_subclass_of($attribute->getName(), Embedded::class)) { |
145
|
|
|
$embedded[$property->getName()] = $attribute->newInstance(); |
146
|
|
|
} |
147
|
|
|
else if ($attribute->getName() === Complex::class || is_subclass_of($attribute->getName(), Complex::class)) { |
148
|
|
|
$complexes[$property->getName()] = $attribute->newInstance(); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$this->processComplexes($complexes); |
154
|
|
|
$this->processEmbedded($embedded); |
155
|
|
|
$this->processColumns($columns); |
156
|
|
|
$this->processConstraints($constraints, $columns, $embedded, $complexes); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return MapperCache::me()->allVariables[$thisName]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param Complex[] $complexes |
164
|
|
|
* @return void |
165
|
|
|
*/ |
166
|
|
|
private function processComplexes(array $complexes): void |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param Embedded[] $embedded |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
|
|
private function processEmbedded(array $embedded): void |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param Column[] $columns |
182
|
|
|
* @return void |
183
|
|
|
*/ |
184
|
|
|
private function processColumns(array $columns): void |
185
|
|
|
{ |
186
|
|
|
$thisName = $this->name(); |
187
|
|
|
|
188
|
|
|
if (!isset(MapperCache::me()->columns[$thisName])) { |
189
|
|
|
MapperCache::me()->columns[$thisName] = []; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
foreach ($columns as $columnName => $column) { |
193
|
|
|
MapperCache::me()->columns[$thisName][$columnName] = $column; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param Constraint[] $constraints |
199
|
|
|
* @param Column[] $columns |
200
|
|
|
* @param Embedded[] $embedded |
201
|
|
|
* @param Complex[] $complexes |
202
|
|
|
* @return void |
203
|
|
|
* @throws EntityException |
204
|
|
|
* @throws ReflectionException |
205
|
|
|
*/ |
206
|
|
|
private function processConstraints(array $constraints, array $columns, array $embedded, array $complexes): void |
207
|
|
|
{ |
208
|
|
|
$thisName = $this->name(); |
209
|
|
|
|
210
|
|
|
if (!isset(MapperCache::me()->constraints[$thisName])) { |
211
|
|
|
MapperCache::me()->constraints[$thisName] = []; |
212
|
|
|
|
213
|
|
|
$entityClass = $this->getEntityClass(); |
214
|
|
|
|
215
|
|
|
foreach ($constraints as $constraintName => $constraint) { |
216
|
|
|
if ($entityClass !== View::class && is_string($constraint->localColumn)) { |
217
|
|
|
$constraint->localColumn = $this->findColumnByOriginName($constraint->localColumn); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
MapperCache::me()->constraints[$thisName][$constraintName] = $constraint; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
MapperCache::me()->allVariables[$thisName] = new MapperVariables($columns, $constraints, $embedded, $complexes); |
224
|
|
|
|
225
|
|
|
foreach (MapperCache::me()->constraints[$thisName] as $constraintName => $constraint) { |
226
|
|
|
$constraint->localTable = $this->getTable(); |
227
|
|
|
|
228
|
|
|
$this->$constraintName = $constraint; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|