Completed
Push — master ( 7ee585...e1c4bf )
by Johannes
04:20
created

SerializationContext::getVisitingStack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * Copyright 2013 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace JMS\Serializer;
20
21
use JMS\Serializer\Exception\LogicException;
22
use JMS\Serializer\Exception\RuntimeException;
23
use Metadata\MetadataFactoryInterface;
24
25
class SerializationContext extends Context
26
{
27
    /** @var \SplObjectStorage */
28
    private $visitingSet;
29
30
    /** @var \SplStack */
31
    private $visitingStack;
32
33 37
    public static function create()
34
    {
35 37
        return new self();
36
    }
37
38
    /**
39
     * @param string $format
40
     */
41 223
    public function initialize($format, VisitorInterface $visitor, GraphNavigator $navigator, MetadataFactoryInterface $factory)
42
    {
43 223
        parent::initialize($format, $visitor, $navigator, $factory);
44
45 223
        $this->visitingSet = new \SplObjectStorage();
46 223
        $this->visitingStack = new \SplStack();
47 223
    }
48
49 161
    public function startVisiting($object)
50
    {
51 161
        if ( ! is_object($object)) {
52 4
            return;
53
        }
54 157
        $this->visitingSet->attach($object);
55 157
        $this->visitingStack->push($object);
56 157
    }
57
58 156
    public function stopVisiting($object)
59
    {
60 156
        if ( ! is_object($object)) {
61 4
            return;
62
        }
63 152
        $this->visitingSet->detach($object);
64 153
        $poppedObject = $this->visitingStack->pop();
65
66 152
        if ($object !== $poppedObject) {
67
            throw new RuntimeException('Context visitingStack not working well');
68
        }
69 152
    }
70
71 161
    public function isVisiting($object)
72
    {
73 161
        if ( ! is_object($object)) {
74 4
            return false;
75
        }
76
77 157
        return $this->visitingSet->contains($object);
78
    }
79
80 1
    public function getPath()
81
    {
82 1
        $path = array();
83 1
        foreach ($this->visitingStack as $obj) {
84 1
            $path[] = get_class($obj);
85 1
        }
86
87 1
        if ( ! $path) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $path of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
88
            return null;
89
        }
90
91 1
        return implode(' -> ', $path);
92
    }
93
94 157
    public function getDirection()
95
    {
96 157
        return GraphNavigator::DIRECTION_SERIALIZATION;
97
    }
98
99 4
    public function getDepth()
100
    {
101 4
        return $this->visitingStack->count();
102
    }
103
104 2
    public function getObject()
105
    {
106 2
        return ! $this->visitingStack->isEmpty() ? $this->visitingStack->top() : null;
107
    }
108
109
    public function getVisitingStack()
110
    {
111
        return $this->visitingStack;
112
    }
113
114
    public function getVisitingSet()
115
    {
116
        return $this->visitingSet;
117
    }
118
}
119