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): ?string |
|||
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); |
|||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
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); |
|||
0 ignored issues
–
show
It seems like
$newContext can also be of type null ; however, parameter $context of Symfony\Component\Serial...rializer::deserialize() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
60 | } |
||||
61 | |||||
62 | /** |
||||
63 | * @param ContextInterface $context |
||||
64 | * |
||||
65 | * @return array|null |
||||
66 | */ |
||||
67 | 4 | private function convertContext(ContextInterface $context = null): ?array |
|||
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 | * @throws \Symfony\Component\Serializer\Exception\LogicException |
||||
98 | * @throws \Symfony\Component\Serializer\Exception\InvalidArgumentException |
||||
99 | * @throws \Symfony\Component\Serializer\Exception\CircularReferenceException |
||||
100 | */ |
||||
101 | public function toArray($data, ContextInterface $context = null): array |
||||
102 | { |
||||
103 | $newContext = $this->convertContext($context); |
||||
104 | |||||
105 | return $this->serializer->normalize($data, null, $newContext); |
||||
0 ignored issues
–
show
It seems like
$newContext can also be of type null ; however, parameter $context of Symfony\Component\Serial...Serializer::normalize() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
106 | } |
||||
107 | |||||
108 | /** |
||||
109 | * Restores objects from an array structure. |
||||
110 | * |
||||
111 | * @param array $data |
||||
112 | * @param string $type |
||||
113 | * @param ContextInterface|null $context |
||||
114 | * |
||||
115 | * @return mixed this returns whatever the passed type is, typically an object or an array of objects |
||||
116 | * @throws \Symfony\Component\Serializer\Exception\UnexpectedValueException |
||||
117 | * @throws \Symfony\Component\Serializer\Exception\RuntimeException |
||||
118 | * @throws \Symfony\Component\Serializer\Exception\NotNormalizableValueException |
||||
119 | * @throws \Symfony\Component\Serializer\Exception\LogicException |
||||
120 | * @throws \Symfony\Component\Serializer\Exception\InvalidArgumentException |
||||
121 | * @throws \Symfony\Component\Serializer\Exception\ExtraAttributesException |
||||
122 | * @throws \Symfony\Component\Serializer\Exception\BadMethodCallException |
||||
123 | */ |
||||
124 | public function fromArray(array $data, $type, ContextInterface $context = null) |
||||
125 | { |
||||
126 | $newContext = $this->convertContext($context); |
||||
127 | |||||
128 | return $this->serializer->denormalize($data, $type, null, $newContext); |
||||
0 ignored issues
–
show
It seems like
$newContext can also be of type null ; however, parameter $context of Symfony\Component\Serial...rializer::denormalize() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
129 | } |
||||
130 | } |
||||
131 |