AbstractVisitor   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 95.12%

Importance

Changes 0
Metric Value
dl 0
loc 151
c 0
b 0
f 0
wmc 15
lcom 2
cbo 4
ccs 39
cts 41
cp 0.9512
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A prepare() 0 6 1
A visitArray() 0 13 2
A visitBoolean() 0 4 1
A visitFloat() 0 4 1
A visitInteger() 0 4 1
A visitNull() 0 4 1
A visitResource() 0 4 1
A visitString() 0 4 1
A startVisitingObject() 0 12 2
A visitObjectProperty() 0 17 3
A finishVisitingObject() 0 4 1
doVisitArray() 0 1 ?
doVisitObjectProperty() 0 6 ?
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 1472
    public function prepare($data, ContextInterface $context)
34
    {
35 1472
        $this->navigator = $context->getNavigator();
36
37 1472
        return $data;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 224
    public function visitArray($data, TypeMetadataInterface $type, ContextInterface $context)
44
    {
45 224
        $context->enterScope($data, $type);
46
47 224
        if ($context->getExclusionStrategy()->skipType($type, $context)) {
48 20
            $data = [];
49
        }
50
51 224
        $result = $this->doVisitArray($data, $type, $context);
52 224
        $context->leaveScope();
53
54 224
        return $result;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 72
    public function visitBoolean($data, TypeMetadataInterface $type, ContextInterface $context)
61
    {
62 72
        return $this->visitData((bool) $data, $type, $context);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 72
    public function visitFloat($data, TypeMetadataInterface $type, ContextInterface $context)
69
    {
70 72
        return $this->visitData((float) $data, $type, $context);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 268
    public function visitInteger($data, TypeMetadataInterface $type, ContextInterface $context)
77
    {
78 268
        return $this->visitData((int) $data, $type, $context);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 224
    public function visitNull($data, TypeMetadataInterface $type, ContextInterface $context)
85
    {
86 224
        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 772
    public function visitString($data, TypeMetadataInterface $type, ContextInterface $context)
101
    {
102 772
        return $this->visitData((string) $data, $type, $context);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 1088
    public function startVisitingObject($data, ClassMetadataInterface $class, ContextInterface $context)
109
    {
110 1088
        $context->enterScope($data, $class);
111
112 1088
        if ($context->getExclusionStrategy()->skipClass($class, $context)) {
113 32
            $context->leaveScope();
114
115 32
            return false;
116
        }
117
118 1088
        return true;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 1088
    public function visitObjectProperty($data, PropertyMetadataInterface $property, ContextInterface $context)
125
    {
126 1088
        $visited = false;
127
128 1088
        if (!$context->getExclusionStrategy()->skipProperty($property, $context)) {
129 1088
            $context->enterScope($data, $property);
130
131 1088
            $name = $property->hasAlias()
132 128
                ? $property->getAlias()
133 1088
                : $context->getNamingStrategy()->convert($property->getName());
134
135 1088
            $visited = $this->doVisitObjectProperty($data, $name, $property, $context);
136 1088
            $context->leaveScope();
137
        }
138
139 1088
        return $visited;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 1088
    public function finishVisitingObject($data, ClassMetadataInterface $class, ContextInterface $context)
146
    {
147 1088
        $context->leaveScope();
148 1088
    }
149
150
    /**
151
     * @param mixed                 $data
152
     * @param TypeMetadataInterface $type
153
     * @param ContextInterface      $context
154
     *
155
     * @return mixed
156
     */
157
    abstract protected function doVisitArray($data, TypeMetadataInterface $type, ContextInterface $context);
158
159
    /**
160
     * @param mixed                     $data
161
     * @param string                    $name
162
     * @param PropertyMetadataInterface $property
163
     * @param ContextInterface          $context
164
     *
165
     * @return bool
166
     */
167
    abstract protected function doVisitObjectProperty(
168
        $data,
169
        $name,
170
        PropertyMetadataInterface $property,
171
        ContextInterface $context
172
    );
173
}
174