Passed
Pull Request — master (#1375)
by Rene
03:24
created

SerializationContext::stopVisiting()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
ccs 0
cts 4
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer;
6
7
use JMS\Serializer\Exception\RuntimeException;
8
use Metadata\MetadataFactoryInterface;
9
10
class SerializationContext extends Context
11
{
12
    /** @var \SplObjectStorage */
13
    private $visitingSet;
14
15
    /** @var \SplStack */
16
    private $visitingStack;
17
18
    /**
19
     * @var string
20
     */
21
    private $initialType;
22
23 108
    /**
24
     * @var bool
25 108
     */
26
    private $serializeNull = false;
27
28
    /**
29
     * @return bool
30
     */
31 287
    private $serializeUnitializedNull = false;
32
33 287
    public static function create(): self
34
    {
35 287
        return new self();
36 287
    }
37 287
38
    public function initialize(string $format, VisitorInterface $visitor, GraphNavigatorInterface $navigator, MetadataFactoryInterface $factory): void
39 208
    {
40
        parent::initialize($format, $visitor, $navigator, $factory);
41 208
42 6
        $this->visitingSet = new \SplObjectStorage();
43
        $this->visitingStack = new \SplStack();
44 202
    }
45 202
46 202
    /**
47
     * Set if NULLs should be serialized (TRUE) ot not (FALSE)
48 200
     */
49
    public function setSerializeNull(bool $bool): self
50 200
    {
51 6
        $this->assertMutable();
52
53 194
        $this->serializeNull = $bool;
54 194
55
        return $this;
56 194
    }
57
58
59 194
    public function getShouldSerializeUnitializedAsNull(): bool
60
    {
61 208
        return $this->serializeUnitializedNull;
62
63 208
    }
64 6
65
    public function setShouldSerializeUnitializedAsNull(bool $bool): self
66
    {
67 202
        $this->assertMutable();
68
69
        $this->serializeUnitializedNull = $bool;
70 1
71
        return $this;
72 1
    }
73 1
74 1
    /**
75
     * Returns TRUE when NULLs should be serialized
76
     * Returns FALSE when NULLs should not be serialized
77 1
     */
78
    public function shouldSerializeNull(): bool
79
    {
80
        return $this->serializeNull;
81 1
    }
82
83
    /**
84 3
     * @param mixed $object
85
     */
86 3
    public function startVisiting($object): void
87
    {
88
        if (!\is_object($object)) {
89 7
            return;
90
        }
91 7
92
        $this->visitingSet->attach($object);
93
        $this->visitingStack->push($object);
94 23
    }
95
96 23
    /**
97
     * @param mixed $object
98
     */
99
    public function stopVisiting($object): void
100
    {
101
        if (!\is_object($object)) {
102
            return;
103
        }
104
105
        $this->visitingSet->detach($object);
106
        $poppedObject = $this->visitingStack->pop();
107
108
        if ($object !== $poppedObject) {
109
            throw new RuntimeException('Context visitingStack not working well');
110
        }
111
    }
112
113 6
    /**
114
     * @param mixed $object
115 6
     */
116 6
    public function isVisiting($object): bool
117 6
    {
118
        if (!\is_object($object)) {
119
            return false;
120 1
        }
121
122 1
        return $this->visitingSet->contains($object);
123 1
    }
124 1
125
    public function getPath(): ?string
126
    {
127
        $path = [];
128
        foreach ($this->visitingStack as $obj) {
129
            $path[] = \get_class($obj);
130
        }
131
132
        if (!$path) {
133
            return null;
134
        }
135
136
        return implode(' -> ', $path);
137
    }
138
139
    public function getDirection(): int
140
    {
141
        return GraphNavigatorInterface::DIRECTION_SERIALIZATION;
142
    }
143
144
    public function getDepth(): int
145
    {
146
        return $this->visitingStack->count();
147
    }
148
149
    public function getObject(): ?object
150
    {
151
        return !$this->visitingStack->isEmpty() ? $this->visitingStack->top() : null;
152
    }
153
154
    public function getVisitingStack(): \SplStack
155
    {
156
        return $this->visitingStack;
157
    }
158
159
    public function getVisitingSet(): \SplObjectStorage
160
    {
161
        return $this->visitingSet;
162
    }
163
164
    /**
165
     * @return $this
166
     */
167
    public function setInitialType(string $type): self
168
    {
169
        $this->assertMutable();
170
171
        $this->initialType = $type;
172
        $this->setAttribute('initial_type', $type);
173
174
        return $this;
175
    }
176
177
    public function getInitialType(): ?string
178
    {
179
        return $this->initialType
180
            ? $this->initialType
181
            : ($this->hasAttribute('initial_type') ? $this->getAttribute('initial_type') : null);
182
    }
183
}
184