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\DeserializerRuntimeException; |
12
|
|
|
use Doctrine\Common\Persistence\Proxy; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @deprecated use Basic or Doctrine ReferenceOneFieldDenormalizer |
16
|
|
|
*/ |
17
|
|
View Code Duplication |
final class ReferenceOneFieldDenormalizer implements FieldDenormalizerInterface |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var callable |
21
|
|
|
*/ |
22
|
|
|
private $repository; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var AccessorInterface |
26
|
|
|
*/ |
27
|
|
|
private $accessor; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param callable $repository |
31
|
|
|
* @param AccessorInterface $accessor |
32
|
|
|
*/ |
33
|
4 |
|
public function __construct(callable $repository, AccessorInterface $accessor) |
34
|
|
|
{ |
35
|
4 |
|
$this->repository = $repository; |
36
|
4 |
|
$this->accessor = $accessor; |
37
|
4 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $path |
41
|
|
|
* @param object $object |
42
|
|
|
* @param mixed $value |
43
|
|
|
* @param DenormalizerContextInterface $context |
44
|
|
|
* @param DenormalizerInterface|null $denormalizer |
45
|
|
|
* |
46
|
|
|
* @throws DeserializerRuntimeException |
47
|
|
|
*/ |
48
|
4 |
|
public function denormalizeField( |
49
|
|
|
string $path, |
50
|
|
|
$object, |
51
|
|
|
$value, |
52
|
|
|
DenormalizerContextInterface $context, |
53
|
|
|
DenormalizerInterface $denormalizer = null |
54
|
|
|
) { |
55
|
4 |
|
if (null === $value) { |
56
|
1 |
|
$this->accessor->setValue($object, $value); |
57
|
|
|
|
58
|
1 |
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
if (!is_string($value)) { |
62
|
1 |
|
throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'string'); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
$repository = $this->repository; |
66
|
|
|
|
67
|
2 |
|
$refObject = $repository($value); |
68
|
|
|
|
69
|
2 |
|
$this->resolveProxy($refObject); |
70
|
|
|
|
71
|
2 |
|
$this->accessor->setValue($object, $refObject); |
72
|
2 |
|
} |
73
|
|
|
|
74
|
2 |
|
private function resolveProxy($refObject) |
75
|
|
|
{ |
76
|
2 |
|
if (null !== $refObject && interface_exists('Doctrine\Common\Persistence\Proxy') |
77
|
2 |
|
&& $refObject instanceof Proxy && !$refObject->__isInitialized() |
78
|
|
|
) { |
79
|
1 |
|
$refObject->__load(); |
80
|
|
|
} |
81
|
2 |
|
} |
82
|
|
|
} |
83
|
|
|
|
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.