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