Completed
Push — master ( d7dfff...a2b1ad )
by Eric
19:00 queued 13:27
created

AbstractVisitor::visitObjectProperty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 3
crap 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;
13
14
use Ivory\Serializer\Context\ContextInterface;
15
use Ivory\Serializer\Mapping\ClassMetadataInterface;
16
use Ivory\Serializer\Mapping\PropertyMetadataInterface;
17
use Ivory\Serializer\Mapping\TypeMetadataInterface;
18
use Ivory\Serializer\Navigator\NavigatorInterface;
19
20
/**
21
 * @author GeLo <[email protected]>
22
 */
23
abstract class AbstractVisitor implements VisitorInterface
24
{
25
    /**
26
     * @var NavigatorInterface
27
     */
28
    protected $navigator;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 549
    public function prepare($data, ContextInterface $context)
34
    {
35 549
        $this->navigator = $context->getNavigator();
36
37 549
        return $data;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 66
    public function visitArray($data, TypeMetadataInterface $type, ContextInterface $context)
44
    {
45 66
        $context->enterScope($data, $type);
46
47 66
        if ($context->getExclusionStrategy()->skipType($type, $context)) {
48 9
            $data = [];
49 6
        }
50
51 66
        $result = $this->doVisitArray($data, $type, $context);
52 66
        $context->leaveScope();
53
54 66
        return $result;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 45
    public function visitBoolean($data, TypeMetadataInterface $type, ContextInterface $context)
61
    {
62 45
        return $this->visitData((bool) $data, $type, $context);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 54
    public function visitFloat($data, TypeMetadataInterface $type, ContextInterface $context)
69
    {
70 54
        return $this->visitData((float) $data, $type, $context);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 96
    public function visitInteger($data, TypeMetadataInterface $type, ContextInterface $context)
77
    {
78 96
        return $this->visitData((int) $data, $type, $context);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 108
    public function visitNull($data, TypeMetadataInterface $type, ContextInterface $context)
85
    {
86 108
        return $this->visitData(null, $type, $context);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function visitResource($data, TypeMetadataInterface $type, ContextInterface $context)
93
    {
94
        throw new \RuntimeException('(De)-Serializing a resource is not supported.');
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 207
    public function visitString($data, TypeMetadataInterface $type, ContextInterface $context)
101
    {
102 207
        return $this->visitData((string) $data, $type, $context);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 387
    public function startVisitingObject($data, ClassMetadataInterface $class, ContextInterface $context)
109
    {
110 387
        if ($context->getExclusionStrategy()->skipClass($class, $context)) {
111 18
            return false;
112
        }
113
114 387
        $context->enterScope($data, $class);
115
116 387
        return true;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 387
    public function visitObjectProperty($data, PropertyMetadataInterface $property, ContextInterface $context)
123
    {
124 387
        $visited = false;
125
126 387
        if (!$context->getExclusionStrategy()->skipProperty($property, $context)) {
127 387
            $context->enterScope($data, $property);
128
129 387
            $name = $property->hasAlias()
130 282
                ? $property->getAlias()
131 387
                : $context->getNamingStrategy()->convert($property->getName());
132
133 387
            $visited = $this->doVisitObjectProperty($data, $name, $property, $context);
134 387
            $context->leaveScope();
135 258
        }
136
137 387
        return $visited;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 387
    public function finishVisitingObject($data, ClassMetadataInterface $class, ContextInterface $context)
144
    {
145 387
        $context->leaveScope();
146 387
    }
147
148
    /**
149
     * @param mixed                 $data
150
     * @param TypeMetadataInterface $type
151
     * @param ContextInterface      $context
152
     *
153
     * @return mixed
154
     */
155
    abstract protected function doVisitArray($data, TypeMetadataInterface $type, ContextInterface $context);
156
157
    /**
158
     * @param mixed                     $data
159
     * @param string                    $name
160
     * @param PropertyMetadataInterface $property
161
     * @param ContextInterface          $context
162
     *
163
     * @return bool
164
     */
165
    abstract protected function doVisitObjectProperty(
166
        $data,
167
        $name,
168
        PropertyMetadataInterface $property,
169
        ContextInterface $context
170
    );
171
}
172