Completed
Push — master ( 89419d...f547e3 )
by Asmir
28:00 queued 10:26
created

GenericSerializationVisitor::setNavigator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
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\Metadata\ClassMetadata;
22
use JMS\Serializer\Exception\InvalidArgumentException;
23
use JMS\Serializer\Metadata\PropertyMetadata;
24
25
/**
26
 * @deprecated
27
 */
28
abstract class GenericSerializationVisitor extends AbstractVisitor
29
{
30
    private $navigator;
31
    private $root;
32
    private $dataStack;
33
    private $data;
34
35
    public function setNavigator(GraphNavigator $navigator)
36
    {
37
        $this->navigator = $navigator;
38
        $this->root = null;
39
        $this->dataStack = new \SplStack;
40
    }
41
42
    /**
43
     * @return GraphNavigator
44
     */
45
    public function getNavigator()
46
    {
47
        return $this->navigator;
48
    }
49
50
    public function visitNull($data, array $type, Context $context)
51
    {
52
        return null;
53
    }
54
55
    public function visitString($data, array $type, Context $context)
56
    {
57
        if (null === $this->root) {
58
            $this->root = $data;
59
        }
60
61
        return (string) $data;
62
    }
63
64
    public function visitBoolean($data, array $type, Context $context)
65
    {
66
        if (null === $this->root) {
67
            $this->root = $data;
68
        }
69
70
        return (boolean) $data;
71
    }
72
73
    public function visitInteger($data, array $type, Context $context)
74
    {
75
        if (null === $this->root) {
76
            $this->root = $data;
77
        }
78
79
        return (int) $data;
80
    }
81
82
    public function visitDouble($data, array $type, Context $context)
83
    {
84
        if (null === $this->root) {
85
            $this->root = $data;
86
        }
87
88
        return (float) $data;
89
    }
90
91
    /**
92
     * @param array $data
93
     * @param array $type
94
     */
95
    public function visitArray($data, array $type, Context $context)
96
    {
97
        $this->dataStack->push($data);
98
99
        $isHash = isset($type['params'][1]);
100
101
        if (null === $this->root) {
102
            $this->root = $isHash ? new \ArrayObject() : array();
103
            $rs = &$this->root;
104
        } else {
105
            $rs = $isHash ? new \ArrayObject() : array();
106
        }
107
108
        $isList = isset($type['params'][0]) && ! isset($type['params'][1]);
109
110
        foreach ($data as $k => $v) {
111
            $v = $this->navigator->accept($v, $this->getElementType($type), $context);
112
113
            if (null === $v && $context->shouldSerializeNull() !== true) {
114
                continue;
115
            }
116
117
            if ($isList) {
118
                $rs[] = $v;
119
            } else {
120
                $rs[$k] = $v;
121
            }
122
        }
123
124
        $this->dataStack->pop();
125
126
        return $rs;
127
    }
128
129
    public function startVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context)
130
    {
131
        if (null === $this->root) {
132
            $this->root = new \stdClass;
133
        }
134
135
        $this->dataStack->push($this->data);
136
        $this->data = array();
137
    }
138
139
    public function endVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context)
140
    {
141
        $rs = $this->data;
142
        $this->data = $this->dataStack->pop();
143
144
        if ($this->root instanceof \stdClass && 0 === $this->dataStack->count()) {
145
            $this->root = $rs;
146
        }
147
148
        return $rs;
149
    }
150
151
    public function visitProperty(PropertyMetadata $metadata, $data, Context $context)
152
    {
153
        $v = $this->accessor->getValue($data, $metadata);
154
155
        $v = $this->navigator->accept($v, $metadata->type, $context);
156
        if (null === $v && $context->shouldSerializeNull() !== true) {
157
            return;
158
        }
159
160
        $k = $this->namingStrategy->translateName($metadata);
161
162
        if ($metadata->inline) {
163
            if (is_array($v)) {
164
                $this->data = array_merge($this->data, $v);
165
            }
166
        } else {
167
            $this->data[$k] = $v;
168
        }
169
    }
170
171
    /**
172
     * Allows you to add additional data to the current object/root element.
173
     * @deprecated use setData instead
174
     * @param string $key
175
     * @param integer|float|boolean|string|array|null $value This value must either be a regular scalar, or an array.
176
     *                                                       It must not contain any objects anymore.
177
     */
178
    public function addData($key, $value)
179
    {
180
        if (isset($this->data[$key])) {
181
            throw new InvalidArgumentException(sprintf('There is already data for "%s".', $key));
182
        }
183
184
        $this->data[$key] = $value;
185
    }
186
187
    /**
188
     * Checks if some data key exists.
189
     *
190
     * @param string $key
191
     * @return boolean
192
     */
193
    public function hasData($key)
194
    {
195
        return isset($this->data[$key]);
196
    }
197
198
    /**
199
     * Allows you to replace existing data on the current object/root element.
200
     *
201
     * @param string $key
202
     * @param integer|float|boolean|string|array|null $value This value must either be a regular scalar, or an array.
203
     *                                                       It must not contain any objects anymore.
204
     */
205
    public function setData($key, $value)
206
    {
207
        $this->data[$key] = $value;
208
    }
209
210
    public function getRoot()
211
    {
212
        return $this->root;
213
    }
214
215
    /**
216
     * @param array|\ArrayObject $data the passed data must be understood by whatever encoding function is applied later.
217
     */
218
    public function setRoot($data)
219
    {
220
        $this->root = $data;
221
    }
222
}
223