Issues (8)

src/Serializer/JMSSerializerAdapter.php (1 issue)

Labels
Severity
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 29
    public function __construct(
34
        Serializer $serializer,
35
        SerializationContextFactoryInterface $serializationContextFactory = null,
36
        DeserializationContextFactoryInterface $deserializationContextFactory = null
37
    ) {
38 29
        $this->serializer = $serializer;
39 29
        $this->serializationContextFactory = $serializationContextFactory;
40 29
        $this->deserializationContextFactory = $deserializationContextFactory;
41 29
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 3
    public function serialize($data, $format, ContextInterface $context = null): ?string
47
    {
48 3
        $context = $this->convertSerializationContext($context);
49
50 3
        return $this->serializer->serialize($data, $format, $context);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 8
    public function deserialize($data, $type, $format, ContextInterface $context = null)
57
    {
58 8
        $context = $this->convertDeserializationContext($context);
59
60 8
        return $this->serializer->deserialize($data, $type, $format, $context);
61
    }
62
63
    /**
64
     * @param ContextInterface|Context $context
65
     *
66
     * @return JMSContext|SerializationContext
67
     */
68 7
    private function convertSerializationContext(ContextInterface $context = null)
69
    {
70 7
        if (null === $context) {
71 3
            return null;
72
        }
73
74 4
        return $this->mapContextAttributes($context, $this->createSerializationContext());
75
    }
76
77
    /**
78
     * @param ContextInterface|Context $context
79
     *
80
     * @return JMSContext|DeserializationContext
81
     */
82 8
    private function convertDeserializationContext(ContextInterface $context = null)
83
    {
84 8
        if (null === $context) {
85 5
            return null;
86
        }
87 3
        $deserializationContext = $this->createDeserializationContext();
88
89 3
        $this->mapMaxDepth($context, $deserializationContext);
90
91 3
        return $this->mapContextAttributes($context, $deserializationContext);
92
    }
93
94
    /**
95
     * Create JMS serialization context
96
     *
97
     * @return SerializationContext
98
     */
99 4
    private function createSerializationContext(): SerializationContext
100
    {
101 4
        return $this->serializationContextFactory
102 2
            ? $this->serializationContextFactory->createSerializationContext()
103 4
            : SerializationContext::create();
104
    }
105
106
    /**
107
     * Create JMS deserialization context
108
     *
109
     * @return DeserializationContext
110
     */
111 3
    private function createDeserializationContext(): DeserializationContext
112
    {
113 3
        return $this->deserializationContextFactory
114 1
            ? $this->deserializationContextFactory->createDeserializationContext()
115 3
            : 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 7
    private function mapContextAttributes(ContextInterface $context, JMSContext $jmsContext)
127
    {
128 7
        foreach ($context->getAttributes() as $key => $value) {
129 1
            $jmsContext->attributes->set($key, $value);
130
        }
131
132 7
        $this->mapVersion($context, $jmsContext);
133
134 7
        $this->mapGroups($context, $jmsContext);
135
136 7
        $this->mapMaxDepthEnabled($context, $jmsContext);
137
138 7
        $this->mapSerializeNull($context, $jmsContext);
139
140 7
        $this->mapExclusionStrategies($context, $jmsContext);
141
142 7
        return $jmsContext;
143
    }
144
145
    /**
146
     * @param ContextInterface $context
147
     * @param DeserializationContext $jmsContext
148
     */
149 3
    private function mapMaxDepth(ContextInterface $context, DeserializationContext $jmsContext): void
150
    {
151 3
        $maxDepth = $context->getMaxDepth();
152 3
        if (null === $maxDepth) {
153 3
            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 7
    private function mapVersion(ContextInterface $context, JMSContext $jmsContext): void
166
    {
167 7
        if (null === $context->getVersion()) {
168 6
            return;
169
        }
170
171 1
        $jmsContext->setVersion($context->getVersion());
0 ignored issues
show
$context->getVersion() of type string is incompatible with the type integer expected by parameter $version of JMS\Serializer\Context::setVersion(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

171
        $jmsContext->setVersion(/** @scrutinizer ignore-type */ $context->getVersion());
Loading history...
172 1
    }
173
174
    /**
175
     * @param ContextInterface $context
176
     * @param JMSContext $jmsContext
177
     */
178 7
    private function mapGroups(ContextInterface $context, JMSContext $jmsContext): void
179
    {
180 7
        if (null === $context->getGroups()) {
181 4
            return;
182
        }
183
184 3
        $jmsContext->setGroups($context->getGroups());
185 3
    }
186
187
    /**
188
     * @param ContextInterface $context
189
     * @param JMSContext $jmsContext
190
     */
191 7
    private function mapMaxDepthEnabled(ContextInterface $context, JMSContext $jmsContext): void
192
    {
193 7
        if (null === $context->isMaxDepthEnabled()) {
194 6
            return;
195
        }
196
197 1
        $jmsContext->enableMaxDepthChecks();
198 1
    }
199
200
    /**
201
     * @param ContextInterface $context
202
     * @param JMSContext $jmsContext
203
     */
204 7
    private function mapSerializeNull(ContextInterface $context, JMSContext $jmsContext): void
205
    {
206 7
        if (null === $context->getSerializeNull()) {
207 6
            return;
208
        }
209
210 1
        $jmsContext->setSerializeNull($context->getSerializeNull());
211 1
    }
212
213
    /**
214
     * @param ContextInterface $context
215
     * @param JMSContext $jmsContext
216
     */
217 7
    private function mapExclusionStrategies(ContextInterface $context, JMSContext $jmsContext): void
218
    {
219 7
        foreach ($context->getExclusionStrategies() as $strategy) {
220 1
            $jmsContext->addExclusionStrategy($strategy);
221
        }
222 7
    }
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 4
    public function toArray($data, ContextInterface $context = null): array
235
    {
236 4
        $context = $this->convertSerializationContext($context);
237
238 4
        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