AbstractGenericVisitor::visitData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 10
cc 2
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;
13
14
use Ivory\Serializer\Context\ContextInterface;
15
use Ivory\Serializer\Mapping\ClassMetadataInterface;
16
use Ivory\Serializer\Mapping\TypeMetadataInterface;
17
18
/**
19
 * @author GeLo <[email protected]>
20
 */
21
abstract class AbstractGenericVisitor extends AbstractVisitor
22
{
23
    /**
24
     * @var mixed[]
25
     */
26
    private $stack;
27
28
    /**
29
     * @var mixed
30
     */
31
    protected $result;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 1252
    public function prepare($data, ContextInterface $context)
37
    {
38 1252
        $this->stack = [];
39 1252
        $this->result = null;
40
41 1252
        return parent::prepare($data, $context);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 256
    public function visitArray($data, TypeMetadataInterface $type, ContextInterface $context)
48
    {
49 256
        $result = [];
50
51 256
        if (!empty($data)) {
52 168
            $this->enterScope();
53 168
            $result = parent::visitArray($data, $type, $context);
54 168
            $this->leaveScope();
55
        }
56
57 256
        return $this->visitData($result, $type, $context);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 1148
    public function visitData($data, TypeMetadataInterface $type, ContextInterface $context)
64
    {
65 1148
        if ($this->result === null) {
66 240
            $this->result = $data;
67
        }
68
69 1148
        return $data;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 920
    public function startVisitingObject($data, ClassMetadataInterface $class, ContextInterface $context)
76
    {
77 920
        if (!parent::startVisitingObject($data, $class, $context)) {
78 24
            return false;
79
        }
80
81 920
        $this->enterScope();
82 920
        $this->result = $this->createResult($class->getName());
83
84 920
        return true;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 920
    public function finishVisitingObject($data, ClassMetadataInterface $class, ContextInterface $context)
91
    {
92 920
        parent::finishVisitingObject($data, $class, $context);
93
94 920
        return $this->leaveScope();
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 568
    public function getResult()
101
    {
102 568
        return $this->result;
103
    }
104
105
    /**
106
     * @param string $class
107
     *
108
     * @return mixed
109
     */
110
    abstract protected function createResult($class);
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 156
    protected function doVisitArray($data, TypeMetadataInterface $type, ContextInterface $context)
116
    {
117 156
        $this->result = [];
118
119 156
        $keyType = $type->getOption('key');
120 156
        $valueType = $type->getOption('value');
121 156
        $ignoreNull = $context->isNullIgnored();
122
123 156
        foreach ($data as $key => $value) {
124 156
            $value = $this->navigator->navigate($value, $context, $valueType);
125
126 156
            if ($value === null && $ignoreNull) {
127 12
                continue;
128
            }
129
130 144
            $key = $this->navigator->navigate($key, $context, $keyType);
131 144
            $this->result[$key] = $value;
132
        }
133
134 156
        return $this->result;
135
    }
136
137 996
    private function enterScope()
138
    {
139 996
        $this->stack[] = $this->result;
140 996
    }
141
142
    /**
143
     * @return mixed
144
     */
145 996
    private function leaveScope()
146
    {
147 996
        $result = $this->result;
148 996
        $this->result = array_pop($this->stack);
149
150 996
        if ($this->result === null) {
151 996
            $this->result = $result;
152
        }
153
154 996
        return $result;
155
    }
156
}
157