1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\Deserialization\Denormalizer\Relation; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\Deserialization\Accessor\AccessorInterface; |
8
|
|
|
use Chubbyphp\Deserialization\Denormalizer\DenormalizerContextInterface; |
9
|
|
|
use Chubbyphp\Deserialization\Denormalizer\DenormalizerInterface; |
10
|
|
|
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface; |
11
|
|
|
use Chubbyphp\Deserialization\DeserializerLogicException; |
12
|
|
|
use Chubbyphp\Deserialization\DeserializerRuntimeException; |
13
|
|
|
use Doctrine\Common\Persistence\Proxy; |
14
|
|
|
|
15
|
|
|
final class EmbedManyFieldDenormalizer implements FieldDenormalizerInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $class; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var AccessorInterface |
24
|
|
|
*/ |
25
|
|
|
private $accessor; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $class |
29
|
|
|
* @param AccessorInterface $accessor |
30
|
|
|
*/ |
31
|
7 |
|
public function __construct(string $class, AccessorInterface $accessor) |
32
|
|
|
{ |
33
|
7 |
|
$this->class = $class; |
34
|
7 |
|
$this->accessor = $accessor; |
35
|
7 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $path |
39
|
|
|
* @param object $object |
40
|
|
|
* @param mixed $value |
41
|
|
|
* @param DenormalizerContextInterface $context |
42
|
|
|
* @param DenormalizerInterface|null $denormalizer |
43
|
|
|
* |
44
|
|
|
* @throws DeserializerLogicException |
45
|
|
|
* @throws DeserializerRuntimeException |
46
|
|
|
*/ |
47
|
7 |
|
public function denormalizeField( |
48
|
|
|
string $path, |
49
|
|
|
$object, |
50
|
|
|
$value, |
51
|
|
|
DenormalizerContextInterface $context, |
52
|
|
|
DenormalizerInterface $denormalizer = null |
53
|
|
|
) { |
54
|
7 |
|
if (null === $value) { |
55
|
1 |
|
$this->accessor->setValue($object, $value); |
56
|
|
|
|
57
|
1 |
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
6 |
|
if (null === $denormalizer) { |
61
|
1 |
|
throw DeserializerLogicException::createMissingDenormalizer($path); |
62
|
|
|
} |
63
|
|
|
|
64
|
5 |
|
if (!is_array($value)) { |
65
|
1 |
|
throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array'); |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
$existingEmbeddedObjects = $this->accessor->getValue($object) ?? []; |
69
|
|
|
|
70
|
4 |
|
$embeddedObjects = []; |
71
|
4 |
|
foreach ($value as $i => $subValue) { |
72
|
4 |
|
$subPath = $path.'['.$i.']'; |
73
|
|
|
|
74
|
4 |
|
if (!is_array($subValue)) { |
75
|
1 |
|
throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'array'); |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
$embeddedObject = $this->getEmbeddedObjectOrClass($i, $existingEmbeddedObjects); |
79
|
|
|
|
80
|
3 |
|
$embeddedObjects[$i] = $denormalizer->denormalize($embeddedObject, $subValue, $context, $subPath); |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
$this->accessor->setValue($object, $embeddedObjects); |
84
|
3 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string|int $i |
88
|
|
|
* @param array|\Traversable $existingEmbeddedObjects |
89
|
|
|
* |
90
|
|
|
* @return object|string |
91
|
|
|
*/ |
92
|
3 |
|
private function getEmbeddedObjectOrClass($i, $existingEmbeddedObjects) |
93
|
|
|
{ |
94
|
3 |
|
if (isset($existingEmbeddedObjects[$i])) { |
95
|
2 |
|
$embeddedObject = $existingEmbeddedObjects[$i]; |
96
|
|
|
|
97
|
2 |
|
if (interface_exists('Doctrine\Common\Persistence\Proxy') |
98
|
2 |
|
&& $embeddedObject instanceof Proxy && !$embeddedObject->__isInitialized() |
99
|
|
|
) { |
100
|
1 |
|
$embeddedObject->__load(); |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
return $embeddedObject; |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return $this->class; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|