1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Entity\Hydrator; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Entity\Hydrator\Exception\FieldNotDeclaredException; |
6
|
|
|
use Bdf\Prime\Entity\Hydrator\Exception\InvalidTypeException; |
7
|
|
|
use Bdf\Prime\Entity\Hydrator\Exception\UninitializedPropertyException; |
8
|
|
|
use Bdf\Prime\Entity\Instantiator\InstantiatorInterface; |
9
|
|
|
use Bdf\Prime\Mapper\Metadata; |
10
|
|
|
use Bdf\Prime\Platform\PlatformTypesInterface; |
11
|
|
|
use Error; |
12
|
|
|
use ReflectionException; |
13
|
|
|
use ReflectionProperty; |
14
|
|
|
use stdClass; |
15
|
|
|
use TypeError; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Base implementation for @see MapperHydratorInterface |
19
|
|
|
* |
20
|
|
|
* Prefer use generated hydrators on production |
|
|
|
|
21
|
|
|
*/ |
22
|
|
|
class MapperHydrator implements MapperHydratorInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var InstantiatorInterface |
26
|
|
|
*/ |
27
|
|
|
protected $instantiator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Metadata |
31
|
|
|
*/ |
32
|
|
|
protected $metadata; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Property accessors, indexed by attribute name |
36
|
|
|
* |
37
|
|
|
* @var ReflectionProperty[][] |
38
|
|
|
*/ |
39
|
|
|
private $reflectionProperties = []; |
40
|
|
|
|
41
|
|
|
/** |
|
|
|
|
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
240 |
|
public function setPrimeInstantiator(InstantiatorInterface $instantiator) |
45
|
|
|
{ |
46
|
240 |
|
$this->instantiator = $instantiator; |
47
|
240 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
|
|
|
|
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
240 |
|
public function setPrimeMetadata(Metadata $metadata) |
53
|
|
|
{ |
54
|
240 |
|
$this->metadata = $metadata; |
55
|
240 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
|
|
|
|
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
432 |
|
public function flatExtract($object, array $attributes = null) |
61
|
|
|
{ |
62
|
432 |
|
$values = []; |
63
|
432 |
|
$cache = []; |
64
|
|
|
|
65
|
432 |
|
$attributes = $attributes === null |
|
|
|
|
66
|
426 |
|
? $this->metadata->attributes |
|
|
|
|
67
|
432 |
|
: array_intersect_key($this->metadata->attributes, $attributes); |
68
|
|
|
|
69
|
432 |
|
foreach ($attributes as $attribute => $metadata) { |
70
|
432 |
|
if (isset($metadata['embedded'])) { |
71
|
350 |
|
$path = $metadata['embedded']; |
72
|
|
|
|
73
|
350 |
|
if (empty($cache[$path])) { |
74
|
350 |
|
$cache[$path] = $this->getOwnerObject($object, $metadata); |
75
|
|
|
} |
76
|
|
|
|
77
|
350 |
|
$value = $cache[$path] ? $this->readFromAttribute($cache[$path], $metadata) : null; |
|
|
|
|
78
|
|
|
} else { |
79
|
432 |
|
$value = $this->readFromAttribute($object, $metadata); |
80
|
|
|
} |
81
|
|
|
|
82
|
432 |
|
$values[$attribute] = $value; |
83
|
|
|
} |
84
|
|
|
|
85
|
432 |
|
return $values; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
|
|
|
|
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
288 |
|
public function flatHydrate($object, array $data, PlatformTypesInterface $types) |
|
|
|
|
92
|
|
|
{ |
93
|
288 |
|
$metadata = $this->metadata->fields; |
|
|
|
|
94
|
288 |
|
$embeddeds = $this->metadata->embeddeds; |
95
|
|
|
$cacheEmbedded = [ |
96
|
288 |
|
'root' => $object |
|
|
|
|
97
|
|
|
]; |
98
|
|
|
|
99
|
288 |
|
foreach ($data as $field => $value) { |
100
|
288 |
|
if (!isset($metadata[$field])) { |
101
|
1 |
|
continue; |
102
|
|
|
} |
103
|
|
|
|
104
|
288 |
|
$value = $types->get($metadata[$field]['type'])->fromDatabase($value, $metadata[$field]['phpOptions']); |
105
|
|
|
|
106
|
288 |
|
if (isset($metadata[$field]['embedded'])) { |
107
|
205 |
|
$path = $metadata[$field]['embedded']; |
108
|
|
|
|
109
|
|
|
//creation du cache d'objet. Le but est de parcourir les paths de l'embedded |
|
|
|
|
110
|
|
|
//de creer les objets et de les associés entre eux |
|
|
|
|
111
|
|
|
//ex: |
|
|
|
|
112
|
|
|
// 'root.wrapper.offer.id' id est embedded dans offer qui est embedded dans wrapper, etc... |
|
|
|
|
113
|
|
|
// l'attribute id peut etre parsé sans que wrapper ne soit déjà construit (parsqu'il n'a pas d'attribut, |
|
|
|
|
114
|
|
|
// ou parce qu'il est définit avant dans le select) |
|
|
|
|
115
|
205 |
|
if (empty($cacheEmbedded[$path])) { |
116
|
205 |
|
for ($i = 0, $l = count($embeddeds[$path]['paths']); $i < $l; $i++) { |
117
|
205 |
|
$pathCursor = $embeddeds[$path]['paths'][$i]; |
118
|
|
|
|
119
|
205 |
|
if (empty($cacheEmbedded[$pathCursor])) { |
120
|
205 |
|
$parentMeta = $embeddeds[$pathCursor]; |
121
|
|
|
|
122
|
205 |
|
if (empty($parentMeta['polymorph'])) { |
123
|
205 |
|
$cacheEmbedded[$pathCursor] = $this->instantiator->instantiate($parentMeta['class'], $parentMeta['hint']); |
124
|
|
|
} else { |
125
|
1 |
|
$cacheEmbedded[$pathCursor] = $this->instantiator->instantiate( |
126
|
1 |
|
$className = $parentMeta['class_map'][$data[$parentMeta['discriminator_field']]], |
|
|
|
|
127
|
1 |
|
$parentMeta['hints'][$className] |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
205 |
|
$this->writeToEmbedded( |
132
|
205 |
|
$cacheEmbedded[$parentMeta['parentPath']], |
133
|
205 |
|
$parentMeta, |
134
|
205 |
|
$cacheEmbedded[$parentMeta['path']], |
135
|
205 |
|
true |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
} |
|
|
|
|
139
|
|
|
} |
|
|
|
|
140
|
|
|
|
141
|
205 |
|
$this->writeToAttribute($cacheEmbedded[$path], $metadata[$field], $value, true); |
142
|
|
|
} else { |
143
|
288 |
|
$this->writeToAttribute($object, $metadata[$field], $value, true); |
144
|
|
|
} |
|
|
|
|
145
|
|
|
} |
|
|
|
|
146
|
288 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
|
|
|
|
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
|
|
|
|
151
|
261 |
|
public function extractOne($object, $attribute) |
152
|
|
|
{ |
153
|
261 |
|
if (!isset($this->metadata->attributes[$attribute])) { |
154
|
38 |
|
if (!isset($this->metadata->embeddeds[$attribute])) { |
155
|
2 |
|
throw new FieldNotDeclaredException($this->metadata->entityClass, $attribute); |
156
|
|
|
} |
157
|
|
|
|
158
|
36 |
|
return $this->readFromEmbedded($object, $this->metadata->embeddeds[$attribute]); |
159
|
|
|
} else { |
160
|
256 |
|
$ownerObject = $this->getOwnerObject($object, $this->metadata->attributes[$attribute]); |
161
|
|
|
|
162
|
|
|
// Polymorphic embedded not instantiated |
163
|
256 |
|
if ($ownerObject === null) { |
164
|
1 |
|
return null; |
165
|
|
|
} |
166
|
|
|
|
167
|
256 |
|
return $this->readFromAttribute($ownerObject, $this->metadata->attributes[$attribute]); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
|
|
|
|
172
|
|
|
* {@inheritdoc} |
173
|
|
|
*/ |
|
|
|
|
174
|
224 |
|
public function hydrateOne($object, $attribute, $value) |
175
|
|
|
{ |
176
|
224 |
|
if (!isset($this->metadata->attributes[$attribute])) { |
177
|
113 |
|
if (!isset($this->metadata->embeddeds[$attribute])) { |
178
|
2 |
|
throw new FieldNotDeclaredException($this->metadata->entityClass, $attribute); |
179
|
|
|
} |
180
|
|
|
|
181
|
111 |
|
$this->writeToEmbedded($object, $this->metadata->embeddeds[$attribute], $value, false); |
182
|
|
|
} else { |
183
|
118 |
|
$ownerObject = $this->getOwnerObject($object, $this->metadata->attributes[$attribute]); |
184
|
|
|
|
185
|
|
|
// Polymorphic embedded not instantiated |
186
|
118 |
|
if ($ownerObject === null) { |
187
|
1 |
|
throw new \InvalidArgumentException('Cannot write to attribute '.$attribute.' : the embedded entity cannot be resolved'); |
188
|
|
|
} |
189
|
|
|
|
190
|
117 |
|
$this->writeToAttribute($ownerObject, $this->metadata->attributes[$attribute], $value, false); |
191
|
|
|
} |
192
|
221 |
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get owner object attribute |
196
|
|
|
* |
197
|
|
|
* @param object $entity |
198
|
|
|
* @param array $metadata Metadata d'un attribut a retrouver |
199
|
|
|
* |
200
|
|
|
* @return object|null The object, or null if cannot be instantiated (polymorph) |
201
|
|
|
*/ |
202
|
443 |
|
protected function getOwnerObject($entity, array $metadata) |
203
|
|
|
{ |
204
|
443 |
|
if (!isset($metadata['embedded'])) { |
205
|
311 |
|
return $entity; |
206
|
|
|
} |
207
|
|
|
|
208
|
363 |
|
$embeddeds = $this->metadata->embeddeds; |
209
|
363 |
|
$current = $entity; |
210
|
363 |
|
$embeddedMeta = $embeddeds[$metadata['embedded']]; |
211
|
363 |
|
$embedded = null; |
212
|
|
|
|
213
|
|
|
//parcourt des paths pour descendre juqu'à l'objet propriétaire des meta données |
|
|
|
|
214
|
|
|
//si l'objet n'existe pas, on le créé avant de l'associer à son object parent |
|
|
|
|
215
|
363 |
|
for ($i = 0, $l = count($embeddedMeta['paths']); $i < $l; $i++) { |
216
|
363 |
|
$parentMeta = $embeddeds[$embeddedMeta['paths'][$i]]; |
217
|
|
|
|
218
|
|
|
try { |
219
|
363 |
|
$embedded = $this->readFromEmbedded($current, $parentMeta); |
220
|
|
|
} catch (UninitializedPropertyException $e) { // The property is not initialized |
|
|
|
|
221
|
|
|
$embedded = null; |
222
|
|
|
} |
223
|
|
|
|
224
|
363 |
|
if ($embedded === null) { |
225
|
214 |
|
if (!isset($parentMeta['class'])) { |
226
|
3 |
|
return null; |
227
|
|
|
} |
228
|
|
|
|
229
|
212 |
|
$embedded = $this->instantiator->instantiate($parentMeta['class'], $parentMeta['hint']); |
230
|
|
|
|
231
|
|
|
// This writes should never fail : $embedded is not null |
232
|
212 |
|
$this->writeToEmbedded($current, $parentMeta, $embedded, false); |
233
|
|
|
} |
234
|
|
|
|
235
|
362 |
|
$current = $embedded; |
236
|
|
|
} |
|
|
|
|
237
|
|
|
|
238
|
362 |
|
return $embedded; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Read a value from an entity, with attribute metadata |
243
|
|
|
* |
244
|
|
|
* @param object $entity |
245
|
|
|
* @param array $metadata |
246
|
|
|
* |
247
|
|
|
* @return mixed |
248
|
|
|
* |
249
|
|
|
* @throws ReflectionException When property do not exist on the object |
|
|
|
|
250
|
|
|
* @throws UninitializedPropertyException When the property is not initialized |
|
|
|
|
251
|
|
|
*/ |
|
|
|
|
252
|
452 |
|
protected function readFromAttribute($entity, array $metadata) |
253
|
|
|
{ |
254
|
452 |
|
$attribute = $metadata['attribute']; |
255
|
452 |
|
$class = get_class($entity); |
256
|
|
|
|
257
|
452 |
|
if (isset($this->reflectionProperties[$class][$attribute])) { |
258
|
|
|
try { |
259
|
429 |
|
return $this->reflectionProperties[$class][$attribute]->getValue($entity); |
260
|
|
|
} catch (Error $e) { |
261
|
|
|
throw new UninitializedPropertyException($class, $this->reflectionProperties[$class][$attribute]->name, $e); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
105 |
|
if (!isset($metadata['embedded'])) { |
266
|
102 |
|
$property = $attribute; |
267
|
|
|
} else { |
268
|
72 |
|
$property = substr($attribute, strlen($metadata['embedded']) + 1); |
|
|
|
|
269
|
|
|
} |
270
|
|
|
|
271
|
105 |
|
return $this->readFromProperty($class, $property, $attribute, $entity); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Read a value from an entity, with embedded metadata |
276
|
|
|
* |
277
|
|
|
* @param object $entity |
278
|
|
|
* @param array $metadata |
279
|
|
|
* |
280
|
|
|
* @return mixed |
281
|
|
|
* |
282
|
|
|
* @throws ReflectionException When property do not exist on the object |
|
|
|
|
283
|
|
|
* @throws UninitializedPropertyException When the property is not initialized |
|
|
|
|
284
|
|
|
*/ |
|
|
|
|
285
|
373 |
|
protected function readFromEmbedded($entity, array $metadata) |
286
|
|
|
{ |
287
|
373 |
|
$attribute = $metadata['path']; |
288
|
373 |
|
$class = get_class($entity); |
289
|
|
|
|
290
|
373 |
|
if (isset($this->reflectionProperties[$class][$attribute])) { |
291
|
|
|
try { |
292
|
345 |
|
return $this->reflectionProperties[$class][$attribute]->getValue($entity); |
293
|
|
|
} catch (Error $e) { |
294
|
|
|
throw new UninitializedPropertyException($class, $this->reflectionProperties[$class][$attribute]->name, $e); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
298
|
76 |
|
if ($metadata['parentPath'] === 'root') { |
299
|
76 |
|
$property = $attribute; |
300
|
|
|
} else { |
301
|
9 |
|
$property = substr($attribute, strlen($metadata['parentPath']) + 1); |
|
|
|
|
302
|
|
|
} |
303
|
|
|
|
304
|
76 |
|
return $this->readFromProperty($class, $property, $attribute, $entity); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
|
|
|
|
308
|
|
|
* @param object $entity |
309
|
|
|
* @param array $metadata |
310
|
|
|
* @param mixed $value |
311
|
|
|
*/ |
312
|
338 |
|
protected function writeToAttribute($entity, array $metadata, $value, bool $skipInvalid) |
313
|
|
|
{ |
314
|
338 |
|
$attribute = $metadata['attribute']; |
315
|
338 |
|
$class = get_class($entity); |
316
|
|
|
|
317
|
338 |
|
if (isset($this->reflectionProperties[$class][$attribute])) { |
318
|
317 |
|
$this->writeToReflection($this->reflectionProperties[$class][$attribute], $entity, $value, $skipInvalid, $metadata); |
319
|
317 |
|
return; |
320
|
|
|
} |
321
|
|
|
|
322
|
40 |
|
if (!isset($metadata['embedded'])) { |
323
|
34 |
|
$property = $attribute; |
324
|
|
|
} else { |
325
|
15 |
|
$property = substr($attribute, strlen($metadata['embedded']) + 1); |
|
|
|
|
326
|
|
|
} |
327
|
|
|
|
328
|
40 |
|
if ($class === stdClass::class) { |
329
|
5 |
|
$entity->{$property} = $value; |
330
|
5 |
|
return; |
331
|
|
|
} |
332
|
|
|
|
333
|
35 |
|
$this->reflectionProperties[$class][$attribute] = $reflectionProperty = new ReflectionProperty($class, $property); |
|
|
|
|
334
|
35 |
|
$reflectionProperty->setAccessible(true); |
335
|
|
|
|
336
|
35 |
|
$this->writeToReflection($reflectionProperty, $entity, $value, $skipInvalid, $metadata); |
337
|
35 |
|
} |
338
|
|
|
|
339
|
|
|
/** |
|
|
|
|
340
|
|
|
* @param object $entity |
341
|
|
|
* @param array $metadata |
342
|
|
|
* @param mixed $value |
343
|
|
|
*/ |
344
|
322 |
|
protected function writeToEmbedded($entity, array $metadata, $value, bool $skipInvalid) |
345
|
|
|
{ |
346
|
322 |
|
$attribute = $metadata['path']; |
347
|
322 |
|
$class = get_class($entity); |
348
|
|
|
|
349
|
322 |
|
if (isset($this->reflectionProperties[$class][$attribute])) { |
350
|
311 |
|
$this->writeToReflection($this->reflectionProperties[$class][$attribute], $entity, $value, $skipInvalid, $metadata); |
351
|
311 |
|
return; |
352
|
|
|
} |
353
|
|
|
|
354
|
32 |
|
if ($metadata['parentPath'] === 'root') { |
355
|
28 |
|
$property = $attribute; |
356
|
|
|
} else { |
357
|
5 |
|
$property = substr($attribute, strlen($metadata['parentPath']) + 1); |
|
|
|
|
358
|
|
|
} |
359
|
|
|
|
360
|
32 |
|
if ($class === stdClass::class) { |
361
|
3 |
|
$entity->{$property} = $value; |
362
|
3 |
|
return; |
363
|
|
|
} |
364
|
|
|
|
365
|
29 |
|
$this->reflectionProperties[$class][$attribute] = $reflectionProperty = new ReflectionProperty($class, $property); |
|
|
|
|
366
|
29 |
|
$reflectionProperty->setAccessible(true); |
367
|
|
|
|
368
|
29 |
|
$this->writeToReflection($reflectionProperty, $entity, $value, $skipInvalid, $metadata); |
369
|
29 |
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Simple property read |
373
|
|
|
* |
374
|
|
|
* @param class-string $class |
|
|
|
|
375
|
|
|
* @param string $property |
376
|
|
|
* @param string $attribute |
377
|
|
|
* @param object $entity |
378
|
|
|
* @return mixed|null |
|
|
|
|
379
|
|
|
* |
380
|
|
|
* @throws ReflectionException |
381
|
|
|
* @throws UninitializedPropertyException |
382
|
|
|
*/ |
|
|
|
|
383
|
109 |
|
private function readFromProperty(string $class, string $property, string $attribute, $entity) |
|
|
|
|
384
|
|
|
{ |
385
|
109 |
|
if ($class === stdClass::class) { |
386
|
9 |
|
return $entity->$property ?? null; |
|
|
|
|
387
|
|
|
} |
388
|
|
|
|
389
|
104 |
|
$this->reflectionProperties[$class][$attribute] = $propertyReflection = new ReflectionProperty($class, $property); |
|
|
|
|
390
|
104 |
|
$propertyReflection->setAccessible(true); |
391
|
|
|
|
392
|
|
|
try { |
393
|
104 |
|
return $propertyReflection->getValue($entity); |
394
|
|
|
} catch (Error $e) { |
395
|
|
|
throw new UninitializedPropertyException($class, $property, $e); |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Check if the value should not be hydrated, in case of null value on not nullable property |
401
|
|
|
* |
402
|
|
|
* @param ReflectionProperty $property |
403
|
|
|
* @param mixed $value |
404
|
|
|
* |
405
|
|
|
* @return bool |
|
|
|
|
406
|
|
|
*/ |
407
|
284 |
|
private function shouldSkipValue(ReflectionProperty $property, $value): bool |
|
|
|
|
408
|
|
|
{ |
409
|
284 |
|
if (PHP_VERSION_ID < 70400 || $value !== null) { |
410
|
284 |
|
return false; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
return $property->hasType() && !$property->getType()->allowsNull(); |
|
|
|
|
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* @param ReflectionProperty $reflectionProperty |
418
|
|
|
* @param object $entity |
419
|
|
|
* @param mixed $value |
420
|
|
|
* @param bool $skipInvalid |
|
|
|
|
421
|
|
|
* @param array $metadata |
422
|
|
|
* |
423
|
|
|
* @throws InvalidTypeException |
424
|
|
|
*/ |
425
|
384 |
|
private function writeToReflection(ReflectionProperty $reflectionProperty, $entity, $value, bool $skipInvalid, array $metadata) |
|
|
|
|
426
|
|
|
{ |
427
|
384 |
|
if (!$skipInvalid || !$this->shouldSkipValue($reflectionProperty, $value)) { |
428
|
|
|
try { |
429
|
384 |
|
$reflectionProperty->setValue($entity, $value); |
430
|
|
|
} catch (TypeError $e) { |
431
|
|
|
throw new InvalidTypeException($e, $metadata['type'] ?? null); |
|
|
|
|
432
|
|
|
} |
433
|
|
|
} |
434
|
384 |
|
} |
435
|
|
|
} |
436
|
|
|
|