Completed
Push — master ( d8a83b...76c960 )
by Eric
02:56
created

PropertyMetadata::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer\Mapping;
13
14
/**
15
 * @author GeLo <[email protected]>
16
 */
17
class PropertyMetadata implements PropertyMetadataInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    private $name;
23
24
    /**
25
     * @var string
26
     */
27
    private $alias;
28
29
    /**
30
     * @var TypeMetadataInterface|null
31
     */
32
    private $type;
33
34
    /**
35
     * @var string|null
36
     */
37
    private $since;
38
39
    /**
40
     * @var string|null
41
     */
42
    private $until;
43
44
    /**
45
     * @var int|null
46
     */
47
    private $maxDepth;
48
49
    /**
50
     * @var string[]
51
     */
52
    private $groups = [];
53
54
    /**
55
     * @param string $name
56
     */
57
    public function __construct($name)
58
    {
59
        $this->setName($name);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getName()
66
    {
67
        return $this->name;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function setName($name)
74
    {
75
        $this->name = $name;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function hasAlias()
82
    {
83
        return $this->alias !== null;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getAlias()
90
    {
91
        return $this->alias;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function setAlias($alias)
98
    {
99
        $this->alias = $alias;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function hasType()
106
    {
107
        return $this->type !== null;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function getType()
114
    {
115
        return $this->type;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function setType(TypeMetadataInterface $type = null)
122
    {
123
        $this->type = $type;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function hasSinceVersion()
130
    {
131
        return $this->since !== null;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function getSinceVersion()
138
    {
139
        return $this->since;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function setSinceVersion($since)
146
    {
147
        $this->since = $since;
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function hasUntilVersion()
154
    {
155
        return $this->until !== null;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function getUntilVersion()
162
    {
163
        return $this->until;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function setUntilVersion($until)
170
    {
171
        $this->until = $until;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function hasMaxDepth()
178
    {
179
        return $this->maxDepth !== null;
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    public function getMaxDepth()
186
    {
187
        return $this->maxDepth;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function setMaxDepth($maxDepth)
194
    {
195
        $this->maxDepth = $maxDepth;
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    public function hasGroups()
202
    {
203
        return !empty($this->groups);
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209
    public function getGroups()
210
    {
211
        return $this->groups;
212
    }
213
214
    /**
215
     * {@inheritdoc}
216
     */
217
    public function setGroups(array $groups)
218
    {
219
        $this->groups = [];
220
        $this->addGroups($groups);
221
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226
    public function addGroups(array $groups)
227
    {
228
        foreach ($groups as $group) {
229
            $this->addGroup($group);
230
        }
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236
    public function hasGroup($group)
237
    {
238
        return in_array($group, $this->groups, true);
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     */
244
    public function addGroup($group)
245
    {
246
        if (!$this->hasGroup($group)) {
247
            $this->groups[] = $group;
248
        }
249
    }
250
251
    /**
252
     * {@inheritdoc}
253
     */
254
    public function removeGroup($group)
255
    {
256
        unset($this->groups[array_search($group, $this->groups, true)]);
257
        $this->groups = array_values($this->groups);
258
    }
259
260
    /**
261
     * {@inheritdoc}
262
     */
263
    public function merge(PropertyMetadataInterface $propertyMetadata)
264
    {
265
        if ($propertyMetadata->hasAlias()) {
266
            $this->setAlias($propertyMetadata->getAlias());
267
        }
268
269
        if ($propertyMetadata->hasType()) {
270
            $this->setType($propertyMetadata->getType());
271
        }
272
273
        if ($propertyMetadata->hasSinceVersion()) {
274
            $this->setSinceVersion($propertyMetadata->getSinceVersion());
275
        }
276
277
        if ($propertyMetadata->hasUntilVersion()) {
278
            $this->setUntilVersion($propertyMetadata->getUntilVersion());
279
        }
280
281
        if ($propertyMetadata->hasMaxDepth()) {
282
            $this->setMaxDepth($propertyMetadata->getMaxDepth());
283
        }
284
285
        foreach ($propertyMetadata->getGroups() as $group) {
286
            $this->addGroup($group);
287
        }
288
    }
289
290
    /**
291
     * {@inheritdoc}
292
     */
293
    public function serialize()
294
    {
295
        return serialize([
296
            $this->name,
297
            $this->alias,
298
            $this->type,
299
            $this->since,
300
            $this->until,
301
            $this->maxDepth,
302
            $this->groups
303
        ]);
304
    }
305
306
    /**
307
     * {@inheritdoc}
308
     */
309
    public function unserialize($serialized)
310
    {
311
        list(
312
            $this->name,
313
            $this->alias,
314
            $this->type,
315
            $this->since,
316
            $this->until,
317
            $this->maxDepth,
318
            $this->groups
319
        ) = unserialize($serialized);
320
    }
321
}
322