Completed
Pull Request — master (#859)
by
unknown
05:02
created

YamlSerializationVisitor::revertCurrentMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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