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 as JMSSerializerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Adapter to plug the JMS serializer into the FOSRestBundle Serializer API. |
16
|
|
|
*/ |
17
|
|
|
class JMSSerializerAdapter implements SerializerInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @internal |
21
|
|
|
*/ |
22
|
|
|
const SERIALIZATION = 0; |
23
|
|
|
/** |
24
|
|
|
* @internal |
25
|
|
|
*/ |
26
|
|
|
const DESERIALIZATION = 1; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var JMSSerializerInterface|Serializer |
30
|
|
|
*/ |
31
|
|
|
private $serializer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var SerializationContextFactoryInterface |
35
|
|
|
*/ |
36
|
|
|
private $serializationContextFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var DeserializationContextFactoryInterface |
40
|
|
|
*/ |
41
|
|
|
private $deserializationContextFactory; |
42
|
|
|
|
43
|
|
|
public function __construct( |
44
|
|
|
JMSSerializerInterface $serializer, |
45
|
|
|
SerializationContextFactoryInterface $serializationContextFactory = null, |
46
|
|
|
DeserializationContextFactoryInterface $deserializationContextFactory = null |
47
|
|
|
) { |
48
|
|
|
$this->serializer = $serializer; |
49
|
|
|
$this->serializationContextFactory = $serializationContextFactory; |
50
|
|
|
$this->deserializationContextFactory = $deserializationContextFactory; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function serialize($data, $format, ContextInterface $context = null) |
57
|
|
|
{ |
58
|
|
|
$context = $this->convertContext($context, self::SERIALIZATION); |
59
|
|
|
|
60
|
|
|
return $this->serializer->serialize($data, $format, $context); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function deserialize($data, $type, $format, ContextInterface $context = null) |
67
|
|
|
{ |
68
|
|
|
$context = $this->convertContext($context, self::DESERIALIZATION); |
69
|
|
|
|
70
|
|
|
return $this->serializer->deserialize($data, $type, $format, $context); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param ContextInterface|Context $context |
75
|
|
|
* @param int $direction {@see self} constants |
76
|
|
|
* |
77
|
|
|
* @return JMSContext|DeserializationContext|SerializationContext |
78
|
|
|
*/ |
79
|
|
|
private function convertContext(ContextInterface $context = null, $direction = self::SERIALIZATION) |
80
|
|
|
{ |
81
|
|
|
if (null === $context) { |
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$jmsContext = self::SERIALIZATION === $direction |
86
|
|
|
? $this->createSerializationContext() |
87
|
|
|
: $this->createDeserializationContext(); |
88
|
|
|
|
89
|
|
|
return $this->mapContextAttributes($context, $jmsContext); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function createSerializationContext() |
93
|
|
|
{ |
94
|
|
|
return $this->serializationContextFactory |
95
|
|
|
? $this->serializationContextFactory->createSerializationContext() |
96
|
|
|
: SerializationContext::create(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function createDeserializationContext() |
100
|
|
|
{ |
101
|
|
|
return $jmsContext = $this->deserializationContextFactory |
|
|
|
|
102
|
|
|
? $this->deserializationContextFactory->createDeserializationContext() |
103
|
|
|
: DeserializationContext::create(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Set jms context attributes from context |
108
|
|
|
* |
109
|
|
|
* @param ContextInterface|Context $context |
110
|
|
|
* @param JMSContext $jmsContext |
111
|
|
|
* |
112
|
|
|
* @return JMSContext|DeserializationContext|SerializationContext |
113
|
|
|
*/ |
114
|
|
|
private function mapContextAttributes(ContextInterface $context, JMSContext $jmsContext) |
115
|
|
|
{ |
116
|
|
|
|
117
|
|
|
$this->mapMaxDepth($context, $jmsContext); |
118
|
|
|
foreach ($context->getAttributes() as $key => $value) { |
119
|
|
|
$jmsContext->attributes->set($key, $value); |
120
|
|
|
} |
121
|
|
|
if (null !== $context->getVersion()) { |
122
|
|
|
$jmsContext->setVersion($context->getVersion()); |
123
|
|
|
} |
124
|
|
|
if (null !== $context->getGroups()) { |
125
|
|
|
$jmsContext->setGroups($context->getGroups()); |
126
|
|
|
} |
127
|
|
|
if (null !== $context->isMaxDepthEnabled()) { |
128
|
|
|
$jmsContext->enableMaxDepthChecks(); |
129
|
|
|
} |
130
|
|
|
if (null !== $context->getSerializeNull()) { |
131
|
|
|
$jmsContext->setSerializeNull($context->getSerializeNull()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
foreach ($context->getExclusionStrategies() as $strategy) { |
135
|
|
|
$jmsContext->addExclusionStrategy($strategy); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $jmsContext; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param ContextInterface $context |
143
|
|
|
* @param JMSContext|DeserializationContext|SerializationContext $jmsContext |
144
|
|
|
*/ |
145
|
|
|
private function mapMaxDepth(ContextInterface $context, JMSContext $jmsContext) |
146
|
|
|
{ |
147
|
|
|
if ($jmsContext instanceof DeserializationContext) { |
148
|
|
|
return; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$maxDepth = $context->getMaxDepth(); |
152
|
|
|
if (null === $maxDepth) { |
153
|
|
|
return; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
for ($i = 0; $i < $maxDepth; ++$i) { |
157
|
|
|
$jmsContext->increaseDepth(); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Converts objects to an array structure. |
163
|
|
|
* |
164
|
|
|
* This is useful when the data needs to be passed on to other methods which expect array data. |
165
|
|
|
* |
166
|
|
|
* @param mixed $data anything that converts to an array, typically an object or an array of objects |
167
|
|
|
* @param ContextInterface|null $context |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
public function toArray($data, ContextInterface $context = null) |
172
|
|
|
{ |
173
|
|
|
$context = $this->convertContext($context, self::SERIALIZATION); |
174
|
|
|
|
175
|
|
|
return $this->serializer->toArray($data, $context); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Restores objects from an array structure. |
180
|
|
|
* |
181
|
|
|
* @param array $data |
182
|
|
|
* @param string $type |
183
|
|
|
* @param ContextInterface|null $context |
184
|
|
|
* |
185
|
|
|
* @return mixed this returns whatever the passed type is, typically an object or an array of objects |
186
|
|
|
*/ |
187
|
|
|
public function fromArray(array $data, $type, ContextInterface $context = null) |
188
|
|
|
{ |
189
|
|
|
$context = $this->convertContext($context, self::DESERIALIZATION); |
190
|
|
|
|
191
|
|
|
return $this->serializer->fromArray($data, $context); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|