Completed
Push — master ( 8eabd5...5d0f28 )
by Guilh
08:14
created

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