Completed
Push — master ( 59f278...6d6ddd )
by Eric
04:14
created

AbstractDeserializationVisitor::createResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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;
13
14
use Ivory\Serializer\Context\ContextInterface;
15
use Ivory\Serializer\Instantiator\InstantiatorInterface;
16
use Ivory\Serializer\Mapping\PropertyMetadataInterface;
17
use Ivory\Serializer\Mapping\TypeMetadataInterface;
18
use Ivory\Serializer\Mutator\MutatorInterface;
19
20
/**
21
 * @author GeLo <[email protected]>
22
 */
23
abstract class AbstractDeserializationVisitor extends AbstractGenericVisitor
24
{
25
    /**
26
     * @var InstantiatorInterface
27
     */
28
    private $instantiator;
29
30
    /**
31
     * @var MutatorInterface
32
     */
33
    private $mutator;
34
35
    /**
36
     * @param InstantiatorInterface $instantiator
37
     * @param MutatorInterface      $mutator
38
     */
39 1312
    public function __construct(InstantiatorInterface $instantiator, MutatorInterface $mutator)
40
    {
41 1312
        $this->instantiator = $instantiator;
42 1312
        $this->mutator = $mutator;
43 1312
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 528
    public function prepare($data, ContextInterface $context)
49
    {
50 528
        return $this->decode(parent::prepare($data, $context));
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 48
    public function visitBoolean($data, TypeMetadataInterface $type, ContextInterface $context)
57
    {
58 48
        return parent::visitBoolean(
59 48
            filter_var($data, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE),
60 24
            $type,
61
            $context
62 24
        );
63
    }
64
65
    /**
66
     * @param mixed $data
67
     *
68
     * @return mixed
69
     */
70
    abstract protected function decode($data);
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 384
    protected function doVisitObjectProperty(
76
        $data,
77
        $name,
78
        PropertyMetadataInterface $property,
79
        ContextInterface $context
80
    ) {
81 384
        $type = $property->getType();
82
83 384
        if ($type === null) {
84
            throw new \RuntimeException(sprintf(
85
                'You must define the type of the %s:%s.',
86
                $property->getName(),
87
                $property->getClass()
88
            ));
89
        }
90
91 384
        if (!$property->isWritable() || !isset($data[$name])) {
92 184
            return false;
93
        }
94
95 304
        $value = $this->navigator->navigate($data[$name], $context, $type);
96
97 304
        if ($value === null && $context->isNullIgnored()) {
98
            return false;
99
        }
100
101 304
        $this->mutator->setValue(
102 304
            $this->result,
103 304
            $property->hasMutator() ? $property->getMutator() : $property->getName(),
104
            $value
105 152
        );
106
107 304
        return true;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 384
    protected function createResult($class)
114
    {
115 384
        return $this->instantiator->instantiate($class);
116
    }
117
}
118