Completed
Push — master ( 1856ca...43d45a )
by Eric
03:58
created

JsonSerializationVisitor::doVisitArray()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 4
nc 2
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer\Visitor\Json;
13
14
use Ivory\Serializer\Accessor\AccessorInterface;
15
use Ivory\Serializer\Context\ContextInterface;
16
use Ivory\Serializer\Exclusion\ExclusionStrategyInterface;
17
use Ivory\Serializer\Mapping\TypeMetadataInterface;
18
use Ivory\Serializer\Naming\NamingStrategyInterface;
19
use Ivory\Serializer\Visitor\AbstractSerializationVisitor;
20
21
/**
22
 * @author GeLo <[email protected]>
23
 */
24
class JsonSerializationVisitor extends AbstractSerializationVisitor
25
{
26
    /**
27
     * @var int
28
     */
29
    private $options;
30
31
    /**
32
     * @param AccessorInterface               $accessor
33
     * @param ExclusionStrategyInterface|null $exclusionStrategy
34
     * @param NamingStrategyInterface|null    $namingStrategy
35
     * @param int                             $options
36
     */
37
    public function __construct(
38
        AccessorInterface $accessor,
39
        ExclusionStrategyInterface $exclusionStrategy = null,
40
        NamingStrategyInterface $namingStrategy = null,
41
        $options = 0
42
    ) {
43
        parent::__construct($accessor, $exclusionStrategy, $namingStrategy);
44
45
        $this->options = $options;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function visitData($data, TypeMetadataInterface $type, ContextInterface $context)
52
    {
53
        if ($data === [] && class_exists($type->getName())) {
54
            $data = (object) $data;
55
        }
56
57
        return parent::visitData($data, $type, $context);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 View Code Duplication
    protected function encode($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        $result = @json_encode($data, $this->options);
66
67
        if (json_last_error() !== JSON_ERROR_NONE) {
68
            throw new \InvalidArgumentException(json_last_error_msg());
69
        }
70
71
        return $result;
72
    }
73
}
74