1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hateoas\Serializer\Metadata; |
4
|
|
|
|
5
|
|
|
use Hateoas\Serializer\JMSSerializerMetadataAwareInterface; |
6
|
|
|
use JMS\Serializer\SerializationContext; |
7
|
|
|
use Metadata\MetadataFactory; |
8
|
|
|
use Metadata\MetadataFactoryInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Adrien Brault <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class InlineDeferrer implements JMSSerializerMetadataAwareInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var MetadataFactory |
17
|
|
|
*/ |
18
|
|
|
protected $serializerMetadataFactory; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \SplObjectStorage |
22
|
|
|
*/ |
23
|
|
|
protected $deferredData; |
24
|
|
|
|
25
|
|
|
public function __construct(MetadataFactory $serializerMetadataFactory = null) |
26
|
|
|
{ |
27
|
|
|
$this->serializerMetadataFactory = $serializerMetadataFactory; |
28
|
|
|
$this->deferredData = new \SplObjectStorage(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function setMetadataFactory(MetadataFactoryInterface $metadataFactory) |
35
|
|
|
{ |
36
|
|
|
$this->serializerMetadataFactory = $metadataFactory; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function handleItems($object, array $items, SerializationContext $context) |
40
|
|
|
{ |
41
|
|
|
if ($this->deferredData->contains($object)) { |
42
|
|
|
$items = array_merge($this->deferredData->offsetGet($object), $items); |
43
|
|
|
$this->deferredData->detach($object); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$parentObjectInlining = $this->getParentObjectInlining($object, $context); |
47
|
|
|
if (null === $parentObjectInlining) { |
48
|
|
|
return $items; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($this->deferredData->contains($parentObjectInlining)) { |
52
|
|
|
$items = array_merge($items, $this->deferredData->offsetGet($parentObjectInlining)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// We need to defer the links serialization to the $parentObject |
56
|
|
|
$this->deferredData->attach($parentObjectInlining, $items); |
57
|
|
|
|
58
|
|
|
return array(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function getParentObjectInlining($object, SerializationContext $context) |
62
|
|
|
{ |
63
|
|
|
$metadataStack = $context->getMetadataStack(); |
64
|
|
|
$visitingStack = $context->getVisitingStack(); |
65
|
|
|
|
66
|
|
|
$parentObject = null; |
67
|
|
|
if (count($visitingStack) > 0) { |
68
|
|
|
$parentObject = $visitingStack[0]; |
69
|
|
|
} |
70
|
|
|
if ($parentObject === $object && count($visitingStack) > 1) { |
71
|
|
|
$parentObject = $visitingStack[1]; // $object is inlined inside $parentObject |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ( |
75
|
|
|
$metadataStack->count() > 0 && isset($metadataStack[0]->inline) && $metadataStack[0]->inline |
76
|
|
|
&& $this->serializerMetadataFactory->getMetadataForClass(get_class($parentObject)) === $metadataStack[1] |
77
|
|
|
) { |
78
|
|
|
return $parentObject; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return null; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.