1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JMS\SerializerBundle\ContextFactory; |
4
|
|
|
|
5
|
|
|
use JMS\Serializer\Context; |
6
|
|
|
use JMS\Serializer\ContextFactory\DeserializationContextFactoryInterface; |
7
|
|
|
use JMS\Serializer\ContextFactory\SerializationContextFactoryInterface; |
8
|
|
|
use JMS\Serializer\DeserializationContext; |
9
|
|
|
use JMS\Serializer\SerializationContext; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ConfiguredContextFactory |
13
|
|
|
*/ |
14
|
|
|
class ConfiguredContextFactory implements SerializationContextFactoryInterface, DeserializationContextFactoryInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Application version |
18
|
|
|
* |
19
|
|
|
* @var null|string |
20
|
|
|
*/ |
21
|
|
|
private $version; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Flag if we should serialize null values |
25
|
|
|
* |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
private $serializeNulls; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Flag if we should enable the max depth exclusion strategy |
32
|
|
|
* |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
private $enableMaxDepthChecks = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Key-value pairs with custom attributes |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $attributes = array(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Serialization groups |
46
|
|
|
* |
47
|
|
|
* @var string[] |
48
|
|
|
*/ |
49
|
|
|
private $groups = array(); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param null|string $version |
53
|
|
|
*/ |
54
|
|
|
public function setVersion($version) |
55
|
|
|
{ |
56
|
|
|
$this->version = $version; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param bool $serializeNulls |
61
|
|
|
*/ |
62
|
|
|
public function setSerializeNulls($serializeNulls) |
63
|
|
|
{ |
64
|
|
|
$this->serializeNulls = (bool)$serializeNulls; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function enableMaxDepthChecks() |
68
|
|
|
{ |
69
|
|
|
$this->enableMaxDepthChecks = true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $attributes |
74
|
|
|
*/ |
75
|
|
|
public function setAttributes(array $attributes) |
76
|
|
|
{ |
77
|
|
|
$this->attributes = $attributes; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string[] $groups |
82
|
|
|
*/ |
83
|
|
|
public function setGroups(array $groups) |
84
|
|
|
{ |
85
|
|
|
$this->groups = $groups; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritDoc |
90
|
|
|
*/ |
91
|
|
|
public function createDeserializationContext() |
92
|
|
|
{ |
93
|
|
|
return $this->configureContext(new DeserializationContext()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritDoc |
98
|
|
|
*/ |
99
|
|
|
public function createSerializationContext() |
100
|
|
|
{ |
101
|
|
|
return $this->configureContext(new SerializationContext()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Configures context according to configuration |
106
|
|
|
* |
107
|
|
|
* @param Context $context The context |
108
|
|
|
* |
109
|
|
|
* @return Context Given object |
110
|
|
|
*/ |
111
|
|
|
private function configureContext(Context $context) |
112
|
|
|
{ |
113
|
|
|
foreach ($this->attributes as $key => $value) { |
114
|
|
|
$context->setAttribute($key, $value); |
115
|
|
|
} |
116
|
|
|
if (!empty($this->groups)) { |
117
|
|
|
$context->setGroups($this->groups); |
118
|
|
|
} |
119
|
|
|
if ($this->serializeNulls !== null) { |
120
|
|
|
$context->setSerializeNull($this->serializeNulls); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($this->enableMaxDepthChecks === true) { |
124
|
|
|
$context->enableMaxDepthChecks(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ($this->version !== null) { |
128
|
|
|
$context->setVersion($this->version); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $context; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|