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 Symfony\Component\Serializer\SerializerInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Adapter to plug the Symfony serializer into the FOSRestBundle Serializer API. |
19
|
|
|
* |
20
|
|
|
* @author Christian Flothmann <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class SymfonySerializerAdapter implements Serializer |
23
|
|
|
{ |
24
|
|
|
private $serializer; |
25
|
|
|
|
26
|
18 |
|
public function __construct(SerializerInterface $serializer) |
27
|
|
|
{ |
28
|
18 |
|
$this->serializer = $serializer; |
29
|
18 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
9 |
|
public function serialize($data, $format, Context $context) |
35
|
|
|
{ |
36
|
9 |
|
$newContext = $this->convertContext($context); |
37
|
9 |
|
$newContext['serializeNull'] = $context->getSerializeNull(); |
38
|
|
|
|
39
|
9 |
|
return $this->serializer->serialize($data, $format, $newContext); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
2 |
|
public function deserialize($data, $type, $format, Context $context) |
46
|
|
|
{ |
47
|
2 |
|
$newContext = $this->convertContext($context); |
48
|
|
|
|
49
|
2 |
|
return $this->serializer->deserialize($data, $type, $format, $newContext); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Context $context |
54
|
|
|
*/ |
55
|
11 |
|
private function convertContext(Context $context) |
56
|
|
|
{ |
57
|
11 |
|
$newContext = array(); |
58
|
11 |
|
foreach ($context->getAttributes() as $key => $value) { |
59
|
9 |
|
$newContext[$key] = $value; |
60
|
11 |
|
} |
61
|
|
|
|
62
|
11 |
|
if (null !== $context->getGroups()) { |
63
|
|
|
$newContext['groups'] = $context->getGroups(); |
64
|
|
|
} |
65
|
11 |
|
$newContext['version'] = $context->getVersion(); |
66
|
11 |
|
$newContext['maxDepth'] = $context->getMaxDepth(false); |
|
|
|
|
67
|
11 |
|
$newContext['enable_max_depth'] = $context->isMaxDepthEnabled(); |
68
|
|
|
|
69
|
11 |
|
return $newContext; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.