Passed
Push — master ( 237f5e...12d735 )
by Paul
02:15
created

JMSSerializerAdapter::mapGroups()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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
13
/**
14
 * Adapter to plug the JMS serializer into the FOSRestBundle Serializer API.
15
 */
16
class JMSSerializerAdapter implements SerializerInterface
17
{
18
    /**
19
     * @var Serializer
20
     */
21
    private $serializer;
22
23
    /**
24
     * @var SerializationContextFactoryInterface
25
     */
26
    private $serializationContextFactory;
27
28
    /**
29
     * @var DeserializationContextFactoryInterface
30
     */
31
    private $deserializationContextFactory;
32
33
    public function __construct(
34
        Serializer $serializer,
35
        SerializationContextFactoryInterface $serializationContextFactory = null,
36
        DeserializationContextFactoryInterface $deserializationContextFactory = null
37
    ) {
38
        $this->serializer = $serializer;
39
        $this->serializationContextFactory = $serializationContextFactory;
40
        $this->deserializationContextFactory = $deserializationContextFactory;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function serialize($data, $format, ContextInterface $context = null)
47
    {
48
        $context = $this->convertSerializationContext($context);
49
50
        return $this->serializer->serialize($data, $format, $context);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function deserialize($data, $type, $format, ContextInterface $context = null)
57
    {
58
        $context = $this->convertDeserializationContext($context);
59
60
        return $this->serializer->deserialize($data, $type, $format, $context);
61
    }
62
63
    /**
64
     * @param ContextInterface|Context $context
65
     *
66
     * @return JMSContext|SerializationContext
67
     */
68
    private function convertSerializationContext(ContextInterface $context = null)
69
    {
70
        if (null === $context) {
71
            return null;
72
        }
73
74
        return $this->mapContextAttributes($context, $this->createSerializationContext());
75
    }
76
77
    /**
78
     * @param ContextInterface|Context $context
79
     *
80
     * @return JMSContext|DeserializationContext
81
     */
82
    private function convertDeserializationContext(ContextInterface $context = null)
83
    {
84
        if (null === $context) {
85
            return null;
86
        }
87
        $deserializationContext = $this->createDeserializationContext();
88
89
        $this->mapMaxDepth($context, $deserializationContext);
90
91
        return $this->mapContextAttributes($context, $deserializationContext);
92
    }
93
94
    /**
95
     * Create JMS serialization context
96
     *
97
     * @return SerializationContext
98
     */
99
    private function createSerializationContext()
100
    {
101
        return $this->serializationContextFactory
102
            ? $this->serializationContextFactory->createSerializationContext()
103
            : SerializationContext::create();
104
    }
105
106
    /**
107
     * Create JMS deserialization context
108
     *
109
     * @return DeserializationContext
110
     */
111
    private function createDeserializationContext()
112
    {
113
        return $this->deserializationContextFactory
114
            ? $this->deserializationContextFactory->createDeserializationContext()
115
            : DeserializationContext::create();
116
    }
117
118
    /**
119
     * Set jms context attributes from context
120
     *
121
     * @param ContextInterface|Context $context
122
     * @param JMSContext $jmsContext
123
     *
124
     * @return JMSContext|DeserializationContext|SerializationContext
125
     */
126
    private function mapContextAttributes(ContextInterface $context, JMSContext $jmsContext)
127
    {
128
        foreach ($context->getAttributes() as $key => $value) {
129
            $jmsContext->attributes->set($key, $value);
130
        }
131
132
        $this->mapVersion($context, $jmsContext);
133
134
        $this->mapGroups($context, $jmsContext);
135
136
        $this->mapMaxDepthEnabled($context, $jmsContext);
137
138
        $this->mapSerializeNull($context, $jmsContext);
139
140
        $this->mapExclusionStrategies($context, $jmsContext);
141
142
        return $jmsContext;
143
    }
144
145
    /**
146
     * @param ContextInterface $context
147
     * @param DeserializationContext $jmsContext
148
     */
149
    private function mapMaxDepth(ContextInterface $context, DeserializationContext $jmsContext)
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
     * @param ContextInterface $context
163
     * @param JMSContext $jmsContext
164
     */
165
    private function mapVersion(ContextInterface $context, JMSContext $jmsContext)
166
    {
167
        if (null === $context->getVersion()) {
168
            return;
169
        }
170
171
        $jmsContext->setVersion($context->getVersion());
172
    }
173
174
    /**
175
     * @param ContextInterface $context
176
     * @param JMSContext $jmsContext
177
     */
178
    private function mapGroups(ContextInterface $context, JMSContext $jmsContext)
179
    {
180
        if (null === $context->getGroups()) {
181
            return;
182
        }
183
184
        $jmsContext->setGroups($context->getGroups());
185
    }
186
187
    /**
188
     * @param ContextInterface $context
189
     * @param JMSContext $jmsContext
190
     */
191
    private function mapMaxDepthEnabled(ContextInterface $context, JMSContext $jmsContext)
192
    {
193
        if (null === $context->isMaxDepthEnabled()) {
194
            return;
195
        }
196
197
        $jmsContext->enableMaxDepthChecks();
198
    }
199
200
    /**
201
     * @param ContextInterface $context
202
     * @param JMSContext $jmsContext
203
     */
204
    private function mapSerializeNull(ContextInterface $context, JMSContext $jmsContext)
205
    {
206
        if (null === $context->getSerializeNull()) {
207
            return;
208
        }
209
210
        $jmsContext->setSerializeNull($context->getSerializeNull());
211
    }
212
213
    /**
214
     * @param ContextInterface $context
215
     * @param JMSContext $jmsContext
216
     */
217
    private function mapExclusionStrategies(ContextInterface $context, JMSContext $jmsContext)
218
    {
219
        foreach ($context->getExclusionStrategies() as $strategy) {
220
            $jmsContext->addExclusionStrategy($strategy);
221
        }
222
    }
223
224
    /**
225
     * Converts objects to an array structure.
226
     *
227
     * This is useful when the data needs to be passed on to other methods which expect array data.
228
     *
229
     * @param mixed $data anything that converts to an array, typically an object or an array of objects
230
     * @param ContextInterface|null $context
231
     *
232
     * @return array
233
     */
234
    public function toArray($data, ContextInterface $context = null)
235
    {
236
        $context = $this->convertSerializationContext($context);
237
238
        return $this->serializer->toArray($data, $context);
239
    }
240
241
    /**
242
     * Restores objects from an array structure.
243
     *
244
     * @param array $data
245
     * @param string $type
246
     * @param ContextInterface|null $context
247
     *
248
     * @return mixed this returns whatever the passed type is, typically an object or an array of objects
249
     */
250
    public function fromArray(array $data, $type, ContextInterface $context = null)
251
    {
252
        $context = $this->convertDeserializationContext($context);
253
254
        return $this->serializer->fromArray($data, $context);
255
    }
256
}
257