1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace JMS\Serializer;
use JMS\Serializer\Exception\RuntimeException;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\Naming\AdvancedNamingStrategyInterface;
use JMS\Serializer\Naming\PropertyNamingStrategyInterface;
/**
* Generic Deserialization Visitor.
* @deprecated
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
abstract class GenericDeserializationVisitor extends AbstractVisitor
{
private $navigator;
private $result;
private $objectStack;
private $currentObject;
public function setNavigator(GraphNavigator $navigator)
{
$this->navigator = $navigator;
$this->result = null;
$this->objectStack = new \SplStack;
}
public function getNavigator()
{
return $this->navigator;
}
public function prepare($data)
{
return $this->decode($data);
}
public function visitNull($data, array $type, Context $context)
{
return null;
}
public function visitString($data, array $type, Context $context)
{
$data = (string)$data;
if (null === $this->result) {
$this->result = $data;
}
return $data;
}
public function visitBoolean($data, array $type, Context $context)
{
$data = (Boolean)$data;
if (null === $this->result) {
$this->result = $data;
}
return $data;
}
public function visitInteger($data, array $type, Context $context)
{
$data = (integer)$data;
if (null === $this->result) {
$this->result = $data;
}
return $data;
}
public function visitDouble($data, array $type, Context $context)
{
$data = (double)$data;
if (null === $this->result) {
$this->result = $data;
}
return $data;
}
public function visitArray($data, array $type, Context $context)
{
if (!\is_array($data)) {
throw new RuntimeException(sprintf('Expected array, but got %s: %s', \gettype($data), json_encode($data)));
}
// If no further parameters were given, keys/values are just passed as is.
if (!$type['params']) {
if (null === $this->result) {
$this->result = $data;
}
return $data;
}
switch (\count($type['params'])) {
case 1: // Array is a list.
$listType = $type['params'][0];
$result = array();
if (null === $this->result) {
$this->result = &$result;
}
foreach ($data as $v) {
$result[] = $this->navigator->accept($v, $listType, $context);
}
return $result;
case 2: // Array is a map.
list($keyType, $entryType) = $type['params'];
$result = array();
if (null === $this->result) {
$this->result = &$result;
}
foreach ($data as $k => $v) {
$result[$this->navigator->accept($k, $keyType, $context)] = $this->navigator->accept($v, $entryType, $context);
}
return $result;
default:
throw new RuntimeException(sprintf('Array type cannot have more than 2 parameters, but got %s.', json_encode($type['params'])));
}
}
public function startVisitingObject(ClassMetadata $metadata, $object, array $type, Context $context)
{
$this->setCurrentObject($object);
if (null === $this->result) {
$this->result = $this->currentObject;
}
}
public function visitProperty(PropertyMetadata $metadata, $data, Context $context)
{
if ($this->namingStrategy instanceof AdvancedNamingStrategyInterface) {
$name = $this->namingStrategy->getPropertyName($metadata, $context);
} else {
$name = $this->namingStrategy->translateName($metadata);
}
if (null === $data) {
return;
}
if (!\is_array($data)) {
throw new RuntimeException(sprintf('Invalid data "%s"(%s), expected "%s".', $data, $metadata->type['name'], $metadata->reflection->class));
}
if (!array_key_exists($name, $data)) {
return;
}
if (!$metadata->type) {
throw new RuntimeException(sprintf('You must define a type for %s::$%s.', $metadata->reflection->class, $metadata->name));
}
$v = $data[$name] !== null ? $this->navigator->accept($data[$name], $metadata->type, $context) : null;
$this->accessor->setValue($this->currentObject, $v, $metadata);
}
public function endVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context)
{
$obj = $this->currentObject;
$this->revertCurrentObject();
return $obj;
}
public function getResult()
{
return $this->result;
}
public function setCurrentObject($object)
{
$this->objectStack->push($this->currentObject);
$this->currentObject = $object;
}
public function getCurrentObject()
{
return $this->currentObject;
}
public function revertCurrentObject()
{
return $this->currentObject = $this->objectStack->pop();
}
abstract protected function decode($str);
}