|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FOS\ElasticaBundle\Serializer; |
|
4
|
|
|
|
|
5
|
|
|
use JMS\Serializer\SerializationContext; |
|
6
|
|
|
use JMS\Serializer\SerializerInterface; |
|
7
|
|
|
|
|
8
|
|
|
class Callback |
|
9
|
|
|
{ |
|
10
|
|
|
protected $serializer; |
|
11
|
|
|
protected $groups = array(); |
|
12
|
|
|
protected $version; |
|
13
|
|
|
protected $serializeNull; |
|
14
|
|
|
|
|
15
|
2 |
|
public function setSerializer($serializer) |
|
16
|
|
|
{ |
|
17
|
2 |
|
$this->serializer = $serializer; |
|
18
|
2 |
|
if (!method_exists($this->serializer, 'serialize')) { |
|
19
|
|
|
throw new \RuntimeException('The serializer must have a "serialize" method.'); |
|
20
|
|
|
} |
|
21
|
2 |
|
} |
|
22
|
|
|
|
|
23
|
2 |
|
public function setGroups(array $groups) |
|
24
|
|
|
{ |
|
25
|
2 |
|
$this->groups = $groups; |
|
26
|
|
|
|
|
27
|
2 |
|
if (!empty($this->groups) && !$this->serializer instanceof SerializerInterface) { |
|
28
|
|
|
throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer".'); |
|
29
|
|
|
} |
|
30
|
2 |
|
} |
|
31
|
|
|
|
|
32
|
1 |
|
public function setVersion($version) |
|
33
|
|
|
{ |
|
34
|
1 |
|
$this->version = $version; |
|
35
|
|
|
|
|
36
|
1 |
|
if ($this->version && !$this->serializer instanceof SerializerInterface) { |
|
37
|
|
|
throw new \RuntimeException('Setting serialization version requires using "JMS\Serializer\Serializer".'); |
|
38
|
|
|
} |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
2 |
|
public function setSerializeNull($serializeNull) |
|
42
|
|
|
{ |
|
43
|
2 |
|
$this->serializeNull = $serializeNull; |
|
44
|
|
|
|
|
45
|
2 |
|
if (true === $this->serializeNull && !$this->serializer instanceof SerializerInterface) { |
|
46
|
|
|
throw new \RuntimeException('Setting null value serialization option requires using "JMS\Serializer\Serializer".'); |
|
47
|
|
|
} |
|
48
|
2 |
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
public function serialize($object) |
|
51
|
|
|
{ |
|
52
|
2 |
|
$context = $this->serializer instanceof SerializerInterface ? SerializationContext::create()->enableMaxDepthChecks() : array(); |
|
53
|
|
|
|
|
54
|
2 |
|
if (!empty($this->groups)) { |
|
55
|
1 |
|
$context->setGroups($this->groups); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
if ($this->version) { |
|
59
|
1 |
|
$context->setVersion($this->version); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
if (!is_array($context)) { |
|
63
|
2 |
|
$context->setSerializeNull($this->serializeNull); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
2 |
|
return $this->serializer->serialize($object, 'json', $context); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|