Passed
Push — master ( 973a5a...376082 )
by Paul
05:11
created

SymfonySerializerAdapter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 75.86%

Importance

Changes 0
Metric Value
dl 0
loc 108
ccs 22
cts 29
cp 0.7586
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A deserialize() 0 5 1
A __construct() 0 3 1
A toArray() 0 5 1
A fromArray() 0 5 1
A serialize() 0 8 2
A convertContext() 0 19 4
1
<?php
2
3
namespace CCT\Component\Rest\Serializer;
4
5
use CCT\Component\Rest\Serializer\Context\Context;
6
use Symfony\Component\Serializer\Serializer;
7
8
/**
9
 * Adapter to plug the Symfony serializer into the FOSRestBundle Serializer API.
10
 */
11
class SymfonySerializerAdapter implements SerializerInterface
12
{
13
    /**
14
     * @var Serializer
15
     */
16
    private $serializer;
17
18
    /**
19
     * SymfonySerializerAdapter constructor.
20
     *
21
     * @param Serializer $serializer
22
     */
23 4
    public function __construct(Serializer $serializer)
24
    {
25 4
        $this->serializer = $serializer;
26 4
    }
27
28
    /**
29
     * Serializes the given data to the specified output format.
30
     *
31
     * @param object|array $data
32
     * @param string $format
33
     * @param ContextInterface|null|Context $context
34
     *
35
     * @return string
36
     */
37 2
    public function serialize($data, $format, ContextInterface $context = null)
38
    {
39 2
        $newContext = $this->convertContext($context);
40 2
        if (null !== $context) {
41 2
            $newContext['serializeNull'] = $context->getSerializeNull();
42
        }
43
44 2
        return $this->serializer->serialize($data, $format, $newContext);
45
    }
46
47
    /**
48
     * @param string $data
49
     * @param string $type
50
     * @param string $format
51
     * @param ContextInterface|null|Context $context
52
     *
53
     * @return object
54
     */
55 2
    public function deserialize($data, $type, $format, ContextInterface $context = null)
56
    {
57 2
        $newContext = $this->convertContext($context);
58
59 2
        return $this->serializer->deserialize($data, $type, $format, $newContext);
60
    }
61
62
    /**
63
     * @param ContextInterface $context
64
     *
65
     * @return array|null
66
     */
67 4
    private function convertContext(ContextInterface $context = null)
68
    {
69 4
        if (null === $context) {
70
            return null;
71
        }
72
73 4
        $newContext = array();
74 4
        foreach ($context->getAttributes() as $key => $value) {
75 1
            $newContext[$key] = $value;
76
        }
77
78 4
        if (null !== $context->getGroups()) {
79 2
            $newContext['groups'] = $context->getGroups();
80
        }
81 4
        $newContext['version'] = $context->getVersion();
82 4
        $newContext['maxDepth'] = $context->getMaxDepth();
83 4
        $newContext['enable_max_depth'] = $context->isMaxDepthEnabled();
84
85 4
        return $newContext;
86
    }
87
88
    /**
89
     * Converts objects to an array structure.
90
     *
91
     * This is useful when the data needs to be passed on to other methods which expect array data.
92
     *
93
     * @param mixed $data anything that converts to an array, typically an object or an array of objects
94
     * @param ContextInterface|null $context
95
     *
96
     * @return array
97
     */
98
    public function toArray($data, ContextInterface $context = null)
99
    {
100
        $newContext = $this->convertContext($context);
101
102
        return $this->serializer->normalize($data, null, $newContext);
103
    }
104
105
    /**
106
     * Restores objects from an array structure.
107
     *
108
     * @param array $data
109
     * @param string $type
110
     * @param ContextInterface|null $context
111
     *
112
     * @return mixed this returns whatever the passed type is, typically an object or an array of objects
113
     */
114
    public function fromArray(array $data, $type, ContextInterface $context = null)
115
    {
116
        $newContext = $this->convertContext($context);
117
118
        return $this->serializer->denormalize($data, $type, null, $newContext);
119
    }
120
}
121