Passed
Push — master ( 58b0d3...f3fa92 )
by Paul
02:22
created

convertDeserializationContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
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
        if ($jmsContext instanceof DeserializationContext) {
129
            $this->mapMaxDepth($context, $jmsContext);
130
        }
131
132
        foreach ($context->getAttributes() as $key => $value) {
133
            $jmsContext->attributes->set($key, $value);
134
        }
135
        if (null !== $context->getVersion()) {
136
            $jmsContext->setVersion($context->getVersion());
137
        }
138
        if (null !== $context->getGroups()) {
139
            $jmsContext->setGroups($context->getGroups());
140
        }
141
        if (null !== $context->isMaxDepthEnabled()) {
142
            $jmsContext->enableMaxDepthChecks();
143
        }
144
        if (null !== $context->getSerializeNull()) {
145
            $jmsContext->setSerializeNull($context->getSerializeNull());
146
        }
147
148
        foreach ($context->getExclusionStrategies() as $strategy) {
149
            $jmsContext->addExclusionStrategy($strategy);
150
        }
151
152
        return $jmsContext;
153
    }
154
155
    /**
156
     * @param ContextInterface $context
157
     * @param DeserializationContext $jmsContext
158
     */
159
    private function mapMaxDepth(ContextInterface $context, DeserializationContext $jmsContext)
160
    {
161
        $maxDepth = $context->getMaxDepth();
162
        if (null === $maxDepth) {
163
            return;
164
        }
165
166
        for ($i = 0; $i < $maxDepth; ++$i) {
167
            $jmsContext->increaseDepth();
168
        }
169
    }
170
171
    /**
172
     * Converts objects to an array structure.
173
     *
174
     * This is useful when the data needs to be passed on to other methods which expect array data.
175
     *
176
     * @param mixed $data anything that converts to an array, typically an object or an array of objects
177
     * @param ContextInterface|null $context
178
     *
179
     * @return array
180
     */
181
    public function toArray($data, ContextInterface $context = null)
182
    {
183
        $context = $this->convertSerializationContext($context);
184
185
        return $this->serializer->toArray($data, $context);
186
    }
187
188
    /**
189
     * Restores objects from an array structure.
190
     *
191
     * @param array $data
192
     * @param string $type
193
     * @param ContextInterface|null $context
194
     *
195
     * @return mixed this returns whatever the passed type is, typically an object or an array of objects
196
     */
197
    public function fromArray(array $data, $type, ContextInterface $context = null)
198
    {
199
        $context = $this->convertDeserializationContext($context);
200
201
        return $this->serializer->fromArray($data, $context);
202
    }
203
}
204