Completed
Push — master ( a7a790...5c4323 )
by Eric
03:39
created

JsonSerializationVisitor::finishVisitingObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 3
crap 2
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\Mapping\ClassMetadataInterface;
17
use Ivory\Serializer\Mapping\TypeMetadataInterface;
18
use Ivory\Serializer\Visitor\AbstractSerializationVisitor;
19
20
/**
21
 * @author GeLo <[email protected]>
22
 */
23
class JsonSerializationVisitor extends AbstractSerializationVisitor
24
{
25
    /**
26
     * @var int
27
     */
28
    private $options;
29
30
    /**
31
     * @param AccessorInterface $accessor
32
     * @param int               $options
33
     */
34 738
    public function __construct(AccessorInterface $accessor, $options = 0)
35
    {
36 738
        parent::__construct($accessor);
37
38 738
        $this->options = $options;
39 738
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 147
    public function visitData($data, TypeMetadataInterface $type, ContextInterface $context)
45
    {
46 147
        if ($data === [] && class_exists($type->getName())) {
47 3
            $data = (object) $data;
48 2
        }
49
50 147
        return parent::visitData($data, $type, $context);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 120
    public function finishVisitingObject($data, ClassMetadataInterface $class, ContextInterface $context)
57
    {
58 120
        if ($this->result === []) {
59 3
            $this->result = (object) $this->result;
60 2
        }
61
62 120
        return parent::finishVisitingObject($data, $class, $context);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 147 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...
69
    {
70 147
        $result = @json_encode($data, $this->options);
71
72 147
        if (json_last_error() !== JSON_ERROR_NONE) {
73
            throw new \InvalidArgumentException(json_last_error_msg());
74
        }
75
76 147
        return $result;
77
    }
78
}
79