Passed
Pull Request — master (#1394)
by
unknown
02:17
created

SerializationContext::getPropertyNamingStrategy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
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 JMS\Serializer\Naming\PropertyNamingStrategyInterface;
9
use Metadata\MetadataFactoryInterface;
10
11
class SerializationContext extends Context
12
{
13
    /** @var \SplObjectStorage */
14
    private $visitingSet;
15
16
    /** @var \SplStack */
17
    private $visitingStack;
18
19
    /**
20
     * @var string
21
     */
22
    private $initialType;
23 108
24
    /**
25 108
     * @var bool
26
     */
27
    private $serializeNull = false;
28
    /**
29
     * @var PropertyNamingStrategyInterface|null
30
     */
31 287
    private $propertyNamingStrategy = null;
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
    public function setPropertyNamingStrategy(PropertyNamingStrategyInterface $propertyNamingStrategy): self
59 194
    {
60
        $this->assertMutable();
61 208
        $this->propertyNamingStrategy = $propertyNamingStrategy;
62
63 208
        return $this;
64 6
    }
65
66
    /**
67 202
     * Returns TRUE when NULLs should be serialized
68
     * Returns FALSE when NULLs should not be serialized
69
     */
70 1
    public function shouldSerializeNull(): bool
71
    {
72 1
        return $this->serializeNull;
73 1
    }
74 1
75
    public function getPropertyNamingStrategy(): ?PropertyNamingStrategyInterface
76
    {
77 1
        return $this->propertyNamingStrategy;
78
    }
79
80
    /**
81 1
     * @param mixed $object
82
     */
83
    public function startVisiting($object): void
84 3
    {
85
        if (!\is_object($object)) {
86 3
            return;
87
        }
88
89 7
        $this->visitingSet->attach($object);
90
        $this->visitingStack->push($object);
91 7
    }
92
93
    /**
94 23
     * @param mixed $object
95
     */
96 23
    public function stopVisiting($object): void
97
    {
98
        if (!\is_object($object)) {
99
            return;
100
        }
101
102
        $this->visitingSet->detach($object);
103
        $poppedObject = $this->visitingStack->pop();
104
105
        if ($object !== $poppedObject) {
106
            throw new RuntimeException('Context visitingStack not working well');
107
        }
108
    }
109
110
    /**
111
     * @param mixed $object
112
     */
113 6
    public function isVisiting($object): bool
114
    {
115 6
        if (!\is_object($object)) {
116 6
            return false;
117 6
        }
118
119
        return $this->visitingSet->contains($object);
120 1
    }
121
122 1
    public function getPath(): ?string
123 1
    {
124 1
        $path = [];
125
        foreach ($this->visitingStack as $obj) {
126
            $path[] = \get_class($obj);
127
        }
128
129
        if (!$path) {
130
            return null;
131
        }
132
133
        return implode(' -> ', $path);
134
    }
135
136
    public function getDirection(): int
137
    {
138
        return GraphNavigatorInterface::DIRECTION_SERIALIZATION;
139
    }
140
141
    public function getDepth(): int
142
    {
143
        return $this->visitingStack->count();
144
    }
145
146
    public function getObject(): ?object
147
    {
148
        return !$this->visitingStack->isEmpty() ? $this->visitingStack->top() : null;
149
    }
150
151
    public function getVisitingStack(): \SplStack
152
    {
153
        return $this->visitingStack;
154
    }
155
156
    public function getVisitingSet(): \SplObjectStorage
157
    {
158
        return $this->visitingSet;
159
    }
160
161
    /**
162
     * @return $this
163
     */
164
    public function setInitialType(string $type): self
165
    {
166
        $this->assertMutable();
167
168
        $this->initialType = $type;
169
        $this->setAttribute('initial_type', $type);
170
171
        return $this;
172
    }
173
174
    public function getInitialType(): ?string
175
    {
176
        return $this->initialType
177
            ? $this->initialType
178
            : ($this->hasAttribute('initial_type') ? $this->getAttribute('initial_type') : null);
179
    }
180
}
181