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
|
|
|
* Key-value pairs with custom attributes |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $attributes; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Serialization groups |
39
|
|
|
* |
40
|
|
|
* @var string[] |
41
|
|
|
*/ |
42
|
|
|
private $groups; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* ConfiguredContextFactory constructor. |
46
|
|
|
* |
47
|
|
|
* @param string|null $version Application version |
48
|
|
|
* @param bool $serializeNulls Flag if we should serialize null values |
49
|
|
|
* @param array $attributes Key-value pairs with custom attributes |
50
|
|
|
* @param string[] $groups Serialization groups |
51
|
|
|
*/ |
52
|
|
|
public function __construct($version, $serializeNulls, array $attributes, array $groups) |
53
|
|
|
{ |
54
|
|
|
$this->version = $version; |
55
|
|
|
$this->serializeNulls = $serializeNulls; |
56
|
|
|
$this->attributes = $attributes; |
57
|
|
|
$this->groups = $groups; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
63
|
|
|
public function createDeserializationContext() |
64
|
|
|
{ |
65
|
|
|
return $this->configureContext(new DeserializationContext()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc |
70
|
|
|
*/ |
71
|
|
|
public function createSerializationContext() |
72
|
|
|
{ |
73
|
|
|
return $this->configureContext(new SerializationContext()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Configures context according to configuration |
78
|
|
|
* |
79
|
|
|
* @param Context $context The context |
80
|
|
|
* |
81
|
|
|
* @return Context Given object |
82
|
|
|
*/ |
83
|
|
|
private function configureContext(Context $context) |
84
|
|
|
{ |
85
|
|
|
foreach ($this->attributes as $key => $value) { |
86
|
|
|
$context->setAttribute($key, $value); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$context->setGroups($this->groups); |
90
|
|
|
$context->setSerializeNull($this->serializeNulls); |
91
|
|
|
if ($this->version !== null) { |
92
|
|
|
$context->setVersion($this->version); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $context; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|