Completed
Pull Request — master (#2079)
by Christian
11:52 queued 01:53
created

Context::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\Context;
13
14
use JMS\Serializer\Exclusion\ExclusionStrategyInterface;
15
16
/**
17
 * Stores the serialization or deserialization context (groups, version, ...).
18
 *
19
 * @author Ener-Getick <[email protected]>
20
 */
21
final class Context
22
{
23
    /**
24
     * @var array
25
     */
26
    private $attributes = array();
27
28
    /**
29
     * @var int|null
30
     */
31
    private $version;
32
33
    /**
34
     * @var array|null
35
     */
36
    private $groups;
37
38
    /**
39
     * @var int
40
     */
41
    private $maxDepth;
42
43
    /**
44
     * @var bool
45
     */
46
    private $isMaxDepthEnabled;
47
48
    /**
49
     * @var bool
50
     */
51
    private $serializeNull;
52
53
    /**
54
     * @var ExclusionStrategyInterface[]
55
     */
56
    private $exclusionStrategies = array();
57
58
    /**
59
     * Sets an attribute.
60 30
     *
61
     * @param string $key
62 30
     * @param mixed  $value
63
     *
64 30
     * @return self
65
     */
66
    public function setAttribute($key, $value)
67
    {
68
        $this->attributes[$key] = $value;
69
70
        return $this;
71
    }
72
73
    /**
74 1
     * Checks if contains a normalization attribute.
75
     *
76 1
     * @param string $key
77
     *
78
     * @return bool
79
     */
80
    public function hasAttribute($key)
81
    {
82
        return isset($this->attributes[$key]);
83
    }
84
85
    /**
86 2
     * Gets an attribute.
87
     *
88 2
     * @param string $key
89 2
     *
90
     * @return mixed
91
     */
92
    public function getAttribute($key)
93
    {
94
        if (isset($this->attributes[$key])) {
95
            return $this->attributes[$key];
96
        }
97
    }
98 19
99
    /**
100 19
     * Gets the attributes.
101
     *
102
     * @return array
103
     */
104
    public function getAttributes()
105
    {
106
        return $this->attributes;
107
    }
108
109
    /**
110 7
     * Sets the normalization version.
111
     *
112 7
     * @param int|null $version
113
     *
114 7
     * @return self
115
     */
116
    public function setVersion($version)
117
    {
118
        $this->version = $version;
119
120
        return $this;
121
    }
122 34
123
    /**
124 34
     * Gets the normalization version.
125
     *
126
     * @return int|null
127
     */
128
    public function getVersion()
129
    {
130
        return $this->version;
131
    }
132
133
    /**
134 6
     * Adds a normalization group.
135
     *
136 6
     * @param string $group
137 6
     *
138 6
     * @return self
139 6
     */
140 6
    public function addGroup($group)
141 6
    {
142
        if (null === $this->groups) {
143 6
            $this->groups = [];
144
        }
145
        if (!in_array($group, $this->groups)) {
146
            $this->groups[] = $group;
147
        }
148
149
        return $this;
150
    }
151
152
    /**
153 5
     * Adds normalization groups.
154
     *
155 5
     * @param string[] $groups
156 5
     *
157 5
     * @return self
158
     */
159 5
    public function addGroups(array $groups)
160
    {
161
        foreach ($groups as $group) {
162
            $this->addGroup($group);
163
        }
164
165
        return $this;
166
    }
167 37
168
    /**
169 37
     * Gets the normalization groups.
170
     *
171
     * @return string[]|null
172
     */
173
    public function getGroups()
174
    {
175
        return $this->groups;
176
    }
177
178
    /**
179 1
     * Set the normalization groups.
180
     *
181 1
     * @param string[]|null $groups
182
     *
183 1
     * @return self
184
     */
185
    public function setGroups(array $groups = null)
186
    {
187
        $this->groups = $groups;
188
189
        return $this;
190
    }
191
192
    public function enableMaxDepth()
193
    {
194
        $this->isMaxDepthEnabled = true;
195 3
196
        return $this;
197 3
    }
198 2
199 2
    public function disableMaxDepth()
200 3
    {
201
        $this->isMaxDepthEnabled = false;
202 3
203
        return $this;
204
    }
205
206
    /**
207
     * @return bool|null
208
     */
209
    public function isMaxDepthEnabled()
210
    {
211
        return $this->isMaxDepthEnabled;
212 20
    }
213
214 20
    /**
215 1
     * Sets serialize null.
216 1
     *
217
     * @param bool|null $serializeNull
218 20
     *
219
     * @return self
220
     */
221 2
    public function setSerializeNull($serializeNull)
222
    {
223 2
        $this->serializeNull = $serializeNull;
224
225 2
        return $this;
226
    }
227
228 1
    /**
229
     * Gets serialize null.
230 1
     *
231
     * @return bool|null
232 1
     */
233
    public function getSerializeNull()
234
    {
235
        return $this->serializeNull;
236
    }
237
238 19
    /**
239
     * Gets the array of exclusion strategies.
240 19
     *
241
     * Notice: This method only applies to the JMS serializer adapter.
242
     *
243
     * @return ExclusionStrategyInterface[]
244
     */
245
    public function getExclusionStrategies()
246
    {
247
        return $this->exclusionStrategies;
248
    }
249
250 20
    /**
251
     * Adds an exclusion strategy.
252 20
     *
253
     * Notice: This method only applies to the JMS serializer adapter.
254 20
     *
255
     * @param ExclusionStrategyInterface $exclusionStrategy
256
     */
257
    public function addExclusionStrategy(ExclusionStrategyInterface $exclusionStrategy)
258
    {
259
        $this->exclusionStrategies[] = $exclusionStrategy;
260
    }
261
}
262