|
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 JMS\Serializer\Context as JMSContext; |
|
16
|
|
|
use JMS\Serializer\DeserializationContext as JMSDeserializationContext; |
|
17
|
|
|
use JMS\Serializer\SerializationContext as JMSSerializationContext; |
|
18
|
|
|
use JMS\Serializer\SerializerInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Adapter to plug the JMS serializer into the FOSRestBundle Serializer API. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Christian Flothmann <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class JMSSerializerAdapter implements Serializer |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @internal |
|
29
|
|
|
*/ |
|
30
|
|
|
const SERIALIZATION = 0; |
|
31
|
|
|
/** |
|
32
|
|
|
* @internal |
|
33
|
|
|
*/ |
|
34
|
|
|
const DESERIALIZATION = 1; |
|
35
|
|
|
|
|
36
|
|
|
private $serializer; |
|
37
|
|
|
|
|
38
|
4 |
|
public function __construct(SerializerInterface $serializer) |
|
39
|
|
|
{ |
|
40
|
4 |
|
$this->serializer = $serializer; |
|
41
|
4 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
4 |
|
public function serialize($data, $format, Context $context) |
|
47
|
|
|
{ |
|
48
|
4 |
|
$context = $this->convertContext($context, self::SERIALIZATION); |
|
49
|
|
|
|
|
50
|
4 |
|
return $this->serializer->serialize($data, $format, $context); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function deserialize($data, $type, $format, Context $context) |
|
57
|
|
|
{ |
|
58
|
|
|
$context = $this->convertContext($context, self::DESERIALIZATION); |
|
59
|
|
|
|
|
60
|
|
|
return $this->serializer->deserialize($data, $type, $format, $context); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param Context $context |
|
65
|
|
|
* @param int $direction {@see self} constants |
|
66
|
|
|
* |
|
67
|
|
|
* @return JMSContext |
|
68
|
|
|
*/ |
|
69
|
4 |
|
private function convertContext(Context $context, $direction) |
|
70
|
|
|
{ |
|
71
|
4 |
|
if ($direction === self::SERIALIZATION) { |
|
72
|
4 |
|
$jmsContext = JMSSerializationContext::create(); |
|
73
|
4 |
|
} else { |
|
74
|
|
|
$jmsContext = JMSDeserializationContext::create(); |
|
75
|
|
|
$maxDepth = $context->getMaxDepth(); |
|
76
|
|
|
if (null !== $maxDepth) { |
|
77
|
|
|
for ($i = 0; $i < $maxDepth; ++$i) { |
|
78
|
|
|
$jmsContext->increaseDepth(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
4 |
|
foreach ($context->getAttributes() as $key => $value) { |
|
84
|
4 |
|
$jmsContext->attributes->set($key, $value); |
|
85
|
4 |
|
} |
|
86
|
|
|
|
|
87
|
4 |
|
if (null !== $context->getVersion()) { |
|
88
|
|
|
$jmsContext->setVersion($context->getVersion()); |
|
89
|
|
|
} |
|
90
|
4 |
|
$groups = $context->getGroups(); |
|
91
|
4 |
|
if (!empty($groups)) { |
|
92
|
|
|
$jmsContext->setGroups($context->getGroups()); |
|
93
|
|
|
} |
|
94
|
4 |
|
if (null !== $context->getMaxDepth()) { |
|
95
|
|
|
$jmsContext->enableMaxDepthChecks(); |
|
96
|
|
|
} |
|
97
|
4 |
|
if (null !== $context->getSerializeNull()) { |
|
98
|
4 |
|
$jmsContext->setSerializeNull($context->getSerializeNull()); |
|
99
|
4 |
|
} |
|
100
|
|
|
|
|
101
|
4 |
|
return $jmsContext; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.