Completed
Push — master ( 8eabd5...5d0f28 )
by Guilh
08:14
created

SymfonySerializerAdapter::convertContext()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0416

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 16
ccs 10
cts 12
cp 0.8333
rs 9.4285
cc 3
eloc 10
nc 4
nop 1
crap 3.0416
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\Serializer;
13
14
use FOS\RestBundle\Context\Context;
15
use Symfony\Component\Serializer\SerializerInterface;
16
17
/**
18
 * Adapter to plug the Symfony serializer into the FOSRestBundle Serializer API.
19
 *
20
 * @author Christian Flothmann <[email protected]>
21
 */
22
class SymfonySerializerAdapter implements Serializer
23
{
24
    private $serializer;
25
26 18
    public function __construct(SerializerInterface $serializer)
27
    {
28 18
        $this->serializer = $serializer;
29 18
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 9
    public function serialize($data, $format, Context $context)
35
    {
36 9
        $newContext = $this->convertContext($context);
37 9
        $newContext['serializeNull'] = $context->getSerializeNull();
38
39 9
        return $this->serializer->serialize($data, $format, $newContext);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function deserialize($data, $type, $format, Context $context)
46
    {
47 2
        $newContext = $this->convertContext($context);
48
49 2
        return $this->serializer->deserialize($data, $type, $format, $newContext);
50
    }
51
52
    /**
53
     * @param Context $context
54
     */
55 11
    private function convertContext(Context $context)
56
    {
57 11
        $newContext = array();
58 11
        foreach ($context->getAttributes() as $key => $value) {
59 9
            $newContext[$key] = $value;
60 11
        }
61
62 11
        if (null !== $context->getGroups()) {
63
            $newContext['groups'] = $context->getGroups();
64
        }
65 11
        $newContext['version'] = $context->getVersion();
66 11
        $newContext['maxDepth'] = $context->getMaxDepth(false);
0 ignored issues
show
Deprecated Code introduced by
The method FOS\RestBundle\Context\Context::getMaxDepth() has been deprecated with message: since version 2.1, to be removed in 3.0. Use {@link self::isMaxDepthEnabled()} instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
67 11
        $newContext['enable_max_depth'] = $context->isMaxDepthEnabled();
68
69 11
        return $newContext;
70
    }
71
}
72