|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright 2016 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\Accessor\AccessorStrategyInterface; |
|
22
|
|
|
use JMS\Serializer\Metadata\ClassMetadata; |
|
23
|
|
|
use JMS\Serializer\Metadata\PropertyMetadata; |
|
24
|
|
|
use JMS\Serializer\Naming\PropertyNamingStrategyInterface; |
|
25
|
|
|
use JMS\Serializer\Util\Writer; |
|
26
|
|
|
use Symfony\Component\Yaml\Inline; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Serialization Visitor for the YAML format. |
|
30
|
|
|
* |
|
31
|
|
|
* @see http://www.yaml.org/spec/ |
|
32
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
|
33
|
|
|
*/ |
|
34
|
|
|
class YamlSerializationVisitor extends AbstractVisitor |
|
35
|
|
|
{ |
|
36
|
|
|
public $writer; |
|
37
|
|
|
|
|
38
|
|
|
private $navigator; |
|
39
|
|
|
private $stack; |
|
40
|
|
|
private $metadataStack; |
|
41
|
|
|
private $currentMetadata; |
|
42
|
|
|
|
|
43
|
379 |
|
public function __construct(PropertyNamingStrategyInterface $namingStrategy, AccessorStrategyInterface $accessorStrategy = null) |
|
44
|
|
|
{ |
|
45
|
379 |
|
parent::__construct($namingStrategy, $accessorStrategy); |
|
46
|
|
|
|
|
47
|
379 |
|
$this->writer = new Writer(); |
|
48
|
379 |
|
} |
|
49
|
|
|
|
|
50
|
98 |
|
public function setNavigator(GraphNavigator $navigator) |
|
51
|
|
|
{ |
|
52
|
98 |
|
$this->navigator = $navigator; |
|
53
|
98 |
|
$this->writer->reset(); |
|
54
|
98 |
|
$this->stack = new \SplStack; |
|
55
|
98 |
|
$this->metadataStack = new \SplStack; |
|
56
|
98 |
|
} |
|
57
|
|
|
|
|
58
|
10 |
|
public function visitNull($data, array $type, Context $context) |
|
59
|
|
|
{ |
|
60
|
10 |
|
if ('' === $this->writer->content) { |
|
61
|
6 |
|
$this->writer->writeln('null'); |
|
62
|
6 |
|
} |
|
63
|
|
|
|
|
64
|
10 |
|
return 'null'; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
61 |
|
public function visitString($data, array $type, Context $context) |
|
68
|
|
|
{ |
|
69
|
61 |
|
$v = Inline::dump($data); |
|
70
|
|
|
|
|
71
|
61 |
|
if ('' === $this->writer->content) { |
|
72
|
5 |
|
$this->writer->writeln($v); |
|
73
|
5 |
|
} |
|
74
|
|
|
|
|
75
|
61 |
|
return $v; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param array $data |
|
80
|
|
|
* @param array $type |
|
81
|
|
|
*/ |
|
82
|
43 |
|
public function visitArray($data, array $type, Context $context) |
|
83
|
|
|
{ |
|
84
|
43 |
|
$isHash = isset($type['params'][1]); |
|
85
|
|
|
|
|
86
|
43 |
|
$count = $this->writer->changeCount; |
|
87
|
43 |
|
$isList = (isset($type['params'][0]) && !isset($type['params'][1])) |
|
88
|
43 |
|
|| array_keys($data) === range(0, count($data) - 1); |
|
89
|
|
|
|
|
90
|
43 |
|
foreach ($data as $k => $v) { |
|
91
|
38 |
|
if (null === $v && $context->shouldSerializeNull() !== true) { |
|
92
|
1 |
|
continue; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
38 |
|
if ($isList && !$isHash) { |
|
96
|
24 |
|
$this->writer->writeln('-'); |
|
97
|
24 |
|
} else { |
|
98
|
17 |
|
$this->writer->writeln(Inline::dump($k) . ':'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
38 |
|
$this->writer->indent(); |
|
102
|
|
|
|
|
103
|
38 |
|
if (null !== $v = $this->navigator->accept($v, $this->getElementType($type), $context)) { |
|
104
|
29 |
|
$this->writer |
|
105
|
29 |
|
->rtrim(false) |
|
106
|
29 |
|
->writeln(' ' . $v); |
|
107
|
29 |
|
} |
|
108
|
|
|
|
|
109
|
38 |
|
$this->writer->outdent(); |
|
110
|
43 |
|
} |
|
111
|
|
|
|
|
112
|
43 |
|
if ($count === $this->writer->changeCount && isset($type['params'][1])) { |
|
113
|
2 |
|
$this->writer |
|
114
|
2 |
|
->rtrim(false) |
|
115
|
2 |
|
->writeln(' {}'); |
|
116
|
43 |
|
} elseif (empty($data)) { |
|
117
|
6 |
|
$this->writer |
|
118
|
6 |
|
->rtrim(false) |
|
119
|
6 |
|
->writeln(' []'); |
|
120
|
6 |
|
} |
|
121
|
43 |
|
} |
|
122
|
|
|
|
|
123
|
6 |
|
public function visitBoolean($data, array $type, Context $context) |
|
124
|
|
|
{ |
|
125
|
6 |
|
$v = $data ? 'true' : 'false'; |
|
126
|
|
|
|
|
127
|
6 |
|
if ('' === $this->writer->content) { |
|
128
|
2 |
|
$this->writer->writeln($v); |
|
129
|
2 |
|
} |
|
130
|
|
|
|
|
131
|
6 |
|
return $v; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
9 |
|
public function visitDouble($data, array $type, Context $context) |
|
135
|
|
|
{ |
|
136
|
9 |
|
$v = (string)$data; |
|
137
|
|
|
|
|
138
|
9 |
|
if ('' === $this->writer->content) { |
|
139
|
4 |
|
$this->writer->writeln($v); |
|
140
|
4 |
|
} |
|
141
|
|
|
|
|
142
|
9 |
|
return $v; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
22 |
|
public function visitInteger($data, array $type, Context $context) |
|
146
|
|
|
{ |
|
147
|
22 |
|
$v = (string)$data; |
|
148
|
|
|
|
|
149
|
22 |
|
if ('' === $this->writer->content) { |
|
150
|
1 |
|
$this->writer->writeln($v); |
|
151
|
1 |
|
} |
|
152
|
|
|
|
|
153
|
22 |
|
return $v; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
52 |
|
public function startVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
|
157
|
|
|
{ |
|
158
|
52 |
|
} |
|
159
|
|
|
|
|
160
|
52 |
|
public function visitProperty(PropertyMetadata $metadata, $data, Context $context) |
|
161
|
|
|
{ |
|
162
|
52 |
|
$v = $this->accessor->getValue($data, $metadata); |
|
163
|
|
|
|
|
164
|
51 |
|
if (null === $v && $context->shouldSerializeNull() !== true) { |
|
165
|
5 |
|
return; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
51 |
|
$name = $this->namingStrategy->translateName($metadata); |
|
169
|
|
|
|
|
170
|
51 |
|
if (!$metadata->inline) { |
|
171
|
51 |
|
$this->writer |
|
172
|
51 |
|
->writeln(Inline::dump($name) . ':') |
|
173
|
51 |
|
->indent(); |
|
174
|
51 |
|
} |
|
175
|
|
|
|
|
176
|
51 |
|
$this->setCurrentMetadata($metadata); |
|
177
|
|
|
|
|
178
|
51 |
|
$count = $this->writer->changeCount; |
|
179
|
|
|
|
|
180
|
51 |
|
if (null !== $v = $this->navigator->accept($v, $metadata->type, $context)) { |
|
181
|
44 |
|
$this->writer |
|
182
|
44 |
|
->rtrim(false) |
|
183
|
44 |
|
->writeln(' ' . $v); |
|
184
|
51 |
|
} elseif ($count === $this->writer->changeCount && !$metadata->inline) { |
|
185
|
2 |
|
$this->writer->revert(); |
|
186
|
2 |
|
} |
|
187
|
|
|
|
|
188
|
51 |
|
if (!$metadata->inline) { |
|
189
|
51 |
|
$this->writer->outdent(); |
|
190
|
51 |
|
} |
|
191
|
51 |
|
$this->revertCurrentMetadata(); |
|
192
|
51 |
|
} |
|
193
|
|
|
|
|
194
|
51 |
|
public function endVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context) |
|
195
|
|
|
{ |
|
196
|
51 |
|
} |
|
197
|
|
|
|
|
198
|
51 |
|
public function setCurrentMetadata(PropertyMetadata $metadata) |
|
199
|
|
|
{ |
|
200
|
51 |
|
$this->metadataStack->push($this->currentMetadata); |
|
201
|
51 |
|
$this->currentMetadata = $metadata; |
|
202
|
51 |
|
} |
|
203
|
|
|
|
|
204
|
51 |
|
public function revertCurrentMetadata() |
|
205
|
|
|
{ |
|
206
|
51 |
|
return $this->currentMetadata = $this->metadataStack->pop(); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
public function getNavigator() |
|
210
|
|
|
{ |
|
211
|
|
|
return $this->navigator; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
95 |
|
public function getResult() |
|
215
|
|
|
{ |
|
216
|
95 |
|
return $this->writer->getContent(); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|