Context::getGroups()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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
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
    private $attributes = array();
24
    private $version;
25
    private $groups;
26
    private $isMaxDepthEnabled;
27
    private $serializeNull;
28
29
    /**
30
     * @var ExclusionStrategyInterface[]
31
     */
32
    private $exclusionStrategies = array();
33
34 22
    public function setAttribute(string $key, $value): self
35
    {
36 22
        $this->attributes[$key] = $value;
37
38 22
        return $this;
39
    }
40
41 1
    public function hasAttribute(string $key): bool
42
    {
43 1
        return isset($this->attributes[$key]);
44
    }
45
46 2
    public function getAttribute(string $key)
47
    {
48 2
        if (isset($this->attributes[$key])) {
49 2
            return $this->attributes[$key];
50
        }
51
    }
52
53 24
    public function getAttributes(): array
54
    {
55 24
        return $this->attributes;
56
    }
57
58 12
    public function setVersion(string $version): self
59
    {
60 12
        $this->version = $version;
61
62 12
        return $this;
63
    }
64
65 42
    public function getVersion(): ?string
66
    {
67 42
        return $this->version;
68
    }
69
70
    /**
71
     * Adds a normalization group.
72
     */
73 6
    public function addGroup(string $group): self
74
    {
75 6
        if (null === $this->groups) {
76 6
            $this->groups = [];
77
        }
78 6
        if (!in_array($group, $this->groups)) {
79 6
            $this->groups[] = $group;
80
        }
81
82 6
        return $this;
83
    }
84
85
    /**
86
     * Adds normalization groups.
87
     *
88
     * @param string[] $groups
89
     */
90 5
    public function addGroups(array $groups): self
91
    {
92 5
        foreach ($groups as $group) {
93 5
            $this->addGroup($group);
94
        }
95
96 5
        return $this;
97
    }
98
99
    /**
100
     * Gets the normalization groups.
101
     *
102
     * @return string[]|null
103
     */
104 45
    public function getGroups(): ?array
105
    {
106 45
        return $this->groups;
107
    }
108
109
    /**
110
     * Set the normalization groups.
111
     *
112
     * @param string[]|null $groups
113
     */
114 4
    public function setGroups(array $groups = null): self
115
    {
116 4
        $this->groups = $groups;
117
118 4
        return $this;
119
    }
120
121 4
    public function enableMaxDepth(): self
122
    {
123 4
        $this->isMaxDepthEnabled = true;
124
125 4
        return $this;
126
    }
127
128 3
    public function disableMaxDepth(): self
129
    {
130 3
        $this->isMaxDepthEnabled = false;
131
132 3
        return $this;
133
    }
134
135 26
    public function isMaxDepthEnabled(): ?bool
136
    {
137 26
        return $this->isMaxDepthEnabled;
138
    }
139
140 17
    public function setSerializeNull(?bool $serializeNull): self
141
    {
142 17
        $this->serializeNull = $serializeNull;
143
144 17
        return $this;
145
    }
146
147 42
    public function getSerializeNull(): ?bool
148
    {
149 42
        return $this->serializeNull;
150
    }
151
152
    /**
153
     * Gets the array of exclusion strategies.
154
     *
155
     * Notice: This method only applies to the JMS serializer adapter.
156
     *
157
     * @return ExclusionStrategyInterface[]
158
     */
159 13
    public function getExclusionStrategies(): array
160
    {
161 13
        return $this->exclusionStrategies;
162
    }
163
164
    /**
165
     * Adds an exclusion strategy.
166
     *
167
     * Notice: This method only applies to the JMS serializer adapter.
168
     */
169 2
    public function addExclusionStrategy(ExclusionStrategyInterface $exclusionStrategy): void
170
    {
171 2
        $this->exclusionStrategies[] = $exclusionStrategy;
172 2
    }
173
}
174