1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2018 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* @license GPL-3.0 |
8
|
|
|
* @author Gerrit Addiks <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Addiks\RDMBundle\Mapping\Drivers; |
12
|
|
|
|
13
|
|
|
use Doctrine\Common\Annotations\Reader; |
14
|
|
|
use ReflectionClass; |
15
|
|
|
use ReflectionProperty; |
16
|
|
|
use Addiks\RDMBundle\Mapping\Drivers\MappingDriverInterface; |
17
|
|
|
use Addiks\RDMBundle\Mapping\MappingInterface; |
18
|
|
|
use Addiks\RDMBundle\Mapping\EntityMapping; |
19
|
|
|
use Addiks\RDMBundle\Mapping\Annotation\Service; |
20
|
|
|
use Addiks\RDMBundle\Mapping\ServiceMapping; |
21
|
|
|
use Addiks\RDMBundle\Mapping\EntityMappingInterface; |
22
|
|
|
use Addiks\RDMBundle\Mapping\Annotation\Choice; |
23
|
|
|
use Addiks\RDMBundle\Mapping\ChoiceMapping; |
24
|
|
|
use Addiks\RDMBundle\Exception\InvalidMappingException; |
25
|
|
|
use Doctrine\ORM\Mapping\Column as ColumnAnnotation; |
26
|
|
|
use Doctrine\DBAL\Schema\Column as DBALColumn; |
27
|
|
|
use Doctrine\DBAL\Types\Type; |
28
|
|
|
use Addiks\RDMBundle\Mapping\ObjectMapping; |
29
|
|
|
use Addiks\RDMBundle\Mapping\CallDefinitionInterface; |
30
|
|
|
use Addiks\RDMBundle\Mapping\FieldMapping; |
31
|
|
|
use Addiks\RDMBundle\Mapping\Annotation\Obj; |
32
|
|
|
use Addiks\RDMBundle\Mapping\Annotation\Arr; |
33
|
|
|
use Addiks\RDMBundle\Mapping\ArrayMapping; |
34
|
|
|
|
35
|
|
|
final class MappingAnnotationDriver implements MappingDriverInterface |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Reader |
40
|
|
|
*/ |
41
|
|
|
private $annotationReader; |
42
|
|
|
|
43
|
7 |
|
public function __construct(Reader $annotationReader) |
44
|
|
|
{ |
45
|
7 |
|
$this->annotationReader = $annotationReader; |
46
|
7 |
|
} |
47
|
|
|
|
48
|
6 |
|
public function loadRDMMetadataForClass(string $className): ?EntityMappingInterface |
49
|
|
|
{ |
50
|
|
|
/** @var ?EntityMappingInterface $mapping */ |
|
|
|
|
51
|
6 |
|
$mapping = null; |
52
|
|
|
|
53
|
|
|
/** @var array<MappingInterface> $fieldMappings */ |
54
|
6 |
|
$fieldMappings = array(); |
55
|
|
|
|
56
|
6 |
|
$classReflection = new ReflectionClass($className); |
57
|
|
|
|
58
|
6 |
|
foreach ($classReflection->getProperties() as $propertyReflection) { |
59
|
|
|
/** @var ReflectionProperty $propertyReflection */ |
60
|
|
|
|
61
|
|
|
/** @var string $fieldName */ |
62
|
6 |
|
$fieldName = $propertyReflection->getName(); |
63
|
|
|
|
64
|
|
|
/** @var array<object> $annotations */ |
65
|
6 |
|
$annotations = $this->annotationReader->getPropertyAnnotations($propertyReflection); |
66
|
|
|
|
67
|
6 |
|
foreach ($annotations as $annotation) { |
68
|
|
|
/** @var object $annotation */ |
69
|
|
|
|
70
|
6 |
|
$fieldMapping = $this->convertAnnotationToMapping($annotation, $fieldName, $className); |
71
|
|
|
|
72
|
6 |
|
if ($fieldMapping instanceof MappingInterface) { |
73
|
6 |
|
$fieldMappings[$fieldName] = $fieldMapping; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
6 |
|
if (!empty($fieldMappings)) { |
79
|
6 |
|
$mapping = new EntityMapping($className, $fieldMappings); |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
return $mapping; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param object $annotation |
87
|
|
|
*/ |
88
|
6 |
|
private function convertAnnotationToMapping( |
89
|
|
|
$annotation, |
90
|
|
|
string $fieldName, |
91
|
|
|
string $className, |
92
|
|
|
bool $convertFieldsOnRootLevel = false |
93
|
|
|
): ?MappingInterface { |
94
|
|
|
/** @var ?MappingInterface $fieldMapping */ |
|
|
|
|
95
|
6 |
|
$fieldMapping = null; |
96
|
|
|
|
97
|
6 |
|
if ($annotation instanceof Service) { |
98
|
5 |
|
$fieldMapping = new ServiceMapping( |
99
|
5 |
|
$annotation->id, |
100
|
5 |
|
$annotation->lax, |
101
|
5 |
|
sprintf( |
102
|
5 |
|
"in entity '%s' on field '%s'", |
103
|
5 |
|
$className, |
104
|
5 |
|
$fieldName |
105
|
|
|
) |
106
|
|
|
); |
107
|
|
|
|
108
|
6 |
|
} elseif ($annotation instanceof Choice) { |
109
|
|
|
/** @var string|null|ColumnAnnotation $column */ |
110
|
4 |
|
$column = $annotation->column; |
111
|
|
|
|
112
|
4 |
|
if (!$column instanceof ColumnAnnotation) { |
113
|
|
|
/** @var string $columnName */ |
114
|
4 |
|
$columnName = $fieldName; |
115
|
|
|
|
116
|
4 |
|
if (is_string($column)) { |
117
|
4 |
|
$columnName = $column; |
118
|
|
|
} |
119
|
|
|
|
120
|
4 |
|
$column = new ColumnAnnotation(); |
121
|
4 |
|
$column->name = $columnName; |
122
|
4 |
|
$column->type = 'string'; |
123
|
4 |
|
$column->length = 255; |
124
|
4 |
|
$column->precision = 10; |
125
|
4 |
|
$column->nullable = (bool)$annotation->nullable; |
126
|
|
|
} |
127
|
|
|
|
128
|
4 |
|
$dbalColumn = new DBALColumn( |
129
|
4 |
|
$column->name, |
130
|
4 |
|
Type::getType($column->type), |
131
|
|
|
[ |
132
|
4 |
|
'notnull' => !$column->nullable, |
133
|
4 |
|
'length' => $column->length, |
134
|
4 |
|
'precision' => $column->precision, |
135
|
4 |
|
'scale' => $column->scale, |
136
|
|
|
] |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
/** @var array<MappingInterface>*/ |
140
|
4 |
|
$choiceMappings = array(); |
141
|
|
|
|
142
|
4 |
|
foreach ($annotation->choices as $determinator => $choiceAnnotation) { |
143
|
|
|
/** @var ?MappingInterface $choiceMapping */ |
|
|
|
|
144
|
3 |
|
$choiceMapping = $this->convertAnnotationToMapping( |
145
|
3 |
|
$choiceAnnotation, |
146
|
3 |
|
$fieldName, |
147
|
3 |
|
$className |
148
|
|
|
); |
149
|
|
|
|
150
|
3 |
|
if ($choiceMapping instanceof MappingInterface) { |
151
|
3 |
|
$choiceMappings[$determinator] = $choiceMapping; |
152
|
|
|
|
153
|
|
|
} else { |
154
|
|
|
throw new InvalidMappingException(sprintf( |
155
|
|
|
"Invalid mapping on entity '%s' in field '%s' of choice-option '%s'!", |
156
|
|
|
$className, |
157
|
|
|
$fieldName, |
158
|
3 |
|
$determinator |
159
|
|
|
)); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
4 |
|
$fieldMapping = new ChoiceMapping( |
164
|
4 |
|
$dbalColumn, |
165
|
4 |
|
$choiceMappings, |
166
|
4 |
|
sprintf( |
167
|
4 |
|
"in entity '%s' on field '%s'", |
168
|
4 |
|
$className, |
169
|
4 |
|
$fieldName |
170
|
|
|
) |
171
|
|
|
); |
172
|
|
|
|
173
|
6 |
|
} elseif ($annotation instanceof Obj) { |
174
|
|
|
/** @var array<MappingInterface> $subFieldMappings */ |
175
|
2 |
|
$subFieldMappings = array(); |
176
|
|
|
|
177
|
|
|
/** @var null|CallDefinitionInterface $factory */ |
178
|
2 |
|
$factory = null; |
179
|
|
|
|
180
|
|
|
/** @var null|CallDefinitionInterface $serializer */ |
181
|
2 |
|
$serializer = null; |
182
|
|
|
|
183
|
2 |
|
foreach ($annotation->fields as $subFieldName => $subFieldAnnotation) { |
184
|
|
|
/** @var object $fieldAnnotation */ |
185
|
|
|
|
186
|
2 |
|
$subFieldMappings[$subFieldName] = $this->convertAnnotationToMapping( |
187
|
2 |
|
$subFieldAnnotation, |
188
|
2 |
|
$fieldName . "->" . $subFieldName, |
189
|
2 |
|
$className, |
190
|
2 |
|
true |
191
|
|
|
); |
192
|
|
|
} |
193
|
|
|
|
194
|
2 |
|
$fieldMapping = new ObjectMapping( |
195
|
2 |
|
$annotation->{"class"}, |
196
|
2 |
|
$subFieldMappings, |
197
|
2 |
|
sprintf( |
198
|
2 |
|
"in entity '%s' on field '%s'", |
199
|
2 |
|
$className, |
200
|
2 |
|
$fieldName |
201
|
|
|
), |
202
|
2 |
|
$factory, |
203
|
2 |
|
$serializer |
204
|
|
|
); |
205
|
|
|
|
206
|
6 |
|
} elseif ($annotation instanceof Arr) { |
207
|
|
|
/** @var array<MappingInterface> $entryMappings */ |
208
|
2 |
|
$entryMappings = array(); |
209
|
|
|
|
210
|
2 |
|
foreach ($annotation->entries as $key => $entryAnnotaton) { |
211
|
2 |
|
$entryMappings[$key] = $this->convertAnnotationToMapping( |
212
|
2 |
|
$entryAnnotaton, |
213
|
2 |
|
$fieldName . "->" . $key, |
214
|
2 |
|
$className, |
215
|
2 |
|
true |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
|
219
|
2 |
|
$fieldMapping = new ArrayMapping( |
220
|
2 |
|
$entryMappings, |
221
|
2 |
|
sprintf( |
222
|
2 |
|
"in entity '%s' on field '%s'", |
223
|
2 |
|
$className, |
224
|
2 |
|
$fieldName |
225
|
|
|
) |
226
|
|
|
); |
227
|
|
|
|
228
|
6 |
|
} elseif ($annotation instanceof ColumnAnnotation && $convertFieldsOnRootLevel) { |
229
|
|
|
/** @var DBALColumn $dbalColumn */ |
230
|
2 |
|
$dbalColumn = new DBALColumn( |
231
|
2 |
|
$annotation->name, |
232
|
2 |
|
Type::getType($annotation->type), |
233
|
|
|
[ |
234
|
2 |
|
'notnull' => !$annotation->nullable, |
235
|
2 |
|
'length' => $annotation->length, |
236
|
2 |
|
'precision' => $annotation->precision, |
237
|
2 |
|
'scale' => $annotation->scale, |
238
|
|
|
] |
239
|
|
|
); |
240
|
|
|
|
241
|
2 |
|
$fieldMapping = new FieldMapping( |
242
|
2 |
|
$dbalColumn, |
243
|
2 |
|
sprintf( |
244
|
2 |
|
"in entity '%s' on field '%s'", |
245
|
2 |
|
$className, |
246
|
2 |
|
$fieldName |
247
|
|
|
) |
248
|
|
|
); |
249
|
|
|
} |
250
|
|
|
|
251
|
6 |
|
return $fieldMapping; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
} |
255
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.