1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright 2013 Johannes M. Schmitt <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace JMS\Serializer; |
20
|
|
|
|
21
|
|
|
use JMS\Serializer\Metadata\ClassMetadata; |
22
|
|
|
use JMS\Serializer\Exception\InvalidArgumentException; |
23
|
|
|
use JMS\Serializer\Metadata\PropertyMetadata; |
24
|
|
|
|
25
|
|
|
abstract class GenericSerializationVisitor extends AbstractVisitor |
26
|
|
|
{ |
27
|
|
|
private $navigator; |
28
|
|
|
private $root; |
29
|
|
|
private $dataStack; |
30
|
|
|
private $data; |
31
|
|
|
|
32
|
85 |
|
public function setNavigator(GraphNavigator $navigator) |
33
|
|
|
{ |
34
|
85 |
|
$this->navigator = $navigator; |
35
|
85 |
|
$this->root = null; |
36
|
85 |
|
$this->dataStack = new \SplStack; |
37
|
85 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return GraphNavigator |
41
|
|
|
*/ |
42
|
|
|
public function getNavigator() |
43
|
|
|
{ |
44
|
|
|
return $this->navigator; |
45
|
|
|
} |
46
|
|
|
|
47
|
13 |
|
public function visitNull($data, array $type, Context $context) |
48
|
|
|
{ |
49
|
13 |
|
return null; |
50
|
|
|
} |
51
|
|
|
|
52
|
48 |
|
public function visitString($data, array $type, Context $context) |
53
|
|
|
{ |
54
|
48 |
|
if (null === $this->root) { |
55
|
8 |
|
$this->root = $data; |
56
|
8 |
|
} |
57
|
|
|
|
58
|
48 |
|
return (string) $data; |
59
|
|
|
} |
60
|
|
|
|
61
|
9 |
|
public function visitBoolean($data, array $type, Context $context) |
62
|
|
|
{ |
63
|
9 |
|
if (null === $this->root) { |
64
|
5 |
|
$this->root = $data; |
65
|
5 |
|
} |
66
|
|
|
|
67
|
9 |
|
return (boolean) $data; |
68
|
|
|
} |
69
|
|
|
|
70
|
18 |
|
public function visitInteger($data, array $type, Context $context) |
71
|
|
|
{ |
72
|
18 |
|
if (null === $this->root) { |
73
|
4 |
|
$this->root = $data; |
74
|
4 |
|
} |
75
|
|
|
|
76
|
18 |
|
return (int) $data; |
77
|
|
|
} |
78
|
|
|
|
79
|
13 |
|
public function visitDouble($data, array $type, Context $context) |
80
|
|
|
{ |
81
|
13 |
|
if (null === $this->root) { |
82
|
7 |
|
$this->root = $data; |
83
|
7 |
|
} |
84
|
|
|
|
85
|
13 |
|
return (float) $data; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $data |
90
|
|
|
* @param array $type |
91
|
|
|
*/ |
92
|
32 |
|
public function visitArray($data, array $type, Context $context) |
93
|
|
|
{ |
94
|
32 |
|
if (null === $this->root) { |
95
|
15 |
|
$this->root = array(); |
96
|
15 |
|
$rs = &$this->root; |
97
|
15 |
|
} else { |
98
|
19 |
|
$rs = array(); |
99
|
|
|
} |
100
|
|
|
|
101
|
32 |
|
$isList = isset($type['params'][0]) && ! isset($type['params'][1]); |
102
|
|
|
|
103
|
32 |
|
foreach ($data as $k => $v) { |
104
|
31 |
|
$v = $this->navigator->accept($v, $this->getElementType($type), $context); |
105
|
|
|
|
106
|
31 |
|
if (null === $v && ( ! is_string($k) || ! $context->shouldSerializeNull())) { |
107
|
2 |
|
continue; |
108
|
|
|
} |
109
|
|
|
|
110
|
31 |
|
if ($isList) { |
111
|
8 |
|
$rs[] = $v; |
112
|
8 |
|
} else { |
113
|
26 |
|
$rs[$k] = $v; |
114
|
|
|
} |
115
|
32 |
|
} |
116
|
|
|
|
117
|
32 |
|
return $rs; |
118
|
|
|
} |
119
|
|
|
|
120
|
48 |
|
public function startVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
121
|
|
|
{ |
122
|
48 |
|
if (null === $this->root) { |
123
|
43 |
|
$this->root = new \stdClass; |
124
|
43 |
|
} |
125
|
|
|
|
126
|
48 |
|
$this->dataStack->push($this->data); |
127
|
48 |
|
$this->data = array(); |
128
|
48 |
|
} |
129
|
|
|
|
130
|
48 |
|
public function endVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
131
|
|
|
{ |
132
|
48 |
|
$rs = $this->data; |
133
|
48 |
|
$this->data = $this->dataStack->pop(); |
134
|
|
|
|
135
|
48 |
|
if ($this->root instanceof \stdClass && 0 === $this->dataStack->count()) { |
136
|
43 |
|
$this->root = $rs; |
137
|
43 |
|
} |
138
|
|
|
|
139
|
48 |
|
return $rs; |
140
|
|
|
} |
141
|
|
|
|
142
|
46 |
|
public function visitProperty(PropertyMetadata $metadata, $data, Context $context) |
143
|
|
|
{ |
144
|
46 |
|
$v = $metadata->getValue($data); |
145
|
|
|
|
146
|
46 |
|
$v = $this->navigator->accept($v, $metadata->type, $context); |
147
|
46 |
|
if (null === $v && ! $context->shouldSerializeNull()) { |
148
|
5 |
|
return; |
149
|
|
|
} |
150
|
|
|
|
151
|
44 |
|
$k = $this->namingStrategy->translateName($metadata); |
152
|
|
|
|
153
|
44 |
|
if ($metadata->inline) { |
154
|
2 |
|
if (is_array($v)) { |
155
|
1 |
|
$this->data = array_merge($this->data, $v); |
156
|
1 |
|
} |
157
|
2 |
|
} else { |
158
|
44 |
|
$this->data[$k] = $v; |
159
|
|
|
} |
160
|
44 |
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Allows you to add additional data to the current object/root element. |
164
|
|
|
* |
165
|
|
|
* @param string $key |
166
|
|
|
* @param scalar|array $value This value must either be a regular scalar, or an array. |
167
|
|
|
* It must not contain any objects anymore. |
168
|
|
|
*/ |
169
|
1 |
|
public function addData($key, $value) |
170
|
|
|
{ |
171
|
1 |
|
if (isset($this->data[$key])) { |
172
|
|
|
throw new InvalidArgumentException(sprintf('There is already data for "%s".', $key)); |
173
|
|
|
} |
174
|
|
|
|
175
|
1 |
|
$this->data[$key] = $value; |
176
|
1 |
|
} |
177
|
|
|
|
178
|
86 |
|
public function getRoot() |
179
|
|
|
{ |
180
|
86 |
|
return $this->root; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param array|\ArrayObject $data the passed data must be understood by whatever encoding function is applied later. |
185
|
|
|
*/ |
186
|
10 |
|
public function setRoot($data) |
187
|
|
|
{ |
188
|
10 |
|
$this->root = $data; |
189
|
10 |
|
} |
190
|
|
|
} |
191
|
|
|
|