1 | <?php |
||
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) |
|
49 | |||
50 | 98 | public function setNavigator(GraphNavigator $navigator) |
|
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) |
|
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) |
|
197 | |||
198 | 51 | public function setCurrentMetadata(PropertyMetadata $metadata) |
|
203 | |||
204 | 51 | public function revertCurrentMetadata() |
|
208 | |||
209 | public function getNavigator() |
||
210 | { |
||
211 | return $this->navigator; |
||
212 | } |
||
213 | |||
214 | 95 | public function getResult() |
|
215 | { |
||
218 | } |
||
219 |