ArraySerializationVisitor::endVisitingObject()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 6
c 1
b 0
f 1
nc 3
nop 4
dl 0
loc 12
rs 10
1
<?php
2
3
/*
4
 * This file is part of PHP CS Fixer.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *     Dariusz Rumiński <[email protected]>
8
 *
9
 * This source file is subject to the MIT license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
13
namespace Etrias\EwarehousingConnector\Serializer;
14
15
use JMS\Serializer\Context;
16
use JMS\Serializer\GenericSerializationVisitor;
17
use JMS\Serializer\Metadata\ClassMetadata;
18
19
class ArraySerializationVisitor extends GenericSerializationVisitor
0 ignored issues
show
Deprecated Code introduced by
The class JMS\Serializer\GenericSerializationVisitor has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

19
class ArraySerializationVisitor extends /** @scrutinizer ignore-deprecated */ GenericSerializationVisitor
Loading history...
20
{
21
    /**
22
     * @return array
23
     */
24
    public function getResult()
25
    {
26
        return $this->getRoot();
27
    }
28
29
    /**
30
     * @param array   $data
31
     * @param array   $type
32
     * @param Context $context
33
     *
34
     * @return array|\ArrayObject|mixed
35
     */
36
    public function visitArray($data, array $type, Context $context)
37
    {
38
        $result = parent::visitArray($data, $type, $context);
39
        if (null !== $this->getRoot() && isset($type['params'][1]) && 0 === count($result)) {
40
            // ArrayObject is specially treated by the json_encode function and
41
            // serialized to { } while a mere array would be serialized to [].
42
            return new \ArrayObject();
43
        }
44
45
        return $result;
46
    }
47
48
    /**
49
     * @param ClassMetadata $metadata
50
     * @param mixed         $data
51
     * @param array         $type
52
     * @param Context       $context
53
     *
54
     * @return \ArrayObject
55
     */
56
    public function endVisitingObject(ClassMetadata $metadata, $data, array $type, Context $context)
57
    {
58
        $rs = parent::endVisitingObject($metadata, $data, $type, $context);
59
        // Force JSON output to "{}" instead of "[]" if it contains either no properties or all properties are null.
60
        if (empty($rs)) {
61
            $rs = new \ArrayObject();
62
            if ([] === $this->getRoot()) {
63
                $this->setRoot(clone $rs);
64
            }
65
        }
66
67
        return $rs;
68
    }
69
}
70