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 FOS\RestBundle\Util\FormWrapper; |
16
|
|
|
use Symfony\Component\Form\FormInterface; |
17
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Adapter to plug the Symfony serializer into the FOSRestBundle Serializer API. |
21
|
|
|
* |
22
|
|
|
* @author Christian Flothmann <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class SymfonySerializerAdapter implements Serializer |
25
|
|
|
{ |
26
|
|
|
private $serializer; |
27
|
|
|
|
28
|
13 |
|
public function __construct(SerializerInterface $serializer) |
29
|
|
|
{ |
30
|
13 |
|
$this->serializer = $serializer; |
31
|
13 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
7 |
|
public function serialize($data, $format, Context $context) |
37
|
|
|
{ |
38
|
|
|
/* |
39
|
|
|
* Bug fix, see https://github.com/symfony/symfony/pull/17984 |
40
|
|
|
* will be removed in the future. |
41
|
|
|
*/ |
42
|
7 |
|
if ($data instanceof FormInterface && $format == 'xml') { |
43
|
1 |
|
$data = new FormWrapper($data); |
44
|
1 |
|
} |
45
|
|
|
|
46
|
7 |
|
$newContext = $this->convertContext($context); |
47
|
7 |
|
$newContext['serializeNull'] = $context->getSerializeNull(); |
48
|
|
|
|
49
|
7 |
|
return $this->serializer->serialize($data, $format, $newContext); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
2 |
|
public function deserialize($data, $type, $format, Context $context) |
56
|
|
|
{ |
57
|
2 |
|
$newContext = $this->convertContext($context); |
58
|
|
|
|
59
|
2 |
|
return $this->serializer->deserialize($data, $type, $format, $newContext); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Context $context |
64
|
|
|
*/ |
65
|
9 |
|
private function convertContext(Context $context) |
66
|
|
|
{ |
67
|
9 |
|
$newContext = array(); |
68
|
9 |
|
foreach ($context->getAttributes() as $key => $value) { |
69
|
7 |
|
$newContext[$key] = $value; |
70
|
9 |
|
} |
71
|
|
|
|
72
|
9 |
|
$newContext['groups'] = $context->getGroups(); |
73
|
9 |
|
$newContext['version'] = $context->getVersion(); |
74
|
9 |
|
$newContext['maxDepth'] = $context->getMaxDepth(); |
75
|
|
|
|
76
|
9 |
|
return $newContext; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|