Completed
Push — master ( b5207e...bb017f )
by Guilh
06:54
created

Context::getExclusionStrategies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
     * @var int|null
29
     */
30
    private $version;
31
    /**
32
     * @var array|null
33
     */
34
    private $groups;
35
    /**
36
     * @var int
37
     */
38
    private $maxDepth;
39
    /**
40
     * @var bool
41
     */
42
    private $isMaxDepthEnabled;
43
    /**
44
     * @var bool
45
     */
46
    private $serializeNull;
47
    /**
48
     * @var ExclusionStrategyInterface[]
49
     */
50
    private $exclusionStrategies = array();
51
52
    /**
53
     * Sets an attribute.
54
     *
55
     * @param string $key
56
     * @param mixed  $value
57
     *
58
     * @return self
59
     */
60 29
    public function setAttribute($key, $value)
61
    {
62 29
        $this->attributes[$key] = $value;
63
64 29
        return $this;
65
    }
66
67
    /**
68
     * Checks if contains a normalization attribute.
69
     *
70
     * @param string $key
71
     *
72
     * @return bool
73
     */
74 1
    public function hasAttribute($key)
75
    {
76 1
        return isset($this->attributes[$key]);
77
    }
78
79
    /**
80
     * Gets an attribute.
81
     *
82
     * @param string $key
83
     *
84
     * @return mixed
85
     */
86 2
    public function getAttribute($key)
87
    {
88 2
        if (isset($this->attributes[$key])) {
89 2
            return $this->attributes[$key];
90
        }
91
    }
92
93
    /**
94
     * Gets the attributes.
95
     *
96
     * @return array
97
     */
98 18
    public function getAttributes()
99
    {
100 18
        return $this->attributes;
101
    }
102
103
    /**
104
     * Sets the normalization version.
105
     *
106
     * @param int|null $version
107
     *
108
     * @return self
109
     */
110 6
    public function setVersion($version)
111
    {
112 6
        $this->version = $version;
113
114 6
        return $this;
115
    }
116
117
    /**
118
     * Gets the normalization version.
119
     *
120
     * @return int|null
121
     */
122 33
    public function getVersion()
123
    {
124 33
        return $this->version;
125
    }
126
127
    /**
128
     * Adds a normalization group.
129
     *
130
     * @param string $group
131
     *
132
     * @return self
133
     */
134 6
    public function addGroup($group)
135
    {
136 6
        if (null === $this->groups) {
137 6
            $this->groups = [];
138 6
        }
139 6
        if (!in_array($group, $this->groups)) {
140 6
            $this->groups[] = $group;
141 6
        }
142
143 6
        return $this;
144
    }
145
146
    /**
147
     * Adds normalization groups.
148
     *
149
     * @param string[] $groups
150
     *
151
     * @return self
152
     */
153 5
    public function addGroups(array $groups)
154
    {
155 5
        foreach ($groups as $group) {
156 5
            $this->addGroup($group);
157 5
        }
158
159 5
        return $this;
160
    }
161
162
    /**
163
     * Gets the normalization groups.
164
     *
165
     * @return string[]|null
166
     */
167 36
    public function getGroups()
168
    {
169 36
        return $this->groups;
170
    }
171
172
    /**
173
     * Set the normalization groups.
174
     *
175
     * @param string[]|null $groups
176
     *
177
     * @return self
178
     */
179 1
    public function setGroups(array $groups = null)
180
    {
181 1
        $this->groups = $groups;
182
183 1
        return $this;
184
    }
185
186
    /**
187
     * Sets the normalization max depth.
188
     *
189
     * @param int|null $maxDepth
190
     *
191
     * @return self
192
     *
193
     * @deprecated since 2.1, to be removed in 3.0. Use {@link self::enableMaxDepth()} and {@link self::disableMaxDepth()} instead
194
     */
195 3 View Code Duplication
    public function setMaxDepth($maxDepth)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
    {
197 3
        if (1 === func_num_args() || func_get_arg(1)) {
198 2
            @trigger_error(sprintf('%s is deprecated since version 2.1 and will be removed in 3.0. Use %s::enableMaxDepth() and %s::disableMaxDepth() instead.', __METHOD__, __CLASS__, __CLASS__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
199 2
        }
200 3
        $this->maxDepth = $maxDepth;
201
202 3
        return $this;
203
    }
204
205
    /**
206
     * Gets the normalization max depth.
207
     *
208
     * @return int|null
209
     *
210
     * @deprecated since version 2.1, to be removed in 3.0. Use {@link self::isMaxDepthEnabled()} instead
211
     */
212 19 View Code Duplication
    public function getMaxDepth()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213
    {
214 19
        if (0 === func_num_args() || func_get_arg(0)) {
215 1
            @trigger_error(sprintf('%s is deprecated since version 2.1 and will be removed in 3.0. Use %s::isMaxDepthEnabled() instead.', __METHOD__, __CLASS__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
216 1
        }
217
218 19
        return $this->maxDepth;
219
    }
220
221 2
    public function enableMaxDepth()
222
    {
223 2
        $this->isMaxDepthEnabled = true;
224
225 2
        return $this;
226
    }
227
228 1
    public function disableMaxDepth()
229
    {
230 1
        $this->isMaxDepthEnabled = false;
231
232 1
        return $this;
233
    }
234
235
    /**
236
     * @return bool|null
237
     */
238 18
    public function isMaxDepthEnabled()
239
    {
240 18
        return $this->isMaxDepthEnabled;
241
    }
242
243
    /**
244
     * Sets serialize null.
245
     *
246
     * @param bool|null $serializeNull
247
     *
248
     * @return self
249
     */
250 19
    public function setSerializeNull($serializeNull)
251
    {
252 19
        $this->serializeNull = $serializeNull;
253
254 19
        return $this;
255
    }
256
257
    /**
258
     * Gets serialize null.
259
     *
260
     * @return bool|null
261
     */
262 31
    public function getSerializeNull()
263
    {
264 31
        return $this->serializeNull;
265
    }
266
267
    /**
268
     * Gets the array of exclusion strategies.
269
     *
270
     * Notice: This method only applies to the JMS serializer adapter.
271
     *
272
     * @return ExclusionStrategyInterface[]
273
     */
274 6
    public function getExclusionStrategies()
275
    {
276 6
        return $this->exclusionStrategies;
277
    }
278
279
    /**
280
     * Adds an exclusion strategy.
281
     *
282
     * Notice: This method only applies to the JMS serializer adapter.
283
     *
284
     * @param ExclusionStrategyInterface $exclusionStrategy
285
     */
286 1
    public function addExclusionStrategy(ExclusionStrategyInterface $exclusionStrategy)
287
    {
288 1
        $this->exclusionStrategies[] = $exclusionStrategy;
289 1
    }
290
}
291