Completed
Push — master ( 1652fa...4eeda9 )
by
unknown
9s
created

ArraySerializationVisitor::addData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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