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\ContextFactory\DeserializationContextFactoryInterface; |
17
|
|
|
use JMS\Serializer\ContextFactory\SerializationContextFactoryInterface; |
18
|
|
|
use JMS\Serializer\DeserializationContext; |
19
|
|
|
use JMS\Serializer\SerializationContext; |
20
|
|
|
use JMS\Serializer\SerializerInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Adapter to plug the JMS serializer into the FOSRestBundle Serializer API. |
24
|
|
|
* |
25
|
|
|
* @author Christian Flothmann <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class JMSSerializerAdapter implements Serializer |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @internal |
31
|
|
|
*/ |
32
|
|
|
const SERIALIZATION = 0; |
33
|
|
|
/** |
34
|
|
|
* @internal |
35
|
|
|
*/ |
36
|
|
|
const DESERIALIZATION = 1; |
37
|
|
|
|
38
|
5 |
|
private $serializer; |
39
|
|
|
private $serializationContextFactory; |
40
|
5 |
|
private $deserializationContextFactory; |
41
|
5 |
|
|
42
|
|
|
public function __construct( |
43
|
|
|
SerializerInterface $serializer, |
44
|
|
|
SerializationContextFactoryInterface $serializationContextFactory = null, |
45
|
|
|
DeserializationContextFactoryInterface $deserializationContextFactory = null |
46
|
5 |
|
) { |
47
|
|
|
$this->serializer = $serializer; |
48
|
5 |
|
$this->serializationContextFactory = $serializationContextFactory; |
49
|
|
|
$this->deserializationContextFactory = $deserializationContextFactory; |
50
|
5 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function serialize($data, $format, Context $context) |
56
|
|
|
{ |
57
|
|
|
$context = $this->convertContext($context, self::SERIALIZATION); |
58
|
|
|
|
59
|
|
|
return $this->serializer->serialize($data, $format, $context); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function deserialize($data, $type, $format, Context $context) |
66
|
|
|
{ |
67
|
|
|
$context = $this->convertContext($context, self::DESERIALIZATION); |
68
|
|
|
|
69
|
5 |
|
return $this->serializer->deserialize($data, $type, $format, $context); |
|
|
|
|
70
|
|
|
} |
71
|
5 |
|
|
72
|
5 |
|
/** |
73
|
5 |
|
* @param Context $context |
74
|
|
|
* @param int $direction {@see self} constants |
75
|
|
|
* |
76
|
|
|
* @return JMSContext |
77
|
|
|
*/ |
78
|
|
|
private function convertContext(Context $context, $direction) |
79
|
|
|
{ |
80
|
|
|
if ($direction === self::SERIALIZATION) { |
81
|
|
|
$jmsContext = $this->serializationContextFactory |
82
|
|
|
? $this->serializationContextFactory->createSerializationContext() |
83
|
5 |
|
: SerializationContext::create(); |
84
|
5 |
|
} else { |
85
|
5 |
|
$jmsContext = $this->deserializationContextFactory |
86
|
|
|
? $this->deserializationContextFactory->createDeserializationContext() |
87
|
5 |
|
: DeserializationContext::create(); |
88
|
|
|
$maxDepth = $context->getMaxDepth(false); |
|
|
|
|
89
|
|
|
if (null !== $maxDepth) { |
90
|
5 |
|
for ($i = 0; $i < $maxDepth; ++$i) { |
91
|
|
|
$jmsContext->increaseDepth(); |
92
|
|
|
} |
93
|
5 |
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
5 |
|
foreach ($context->getAttributes() as $key => $value) { |
97
|
5 |
|
$jmsContext->attributes->set($key, $value); |
98
|
5 |
|
} |
99
|
|
|
|
100
|
5 |
|
if (null !== $context->getVersion()) { |
101
|
|
|
$jmsContext->setVersion($context->getVersion()); |
102
|
5 |
|
} |
103
|
|
|
if (null !== $context->getGroups()) { |
104
|
5 |
|
$jmsContext->setGroups($context->getGroups()); |
105
|
|
|
} |
106
|
|
|
if (null !== $context->getMaxDepth(false) || null !== $context->isMaxDepthEnabled()) { |
|
|
|
|
107
|
|
|
$jmsContext->enableMaxDepthChecks(); |
108
|
|
|
} |
109
|
|
|
if (null !== $context->getSerializeNull()) { |
110
|
|
|
$jmsContext->setSerializeNull($context->getSerializeNull()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
foreach ($context->getExclusionStrategies() as $strategy) { |
114
|
|
|
$jmsContext->addExclusionStrategy($strategy); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $jmsContext; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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.