Completed
Push — master ( 602bd8...b0a46c )
by Johannes
03:05
created

YamlSerializationVisitor::visitInteger()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

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