|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CCT\Component\Rest\Serializer; |
|
4
|
|
|
|
|
5
|
|
|
use CCT\Component\Rest\Serializer\Context\Context; |
|
6
|
|
|
use JMS\Serializer\Context as JMSContext; |
|
7
|
|
|
use JMS\Serializer\ContextFactory\DeserializationContextFactoryInterface; |
|
8
|
|
|
use JMS\Serializer\ContextFactory\SerializationContextFactoryInterface; |
|
9
|
|
|
use JMS\Serializer\DeserializationContext; |
|
10
|
|
|
use JMS\Serializer\SerializationContext; |
|
11
|
|
|
use JMS\Serializer\Serializer; |
|
12
|
|
|
use JMS\Serializer\SerializerInterface; |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Adapter to plug the JMS serializer into the FOSRestBundle Serializer API. |
|
16
|
|
|
*/ |
|
17
|
|
|
class JMSSerializerAdapter implements \CCT\Component\Rest\Serializer\SerializerInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @internal |
|
21
|
|
|
*/ |
|
22
|
|
|
const SERIALIZATION = 0; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @internal |
|
26
|
|
|
*/ |
|
27
|
|
|
const DESERIALIZATION = 1; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var SerializerInterface|Serializer |
|
31
|
|
|
*/ |
|
32
|
|
|
private $serializer; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var SerializationContextFactoryInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $serializationContextFactory; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var DeserializationContextFactoryInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $deserializationContextFactory; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct( |
|
45
|
|
|
SerializerInterface $serializer, |
|
46
|
|
|
SerializationContextFactoryInterface $serializationContextFactory = null, |
|
47
|
|
|
DeserializationContextFactoryInterface $deserializationContextFactory = null |
|
48
|
|
|
) { |
|
49
|
|
|
$this->serializer = $serializer; |
|
50
|
|
|
$this->serializationContextFactory = $serializationContextFactory; |
|
51
|
|
|
$this->deserializationContextFactory = $deserializationContextFactory; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
public function serialize($data, $format, ContextInterface $context = null) |
|
58
|
|
|
{ |
|
59
|
|
|
$context = $this->convertContext($context, self::SERIALIZATION); |
|
60
|
|
|
|
|
61
|
|
|
return $this->serializer->serialize($data, $format, $context); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function deserialize($data, $type, $format, ContextInterface $context = null) |
|
68
|
|
|
{ |
|
69
|
|
|
if (null === $context) { |
|
70
|
|
|
$context = $this->deserializationContextFactory->createDeserializationContext(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$context = $this->convertContext($context, self::DESERIALIZATION); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
return $this->serializer->deserialize($data, $type, $format, $context); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param Context $context |
|
80
|
|
|
* @param int $direction {@see self} constants |
|
81
|
|
|
* |
|
82
|
|
|
* @return JMSContext |
|
83
|
|
|
*/ |
|
84
|
|
|
private function convertContext(Context $context = null, $direction) |
|
85
|
|
|
{ |
|
86
|
|
|
if (self::SERIALIZATION === $direction) { |
|
87
|
|
|
$jmsContext = $this->serializationContextFactory |
|
88
|
|
|
? $this->serializationContextFactory->createSerializationContext() |
|
89
|
|
|
: SerializationContext::create(); |
|
90
|
|
|
} else { |
|
91
|
|
|
$jmsContext = $this->deserializationContextFactory |
|
92
|
|
|
? $this->deserializationContextFactory->createDeserializationContext() |
|
93
|
|
|
: DeserializationContext::create(); |
|
94
|
|
|
$maxDepth = $context->getMaxDepth(false); |
|
|
|
|
|
|
95
|
|
|
if (null !== $maxDepth) { |
|
|
|
|
|
|
96
|
|
|
for ($i = 0; $i < $maxDepth; ++$i) { |
|
97
|
|
|
$jmsContext->increaseDepth(); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
foreach ($context->getAttributes() as $key => $value) { |
|
103
|
|
|
$jmsContext->attributes->set($key, $value); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if (null !== $context->getVersion()) { |
|
107
|
|
|
$jmsContext->setVersion($context->getVersion()); |
|
108
|
|
|
} |
|
109
|
|
|
if (null !== $context->getGroups()) { |
|
110
|
|
|
$jmsContext->setGroups($context->getGroups()); |
|
111
|
|
|
} |
|
112
|
|
|
if (null !== $context->isMaxDepthEnabled()) { |
|
|
|
|
|
|
113
|
|
|
$jmsContext->enableMaxDepthChecks(); |
|
114
|
|
|
} |
|
115
|
|
|
if (null !== $context->getSerializeNull()) { |
|
|
|
|
|
|
116
|
|
|
$jmsContext->setSerializeNull($context->getSerializeNull()); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
foreach ($context->getExclusionStrategies() as $strategy) { |
|
120
|
|
|
$jmsContext->addExclusionStrategy($strategy); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $jmsContext; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Converts objects to an array structure. |
|
128
|
|
|
* |
|
129
|
|
|
* This is useful when the data needs to be passed on to other methods which expect array data. |
|
130
|
|
|
* |
|
131
|
|
|
* @param mixed $data anything that converts to an array, typically an object or an array of objects |
|
132
|
|
|
* @param ContextInterface|null $context |
|
133
|
|
|
* |
|
134
|
|
|
* @return array |
|
135
|
|
|
*/ |
|
136
|
|
|
public function toArray($data, ContextInterface $context = null) |
|
137
|
|
|
{ |
|
138
|
|
|
$context = $this->convertContext($context, self::SERIALIZATION); |
|
139
|
|
|
|
|
140
|
|
|
$this->serializer->toArray($data, $context); |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Restores objects from an array structure. |
|
145
|
|
|
* |
|
146
|
|
|
* @param array $data |
|
147
|
|
|
* @param string $type |
|
148
|
|
|
* @param ContextInterface|null $context |
|
149
|
|
|
* |
|
150
|
|
|
* @return mixed this returns whatever the passed type is, typically an object or an array of objects |
|
151
|
|
|
*/ |
|
152
|
|
|
public function fromArray(array $data, $type, ContextInterface $context = null) |
|
153
|
|
|
{ |
|
154
|
|
|
$context = $this->convertContext($context, self::DESERIALIZATION); |
|
155
|
|
|
|
|
156
|
|
|
$this->serializer->fromArray($data, $context); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: