Completed
Push — master ( 808187...59f278 )
by Eric
04:10
created

PropertyMetadata::getGroups()   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 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 $class;
28
29
    /**
30
     * @var string
31
     */
32
    private $alias;
33
34
    /**
35
     * @var TypeMetadataInterface|null
36
     */
37
    private $type;
38
39
    /**
40
     * @var bool
41
     */
42
    private $readable = true;
43
44
    /**
45
     * @var bool
46
     */
47
    private $writable = true;
48
49
    /**
50
     * @var string|null
51
     */
52
    private $accessor;
53
54
    /**
55
     * @var string|null
56
     */
57
    private $mutator;
58
59
    /**
60
     * @var string|null
61
     */
62
    private $since;
63
64
    /**
65
     * @var string|null
66
     */
67
    private $until;
68
69
    /**
70
     * @var int|null
71
     */
72
    private $maxDepth;
73
74
    /**
75
     * @var string[]
76
     */
77
    private $groups = [];
78
79
    /**
80
     * @param string $name
81
     * @param string $class
82
     */
83 1640
    public function __construct($name, $class)
84
    {
85 1640
        $this->setName($name);
86 1640
        $this->setClass($class);
87 1640
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 1420
    public function getName()
93
    {
94 1420
        return $this->name;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 1640
    public function setName($name)
101
    {
102 1640
        $this->name = $name;
103 1640
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 8
    public function getClass()
109
    {
110 8
        return $this->class;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 1640
    public function setClass($class)
117
    {
118 1640
        $this->class = $class;
119 1640
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 1372
    public function hasAlias()
125
    {
126 1372
        return $this->alias !== null;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 476
    public function getAlias()
133
    {
134 476
        return $this->alias;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 152
    public function setAlias($alias)
141
    {
142 152
        $this->alias = $alias;
143 152
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 348
    public function hasType()
149
    {
150 348
        return $this->type !== null;
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156 1152
    public function getType()
157
    {
158 1152
        return $this->type;
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164 1152
    public function setType(TypeMetadataInterface $type = null)
165
    {
166 1152
        $this->type = $type;
167 1152
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172 988
    public function isReadable()
173
    {
174 988
        return $this->readable;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180 1564
    public function setReadable($readable)
181
    {
182 1564
        $this->readable = $readable;
183 1564
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188 732
    public function isWritable()
189
    {
190 732
        return $this->writable;
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196 1560
    public function setWritable($writable)
197
    {
198 1560
        $this->writable = $writable;
199 1560
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204 988
    public function hasAccessor()
205
    {
206 988
        return $this->accessor !== null;
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212 364
    public function getAccessor()
213
    {
214 364
        return $this->accessor;
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220 56
    public function setAccessor($accessor)
221
    {
222 56
        $this->accessor = $accessor;
223 56
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228 652
    public function hasMutator()
229
    {
230 652
        return $this->mutator !== null;
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236 364
    public function getMutator()
237
    {
238 364
        return $this->mutator;
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     */
244 56
    public function setMutator($mutator)
245
    {
246 56
        $this->mutator = $mutator;
247 56
    }
248
249
    /**
250
     * {@inheritdoc}
251
     */
252 412
    public function hasSinceVersion()
253
    {
254 412
        return $this->since !== null;
255
    }
256
257
    /**
258
     * {@inheritdoc}
259
     */
260 412
    public function getSinceVersion()
261
    {
262 412
        return $this->since;
263
    }
264
265
    /**
266
     * {@inheritdoc}
267
     */
268 120
    public function setSinceVersion($since)
269
    {
270 120
        $this->since = $since;
271 120
    }
272
273
    /**
274
     * {@inheritdoc}
275
     */
276 412
    public function hasUntilVersion()
277
    {
278 412
        return $this->until !== null;
279
    }
280
281
    /**
282
     * {@inheritdoc}
283
     */
284 412
    public function getUntilVersion()
285
    {
286 412
        return $this->until;
287
    }
288
289
    /**
290
     * {@inheritdoc}
291
     */
292 120
    public function setUntilVersion($until)
293
    {
294 120
        $this->until = $until;
295 120
    }
296
297
    /**
298
     * {@inheritdoc}
299
     */
300 380
    public function hasMaxDepth()
301
    {
302 380
        return $this->maxDepth !== null;
303
    }
304
305
    /**
306
     * {@inheritdoc}
307
     */
308 380
    public function getMaxDepth()
309
    {
310 380
        return $this->maxDepth;
311
    }
312
313
    /**
314
     * {@inheritdoc}
315
     */
316 88
    public function setMaxDepth($maxDepth)
317
    {
318 88
        $this->maxDepth = $maxDepth;
319 88
    }
320
321
    /**
322
     * {@inheritdoc}
323
     */
324 356
    public function hasGroups()
325
    {
326 356
        return !empty($this->groups);
327
    }
328
329
    /**
330
     * {@inheritdoc}
331
     */
332 360
    public function getGroups()
333
    {
334 360
        return array_keys($this->groups);
335
    }
336
337
    /**
338
     * {@inheritdoc}
339
     */
340 12
    public function setGroups(array $groups)
341
    {
342 12
        $this->groups = [];
343 12
        $this->addGroups($groups);
344 12
    }
345
346
    /**
347
     * {@inheritdoc}
348
     */
349 12
    public function addGroups(array $groups)
350
    {
351 12
        foreach ($groups as $group) {
352 12
            $this->addGroup($group);
353 6
        }
354 12
    }
355
356
    /**
357
     * {@inheritdoc}
358
     */
359 148
    public function hasGroup($group)
360
    {
361 148
        return isset($this->groups[$group]);
362
    }
363
364
    /**
365
     * {@inheritdoc}
366
     */
367 148
    public function addGroup($group)
368
    {
369 148
        if (!$this->hasGroup($group)) {
370 148
            $this->groups[$group] = true;
371 74
        }
372 148
    }
373
374
    /**
375
     * {@inheritdoc}
376
     */
377 4
    public function removeGroup($group)
378
    {
379 4
        unset($this->groups[$group]);
380 4
    }
381
382
    /**
383
     * {@inheritdoc}
384
     */
385 4
    public function merge(PropertyMetadataInterface $propertyMetadata)
386
    {
387 4
        $this->setReadable($propertyMetadata->isReadable());
388 4
        $this->setWritable($propertyMetadata->isWritable());
389
390 4
        if ($propertyMetadata->hasAlias()) {
391 4
            $this->setAlias($propertyMetadata->getAlias());
392 2
        }
393
394 4
        if ($propertyMetadata->hasType()) {
395 4
            $this->setType($propertyMetadata->getType());
396 2
        }
397
398 4
        if ($propertyMetadata->hasAccessor()) {
399 4
            $this->setAccessor($propertyMetadata->getAccessor());
400 2
        }
401
402 4
        if ($propertyMetadata->hasMutator()) {
403 4
            $this->setMutator($propertyMetadata->getMutator());
404 2
        }
405
406 4
        if ($propertyMetadata->hasSinceVersion()) {
407 4
            $this->setSinceVersion($propertyMetadata->getSinceVersion());
408 2
        }
409
410 4
        if ($propertyMetadata->hasUntilVersion()) {
411 4
            $this->setUntilVersion($propertyMetadata->getUntilVersion());
412 2
        }
413
414 4
        if ($propertyMetadata->hasMaxDepth()) {
415 4
            $this->setMaxDepth($propertyMetadata->getMaxDepth());
416 2
        }
417
418 4
        foreach ($propertyMetadata->getGroups() as $group) {
419 4
            $this->addGroup($group);
420 2
        }
421 4
    }
422
423
    /**
424
     * {@inheritdoc}
425
     */
426 4
    public function serialize()
427
    {
428 4
        return serialize([
429 4
            $this->name,
430 4
            $this->class,
431 4
            $this->alias,
432 4
            $this->type,
433 4
            $this->readable,
434 4
            $this->writable,
435 4
            $this->accessor,
436 4
            $this->mutator,
437 4
            $this->since,
438 4
            $this->until,
439 4
            $this->maxDepth,
440 4
            $this->groups,
441 2
        ]);
442
    }
443
444
    /**
445
     * {@inheritdoc}
446
     */
447 4
    public function unserialize($serialized)
448
    {
449
        list(
450 4
            $this->name,
451 4
            $this->class,
452 4
            $this->alias,
453 4
            $this->type,
454 4
            $this->readable,
455 4
            $this->writable,
456 4
            $this->accessor,
457 4
            $this->mutator,
458 4
            $this->since,
459 4
            $this->until,
460 4
            $this->maxDepth,
461 4
            $this->groups
462 4
        ) = unserialize($serialized);
463 4
    }
464
}
465