1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\Deserialization\Denormalizer\Relation\Doctrine; |
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\Collections\ArrayCollection; |
14
|
|
|
use Doctrine\Common\Persistence\Proxy; |
15
|
|
|
|
16
|
|
View Code Duplication |
final class EmbedManyFieldDenormalizer implements FieldDenormalizerInterface |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $class; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var AccessorInterface |
25
|
|
|
*/ |
26
|
|
|
private $accessor; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $class |
30
|
|
|
* @param AccessorInterface $accessor |
31
|
|
|
*/ |
32
|
7 |
|
public function __construct(string $class, AccessorInterface $accessor) |
33
|
|
|
{ |
34
|
7 |
|
$this->class = $class; |
35
|
7 |
|
$this->accessor = $accessor; |
36
|
7 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $path |
40
|
|
|
* @param object $object |
41
|
|
|
* @param mixed $value |
42
|
|
|
* @param DenormalizerContextInterface $context |
43
|
|
|
* @param DenormalizerInterface|null $denormalizer |
44
|
|
|
* |
45
|
|
|
* @throws DeserializerLogicException |
46
|
|
|
* @throws DeserializerRuntimeException |
47
|
|
|
*/ |
48
|
7 |
|
public function denormalizeField( |
49
|
|
|
string $path, |
50
|
|
|
$object, |
51
|
|
|
$value, |
52
|
|
|
DenormalizerContextInterface $context, |
53
|
|
|
DenormalizerInterface $denormalizer = null |
54
|
|
|
) { |
55
|
7 |
|
if (null === $value) { |
56
|
1 |
|
$this->accessor->setValue($object, $value); |
57
|
|
|
|
58
|
1 |
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
6 |
|
if (null === $denormalizer) { |
62
|
1 |
|
throw DeserializerLogicException::createMissingDenormalizer($path); |
63
|
|
|
} |
64
|
|
|
|
65
|
5 |
|
if (!is_array($value)) { |
66
|
1 |
|
throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array'); |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
$existEmbObjects = $this->accessor->getValue($object); |
70
|
|
|
|
71
|
4 |
|
$relatedObjects = new ArrayCollection(); |
72
|
4 |
|
foreach ($value as $i => $subValue) { |
73
|
4 |
|
$subPath = $path.'['.$i.']'; |
74
|
|
|
|
75
|
4 |
|
if (!is_array($subValue)) { |
76
|
1 |
|
throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'array'); |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
$relatedObject = $this->getRelatedObjectOrClass($existEmbObjects[$i] ?? null); |
80
|
|
|
|
81
|
3 |
|
$relatedObjects[$i] = $denormalizer->denormalize($relatedObject, $subValue, $context, $subPath); |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
$this->accessor->setValue($object, $relatedObjects); |
85
|
3 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param object|null $existEmbObject |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
3 |
|
private function getRelatedObjectOrClass($existEmbObject) |
93
|
|
|
{ |
94
|
3 |
|
if (null === $existEmbObject) { |
95
|
1 |
|
return $this->class; |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
$this->resolveProxy($existEmbObject); |
99
|
|
|
|
100
|
2 |
|
return $existEmbObject; |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
private function resolveProxy($relatedObject) |
104
|
|
|
{ |
105
|
2 |
|
if (null !== $relatedObject && $relatedObject instanceof Proxy && !$relatedObject->__isInitialized()) { |
106
|
1 |
|
$relatedObject->__load(); |
107
|
|
|
} |
108
|
2 |
|
} |
109
|
|
|
} |
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.