Completed
Pull Request — master (#14)
by
unknown
03:04
created

ArraySerializationVisitor::setData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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 (boolean)$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
     * @return array|\ArrayObject
81
     */
82
    public function visitArray($data, array $type, Context $context)
83
    {
84
        $this->dataStack->push($data);
85
86
        $isHash = isset($type['params'][1]);
87
88
        if (null === $this->root) {
89
            $this->root = $isHash ? new \ArrayObject() : array();
90
            $rs = &$this->root;
91
        } else {
92
            $rs = $isHash ? new \ArrayObject() : array();
93
        }
94
95
        $isList = isset($type['params'][0]) && !isset($type['params'][1]);
96
97
        foreach ($data as $k => $v) {
98
            $v = $this->navigator->accept($v, $this->getElementType($type), $context);
99
100
            if (null === $v && $context->shouldSerializeNull() !== true) {
101
                continue;
102
            }
103
104
            if ($isList) {
105
                $rs[] = $v;
106
            } else {
107
                $rs[$k] = $v;
108
            }
109
        }
110
111
        $this->dataStack->pop();
112
113
        return $rs;
114
    }
115
116
    public function startVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context)
117
    {
118
        if (null === $this->root) {
119
            $this->root = new \stdClass;
120
        }
121
122
        $this->dataStack->push($this->data);
123
        $this->data = array();
124
    }
125
126
    public function endVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context)
127
    {
128
        $rs = $this->data;
129
        $this->data = $this->dataStack->pop();
130
131
        if ($this->root instanceof \stdClass && 0 === $this->dataStack->count()) {
132
            $this->root = $rs;
133
        }
134
135
        return $rs;
136
    }
137
138
    public function visitProperty(PropertyMetadata $metadata, $data, Context $context)
139
    {
140
        $v = $this->accessor->getValue($data, $metadata);
141
142
        $v = $this->navigator->accept($v, $metadata->type, $context);
143
        if (null === $v && $context->shouldSerializeNull() !== true) {
144
            return;
145
        }
146
147
        if ($this->namingStrategy instanceof AdvancedNamingStrategyInterface) {
148
            $k = $this->namingStrategy->getPropertyName($metadata, $context);
149
        } else {
150
            $k = $this->namingStrategy->translateName($metadata);
151
        }
152
153
        if ($metadata->inline) {
154
            if (\is_array($v)) {
155
                $this->data = array_merge($this->data, $v);
156
            }
157
        } else {
158
            $this->data[$k] = $v;
159
        }
160
    }
161
162
    /**
163
     * Allows you to add additional data to the current object/root element.
164
     * @deprecated use setData instead
165
     * @param string $key
166
     * @param integer|float|boolean|string|array|null $value This value must either be a regular scalar, or an array.
167
     *                                                       It must not contain any objects anymore.
168
     */
169
    public function addData($key, $value)
170
    {
171
        if (isset($this->data[$key])) {
172
            throw new InvalidArgumentException(sprintf('There is already data for "%s".', $key));
173
        }
174
175
        $this->data[$key] = $value;
176
    }
177
178
    /**
179
     * Checks if some data key exists.
180
     *
181
     * @param string $key
182
     * @return boolean
183
     */
184
    public function hasData($key)
185
    {
186
        return isset($this->data[$key]);
187
    }
188
189
    /**
190
     * Allows you to replace existing data on the current object/root element.
191
     *
192
     * @param string $key
193
     * @param integer|float|boolean|string|array|null $value This value must either be a regular scalar, or an array.
194
     *                                                       It must not contain any objects anymore.
195
     */
196
    public function setData($key, $value)
197
    {
198
        $this->data[$key] = $value;
199
    }
200
201
    public function getRoot()
202
    {
203
        return $this->root;
204
    }
205
206
    /**
207
     * @param array|\ArrayObject $data the passed data must be understood by whatever encoding function is applied later.
208
     */
209
    public function setRoot($data)
210
    {
211
        $this->root = $data;
212
    }
213
214
    public function getResult()
215
    {
216
        return $this->getRoot();
217
    }
218
}
219