1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle 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\ElasticaBundle\Serializer; |
13
|
|
|
|
14
|
|
|
use JMS\Serializer\SerializationContext; |
15
|
|
|
use JMS\Serializer\SerializerInterface as JMSSerializer; |
16
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
17
|
|
|
|
18
|
|
|
class Callback |
19
|
|
|
{ |
20
|
|
|
protected $serializer; |
21
|
|
|
protected $groups = []; |
22
|
|
|
protected $version; |
23
|
|
|
protected $serializeNull; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param $serializer |
27
|
|
|
*/ |
28
|
3 |
|
public function setSerializer($serializer) |
29
|
|
|
{ |
30
|
3 |
|
$this->serializer = $serializer; |
31
|
3 |
|
if (!method_exists($this->serializer, 'serialize')) { |
32
|
1 |
|
throw new \RuntimeException('The serializer must have a "serialize" method.'); |
33
|
|
|
} |
34
|
2 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $groups |
38
|
|
|
*/ |
39
|
2 |
|
public function setGroups(array $groups) |
40
|
|
|
{ |
41
|
2 |
|
$this->groups = $groups; |
42
|
|
|
|
43
|
2 |
|
if (!empty($this->groups) && !$this->serializer instanceof SerializerInterface && !$this->serializer instanceof JMSSerializer) { |
44
|
1 |
|
throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer" or "Symfony\Component\Serializer\Serializer"'); |
45
|
|
|
} |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $version |
50
|
|
|
*/ |
51
|
|
|
public function setVersion($version) |
52
|
|
|
{ |
53
|
|
|
$this->version = $version; |
54
|
|
|
|
55
|
|
|
if ($this->version && !$this->serializer instanceof JMSSerializer) { |
56
|
|
|
throw new \RuntimeException('Setting serialization version requires using "JMS\Serializer\Serializer".'); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $serializeNull |
62
|
|
|
*/ |
63
|
|
|
public function setSerializeNull($serializeNull) |
64
|
|
|
{ |
65
|
|
|
$this->serializeNull = $serializeNull; |
66
|
|
|
|
67
|
|
|
if (true === $this->serializeNull && !$this->serializer instanceof JMSSerializer) { |
68
|
|
|
throw new \RuntimeException('Setting null value serialization option requires using "JMS\Serializer\Serializer".'); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $object |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function serialize($object) |
78
|
|
|
{ |
79
|
|
|
$context = $this->serializer instanceof JMSSerializer ? SerializationContext::create()->enableMaxDepthChecks() : []; |
80
|
|
|
|
81
|
|
|
if (!empty($this->groups)) { |
82
|
|
|
if ($context instanceof SerializationContext) { |
83
|
|
|
$context->setGroups($this->groups); |
84
|
|
|
} else { |
85
|
|
|
$context['groups'] = $this->groups; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($this->version) { |
90
|
|
|
$context->setVersion($this->version); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (!is_array($context)) { |
94
|
|
|
$context->setSerializeNull($this->serializeNull); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->serializer->serialize($object, 'json', $context); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|