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

src/Serializer/SymfonySerializerAdapter.php (2 issues)

Labels
Severity
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
use Symfony\Component\Serializer\SerializerInterface as SymfonySerializerInterface;
8
9
/**
10
 * Adapter to plug the Symfony serializer into the FOSRestBundle Serializer API.
11
 */
12
class SymfonySerializerAdapter implements SerializerInterface
13
{
14
    /**
15
     * @var SymfonySerializerInterface|Serializer
16
     */
17
    private $serializer;
18
19
    /**
20
     * SymfonySerializerAdapter constructor.
21
     *
22
     * @param SymfonySerializerInterface $serializer
23
     */
24
    public function __construct(SymfonySerializerInterface $serializer)
25
    {
26
        $this->serializer = $serializer;
27
    }
28
29
    /**
30
     * Serializes the given data to the specified output format.
31
     *
32
     * @param object|array $data
33
     * @param string $format
34
     * @param ContextInterface|null|Context $context
35
     *
36
     * @return string
37
     */
38
    public function serialize($data, $format, ContextInterface $context = null)
39
    {
40
        $newContext = $this->convertContext($context);
41
        $newContext['serializeNull'] = $context->getSerializeNull();
0 ignored issues
show
The method getSerializeNull() does not exist on null. ( Ignorable by Annotation )

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

41
        /** @scrutinizer ignore-call */ 
42
        $newContext['serializeNull'] = $context->getSerializeNull();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
43
        return $this->serializer->serialize($data, $format, $newContext);
44
    }
45
46
    /**
47
     * @param string $data
48
     * @param string $type
49
     * @param string $format
50
     * @param ContextInterface|null|Context $context
51
     *
52
     * @return object
53
     */
54
    public function deserialize($data, $type, $format, ContextInterface $context = null)
55
    {
56
        $newContext = $this->convertContext($context);
57
58
        return $this->serializer->deserialize($data, $type, $format, $newContext);
59
    }
60
61
    /**
62
     * @param ContextInterface $context
63
     *
64
     * @return array|null
65
     */
66
    private function convertContext(ContextInterface $context = null)
67
    {
68
        if (null === $context) {
69
            return null;
70
        }
71
72
        $newContext = array();
73
        foreach ($context->getAttributes() as $key => $value) {
74
            $newContext[$key] = $value;
75
        }
76
77
        if (null !== $context->getGroups()) {
78
            $newContext['groups'] = $context->getGroups();
79
        }
80
        $newContext['version'] = $context->getVersion();
81
        $newContext['maxDepth'] = $context->getMaxDepth();
82
        $newContext['enable_max_depth'] = $context->isMaxDepthEnabled();
83
84
        return $newContext;
85
    }
86
87
    /**
88
     * Converts objects to an array structure.
89
     *
90
     * This is useful when the data needs to be passed on to other methods which expect array data.
91
     *
92
     * @param mixed $data anything that converts to an array, typically an object or an array of objects
93
     * @param ContextInterface|null $context
94
     *
95
     * @return array
96
     */
97
    public function toArray($data, ContextInterface $context = null)
98
    {
99
        $newContext = $this->convertContext($context);
100
101
        return $this->serializer->normalize($data, null, $newContext);
102
    }
103
104
    /**
105
     * Restores objects from an array structure.
106
     *
107
     * @param array $data
108
     * @param string $type
109
     * @param ContextInterface|null $context
110
     *
111
     * @return mixed this returns whatever the passed type is, typically an object or an array of objects
112
     */
113
    public function fromArray(array $data, $type, ContextInterface $context = null)
114
    {
115
        $newContext = $this->convertContext($context);
116
117
        return $this->serializer->denormalize($data, $type, null, $newContext);
0 ignored issues
show
The method denormalize() does not exist on Symfony\Component\Serializer\SerializerInterface. It seems like you code against a sub-type of Symfony\Component\Serializer\SerializerInterface such as Symfony\Component\Serializer\Serializer. ( Ignorable by Annotation )

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

117
        return $this->serializer->/** @scrutinizer ignore-call */ denormalize($data, $type, null, $newContext);
Loading history...
118
    }
119
}
120