Completed
Pull Request — master (#859)
by
unknown
04:25
created

YamlSerializationVisitor::startVisitingObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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