Completed
Push — master ( ddba2d...d7dfff )
by Eric
14:58
created

AbstractVisitor::enterScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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