Completed
Push — master ( 665986...1514e7 )
by Guilh
08:09 queued 05:04
created

Context::addGroups()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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
/**
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
31
     */
32
    private $groups = array();
33
    /**
34
     * @var int
35
     */
36
    private $maxDepth;
37
    /**
38
     * @var bool
39
     */
40
    private $serializeNull;
41
42
    /**
43
     * Sets an attribute.
44
     *
45
     * @param string $key
46
     * @param mixed  $value
47
     *
48
     * @return self
49
     */
50 29
    public function setAttribute($key, $value)
51
    {
52 29
        $this->attributes[$key] = $value;
53
54 29
        return $this;
55
    }
56
57
    /**
58
     * Checks if contains a normalization attribute.
59
     *
60
     * @param string $key
61
     *
62
     * @return bool
63
     */
64 1
    public function hasAttribute($key)
65
    {
66 1
        return isset($this->attributes[$key]);
67
    }
68
69
    /**
70
     * Gets an attribute.
71
     *
72
     * @param string $key
73
     *
74
     * @return mixed
75
     */
76 2
    public function getAttribute($key)
77
    {
78 2
        if (isset($this->attributes[$key])) {
79 2
            return $this->attributes[$key];
80
        }
81
    }
82
83
    /**
84
     * Gets the attributes.
85
     *
86
     * @return array
87
     */
88 18
    public function getAttributes()
89
    {
90 18
        return $this->attributes;
91
    }
92
93
    /**
94
     * Sets the normalization version.
95
     *
96
     * @param int|null $version
97
     *
98
     * @return self
99
     */
100 6
    public function setVersion($version)
101
    {
102 6
        $this->version = $version;
103
104 6
        return $this;
105
    }
106
107
    /**
108
     * Gets the normalization version.
109
     *
110
     * @return int|null
111
     */
112 33
    public function getVersion()
113
    {
114 33
        return $this->version;
115
    }
116
117
    /**
118
     * Adds a normalization group.
119
     *
120
     * @param string $group
121
     *
122
     * @return self
123
     */
124 6
    public function addGroup($group)
125
    {
126 6
        if (!in_array($group, $this->groups)) {
127 6
            $this->groups[] = $group;
128 6
        }
129
130 6
        return $this;
131
    }
132
133
    /**
134
     * Adds normalization groups.
135
     *
136
     * @param string[] $groups
137
     *
138
     * @return self
139
     */
140 5
    public function addGroups(array $groups)
141
    {
142 5
        foreach ($groups as $group) {
143 5
            $this->addGroup($group);
144 5
        }
145
146 5
        return $this;
147
    }
148
149
    /**
150
     * Gets the normalization groups.
151
     *
152
     * @return string[]
153
     */
154 36
    public function getGroups()
155
    {
156 36
        return $this->groups;
157
    }
158
159
    /**
160
     * Set the normalization groups.
161
     *
162
     * @param string[] $groups
163
     *
164
     * @return self
165
     */
166 1
    public function setGroups(array $groups)
167
    {
168 1
        $this->groups = $groups;
169
170 1
        return $this;
171
    }
172
173
    /**
174
     * Sets the normalization max depth.
175
     *
176
     * @param int|null $maxDepth
177
     *
178
     * @return self
179
     */
180 2
    public function setMaxDepth($maxDepth)
181
    {
182 2
        $this->maxDepth = $maxDepth;
183
184 2
        return $this;
185
    }
186
187
    /**
188
     * Gets the normalization max depth.
189
     *
190
     * @return int|null
191
     */
192 19
    public function getMaxDepth()
193
    {
194 19
        return $this->maxDepth;
195
    }
196
197
    /**
198
     * Sets serialize null.
199
     *
200
     * @param bool|null $serializeNull
201
     *
202
     * @return self
203
     */
204 19
    public function setSerializeNull($serializeNull)
205
    {
206 19
        $this->serializeNull = $serializeNull;
207
208 19
        return $this;
209
    }
210
211
    /**
212
     * Gets serialize null.
213
     *
214
     * @return bool|null
215
     */
216 31
    public function getSerializeNull()
217
    {
218 31
        return $this->serializeNull;
219
    }
220
}
221