|
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) { |
|
|
|
|
|
|
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
|
|
|
|
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.