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 ReferenceManyFieldDenormalizer implements FieldDenormalizerInterface |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var callable |
20
|
|
|
*/ |
21
|
|
|
private $repository; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var AccessorInterface |
25
|
|
|
*/ |
26
|
|
|
private $accessor; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param callable $repository |
30
|
|
|
* @param AccessorInterface $accessor |
31
|
|
|
*/ |
32
|
|
|
public function __construct(callable $repository, AccessorInterface $accessor) |
33
|
|
|
{ |
34
|
|
|
$this->repository = $repository; |
35
|
|
|
$this->accessor = $accessor; |
36
|
|
|
} |
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
|
|
|
public function denormalizeField( |
49
|
|
|
string $path, |
50
|
|
|
$object, |
51
|
|
|
$value, |
52
|
|
|
DenormalizerContextInterface $context, |
53
|
|
|
DenormalizerInterface $denormalizer = null |
54
|
|
|
) { |
55
|
|
|
if (null === $value) { |
56
|
|
|
$this->accessor->setValue($object, $value); |
57
|
|
|
|
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!is_array($value)) { |
62
|
|
|
throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$repository = $this->repository; |
66
|
|
|
|
67
|
|
|
$refObjects = new ArrayCollection(); |
68
|
|
|
foreach ($value as $i => $subValue) { |
69
|
|
|
$subPath = $path.'['.$i.']'; |
70
|
|
|
|
71
|
|
|
if (!is_string($subValue)) { |
72
|
|
|
throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'string'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$refObject = $repository($subValue); |
76
|
|
|
if (null !== $refObject && $refObject instanceof Proxy && !$refObject->__isInitialized()) { |
77
|
|
|
$refObject->__load(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$refObjects[$i] = $refObject; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->accessor->setValue($object, $refObjects); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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.