Passed
Push — master ( f3fa92...524f76 )
by Paul
02:03
created

JMSSerializerAdapter::mapContextAttributes()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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