Completed
Push — master ( 1319dc...6e6413 )
by Lukas Kahwe
05:12
created

SymfonySerializerAdapter::convertContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 8
cts 9
cp 0.8889
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
crap 2.0054
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
final class SymfonySerializerAdapter implements Serializer
23
{
24
    private $serializer;
25
26 12
    public function __construct(SerializerInterface $serializer)
27
    {
28 12
        $this->serializer = $serializer;
29 12
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 7
    public function serialize($data, $format, Context $context)
35
    {
36 7
        $newContext = $this->convertContext($context);
37 7
        $newContext['serializeNull'] = $context->getSerializeNull();
38
39 7
        return $this->serializer->serialize($data, $format, $newContext);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    public function deserialize($data, $type, $format, Context $context)
46
    {
47 1
        $newContext = $this->convertContext($context);
48
49 1
        return $this->serializer->deserialize($data, $type, $format, $newContext);
50
    }
51
52
    /**
53
     * @param Context $context
54
     */
55 8
    private function convertContext(Context $context)
56
    {
57 8
        $newContext = array();
58 8
        foreach ($context->getAttributes() as $key => $value) {
59
            $newContext[$key] = $value;
60 8
        }
61
62 8
        $newContext['groups'] = $context->getGroups();
63 8
        $newContext['version'] = $context->getVersion();
64 8
        $newContext['maxDepth'] = $context->getMaxDepth();
65
66 8
        return $newContext;
67
    }
68
}
69