Completed
Pull Request — master (#11)
by Eric
65:27
created

PropertyMetadata::serialize()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 21
nc 1
nop 0
crap 2
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
     * @var bool
81
     */
82
    private $xmlAttribute = false;
83 1640
84
    /**
85 1640
     * @var bool
86 1640
     */
87 1640
    private $xmlValue = false;
88
89
    /**
90
     * @var bool
91
     */
92 1420
    private $xmlInline = false;
93
94 1420
    /**
95
     * @var string|null
96
     */
97
    private $xmlEntry;
98
99
    /**
100 1640
     * @var string|null
101
     */
102 1640
    private $xmlEntryAttribute;
103 1640
104
    /**
105
     * @var bool|null
106
     */
107
    private $xmlKeyAsAttribute;
108 8
109
    /**
110 8
     * @var bool|null
111
     */
112
    private $xmlKeyAsNode;
113
114
    /**
115
     * @param string $name
116 1640
     * @param string $class
117
     */
118 1640
    public function __construct($name, $class)
119 1640
    {
120
        $this->setName($name);
121
        $this->setClass($class);
122
    }
123
124 1372
    /**
125
     * {@inheritdoc}
126 1372
     */
127
    public function getName()
128
    {
129
        return $this->name;
130
    }
131
132 476
    /**
133
     * {@inheritdoc}
134 476
     */
135
    public function setName($name)
136
    {
137
        $this->name = $name;
138
    }
139
140 152
    /**
141
     * {@inheritdoc}
142 152
     */
143 152
    public function getClass()
144
    {
145
        return $this->class;
146
    }
147
148 348
    /**
149
     * {@inheritdoc}
150 348
     */
151
    public function setClass($class)
152
    {
153
        $this->class = $class;
154
    }
155
156 1152
    /**
157
     * {@inheritdoc}
158 1152
     */
159
    public function hasAlias()
160
    {
161
        return $this->alias !== null;
162
    }
163
164 1152
    /**
165
     * {@inheritdoc}
166 1152
     */
167 1152
    public function getAlias()
168
    {
169
        return $this->alias;
170
    }
171
172 988
    /**
173
     * {@inheritdoc}
174 988
     */
175
    public function setAlias($alias)
176
    {
177
        $this->alias = $alias;
178
    }
179
180 1564
    /**
181
     * {@inheritdoc}
182 1564
     */
183 1564
    public function hasType()
184
    {
185
        return $this->type !== null;
186
    }
187
188 732
    /**
189
     * {@inheritdoc}
190 732
     */
191
    public function getType()
192
    {
193
        return $this->type;
194
    }
195
196 1560
    /**
197
     * {@inheritdoc}
198 1560
     */
199 1560
    public function setType(TypeMetadataInterface $type = null)
200
    {
201
        $this->type = $type;
202
    }
203
204 988
    /**
205
     * {@inheritdoc}
206 988
     */
207
    public function isReadable()
208
    {
209
        return $this->readable;
210
    }
211
212 364
    /**
213
     * {@inheritdoc}
214 364
     */
215
    public function setReadable($readable)
216
    {
217
        $this->readable = $readable;
218
    }
219
220 56
    /**
221
     * {@inheritdoc}
222 56
     */
223 56
    public function isWritable()
224
    {
225
        return $this->writable;
226
    }
227
228 652
    /**
229
     * {@inheritdoc}
230 652
     */
231
    public function setWritable($writable)
232
    {
233
        $this->writable = $writable;
234
    }
235
236 364
    /**
237
     * {@inheritdoc}
238 364
     */
239
    public function hasAccessor()
240
    {
241
        return $this->accessor !== null;
242
    }
243
244 56
    /**
245
     * {@inheritdoc}
246 56
     */
247 56
    public function getAccessor()
248
    {
249
        return $this->accessor;
250
    }
251
252 412
    /**
253
     * {@inheritdoc}
254 412
     */
255
    public function setAccessor($accessor)
256
    {
257
        $this->accessor = $accessor;
258
    }
259
260 412
    /**
261
     * {@inheritdoc}
262 412
     */
263
    public function hasMutator()
264
    {
265
        return $this->mutator !== null;
266
    }
267
268 120
    /**
269
     * {@inheritdoc}
270 120
     */
271 120
    public function getMutator()
272
    {
273
        return $this->mutator;
274
    }
275
276 412
    /**
277
     * {@inheritdoc}
278 412
     */
279
    public function setMutator($mutator)
280
    {
281
        $this->mutator = $mutator;
282
    }
283
284 412
    /**
285
     * {@inheritdoc}
286 412
     */
287
    public function hasSinceVersion()
288
    {
289
        return $this->since !== null;
290
    }
291
292 120
    /**
293
     * {@inheritdoc}
294 120
     */
295 120
    public function getSinceVersion()
296
    {
297
        return $this->since;
298
    }
299
300 380
    /**
301
     * {@inheritdoc}
302 380
     */
303
    public function setSinceVersion($since)
304
    {
305
        $this->since = $since;
306
    }
307
308 380
    /**
309
     * {@inheritdoc}
310 380
     */
311
    public function hasUntilVersion()
312
    {
313
        return $this->until !== null;
314
    }
315
316 88
    /**
317
     * {@inheritdoc}
318 88
     */
319 88
    public function getUntilVersion()
320
    {
321
        return $this->until;
322
    }
323
324 356
    /**
325
     * {@inheritdoc}
326 356
     */
327
    public function setUntilVersion($until)
328
    {
329
        $this->until = $until;
330
    }
331
332 360
    /**
333
     * {@inheritdoc}
334 360
     */
335
    public function hasMaxDepth()
336
    {
337
        return $this->maxDepth !== null;
338
    }
339
340 12
    /**
341
     * {@inheritdoc}
342 12
     */
343 12
    public function getMaxDepth()
344 12
    {
345
        return $this->maxDepth;
346
    }
347
348
    /**
349 12
     * {@inheritdoc}
350
     */
351 12
    public function setMaxDepth($maxDepth)
352 12
    {
353 6
        $this->maxDepth = $maxDepth;
354 12
    }
355
356
    /**
357
     * {@inheritdoc}
358
     */
359 148
    public function hasGroups()
360
    {
361 148
        return !empty($this->groups);
362
    }
363
364
    /**
365
     * {@inheritdoc}
366
     */
367 148
    public function getGroups()
368
    {
369 148
        return array_keys($this->groups);
370 148
    }
371 74
372 148
    /**
373
     * {@inheritdoc}
374
     */
375
    public function setGroups(array $groups)
376
    {
377 4
        $this->groups = [];
378
        $this->addGroups($groups);
379 4
    }
380 4
381
    /**
382
     * {@inheritdoc}
383
     */
384
    public function addGroups(array $groups)
385 4
    {
386
        foreach ($groups as $group) {
387 4
            $this->addGroup($group);
388 4
        }
389
    }
390 4
391 4
    /**
392 2
     * {@inheritdoc}
393
     */
394 4
    public function hasGroup($group)
395 4
    {
396 2
        return isset($this->groups[$group]);
397
    }
398 4
399 4
    /**
400 2
     * {@inheritdoc}
401
     */
402 4
    public function addGroup($group)
403 4
    {
404 2
        if (!$this->hasGroup($group)) {
405
            $this->groups[$group] = true;
406 4
        }
407 4
    }
408 2
409
    /**
410 4
     * {@inheritdoc}
411 4
     */
412 2
    public function removeGroup($group)
413
    {
414 4
        unset($this->groups[$group]);
415 4
    }
416 2
417
    /**
418 4
     * {@inheritdoc}
419 4
     */
420 2
    public function isXmlAttribute()
421 4
    {
422
        return $this->xmlAttribute;
423
    }
424
425
    /**
426 4
     * {@inheritdoc}
427
     */
428 4
    public function setXmlAttribute($xmlAttribute)
429 4
    {
430 4
        $this->xmlAttribute = $xmlAttribute;
431 4
    }
432 4
433 4
    /**
434 4
     * {@inheritdoc}
435 4
     */
436 4
    public function isXmlValue()
437 4
    {
438 4
        return $this->xmlValue;
439 4
    }
440 4
441 2
    /**
442
     * {@inheritdoc}
443
     */
444
    public function setXmlValue($xmlValue)
445
    {
446
        $this->xmlValue = $xmlValue;
447 4
    }
448
449
    /**
450 4
     * {@inheritdoc}
451 4
     */
452 4
    public function isXmlInline()
453 4
    {
454 4
        return $this->xmlInline;
455 4
    }
456 4
457 4
    /**
458 4
     * {@inheritdoc}
459 4
     */
460 4
    public function setXmlInline($xmlInline)
461 4
    {
462 4
        $this->xmlInline = $xmlInline;
463 4
    }
464
465
    /**
466
     * {@inheritdoc}
467
     */
468
    public function hasXmlEntry()
469
    {
470
        return $this->xmlEntry !== null;
471
    }
472
473
    /**
474
     * {@inheritdoc}
475
     */
476
    public function getXmlEntry()
477
    {
478
        return $this->xmlEntry;
479
    }
480
481
    /**
482
     * {@inheritdoc}
483
     */
484
    public function setXmlEntry($xmlEntry)
485
    {
486
        $this->xmlEntry = $xmlEntry;
487
    }
488
489
    /**
490
     * {@inheritdoc}
491
     */
492
    public function hasXmlEntryAttribute()
493
    {
494
        return $this->xmlEntryAttribute !== null;
495
    }
496
497
    /**
498
     * {@inheritdoc}
499
     */
500
    public function getXmlEntryAttribute()
501
    {
502
        return $this->xmlEntryAttribute;
503
    }
504
505
    /**
506
     * {@inheritdoc}
507
     */
508
    public function setXmlEntryAttribute($xmlEntryAttribute)
509
    {
510
        $this->xmlEntryAttribute = $xmlEntryAttribute;
511
    }
512
513
    /**
514
     * {@inheritdoc}
515
     */
516
    public function hasXmlKeyAsAttribute()
517
    {
518
        return $this->xmlKeyAsAttribute !== null;
519
    }
520
521
    /**
522
     * {@inheritdoc}
523
     */
524
    public function useXmlKeyAsAttribute()
525
    {
526
        return $this->xmlKeyAsAttribute;
527
    }
528
529
    /**
530
     * {@inheritdoc}
531
     */
532
    public function setXmlKeyAsAttribute($xmlKeyAsAttribute)
533
    {
534
        $this->xmlKeyAsAttribute = $xmlKeyAsAttribute;
535
    }
536
537
    /**
538
     * {@inheritdoc}
539
     */
540
    public function hasXmlKeyAsNode()
541
    {
542
        return $this->xmlKeyAsNode !== null;
543
    }
544
545
    /**
546
     * {@inheritdoc}
547
     */
548
    public function useXmlKeyAsNode()
549
    {
550
        return $this->xmlKeyAsNode;
551
    }
552
553
    /**
554
     * {@inheritdoc}
555
     */
556
    public function setXmlKeyAsNode($xmlKeyAsNode)
557
    {
558
        $this->xmlKeyAsNode = $xmlKeyAsNode;
559
    }
560
561
    /**
562
     * {@inheritdoc}
563
     */
564
    public function merge(PropertyMetadataInterface $propertyMetadata)
565
    {
566
        $this->setReadable($propertyMetadata->isReadable());
567
        $this->setWritable($propertyMetadata->isWritable());
568
        $this->setXmlAttribute($propertyMetadata->isXmlAttribute());
569
        $this->setXmlValue($propertyMetadata->isXmlValue());
570
        $this->setXmlInline($propertyMetadata->isXmlInline());
571
572
        if ($propertyMetadata->hasAlias()) {
573
            $this->setAlias($propertyMetadata->getAlias());
574
        }
575
576
        if ($propertyMetadata->hasType()) {
577
            $this->setType($propertyMetadata->getType());
578
        }
579
580
        if ($propertyMetadata->hasAccessor()) {
581
            $this->setAccessor($propertyMetadata->getAccessor());
582
        }
583
584
        if ($propertyMetadata->hasMutator()) {
585
            $this->setMutator($propertyMetadata->getMutator());
586
        }
587
588
        if ($propertyMetadata->hasSinceVersion()) {
589
            $this->setSinceVersion($propertyMetadata->getSinceVersion());
590
        }
591
592
        if ($propertyMetadata->hasUntilVersion()) {
593
            $this->setUntilVersion($propertyMetadata->getUntilVersion());
594
        }
595
596
        if ($propertyMetadata->hasMaxDepth()) {
597
            $this->setMaxDepth($propertyMetadata->getMaxDepth());
598
        }
599
600
        foreach ($propertyMetadata->getGroups() as $group) {
601
            $this->addGroup($group);
602
        }
603
604
        if ($propertyMetadata->hasXmlEntry()) {
605
            $this->setXmlEntry($propertyMetadata->getXmlEntry());
606
        }
607
608
        if ($propertyMetadata->hasXmlEntryAttribute()) {
609
            $this->setXmlEntryAttribute($propertyMetadata->getXmlEntryAttribute());
610
        }
611
612
        if ($propertyMetadata->hasXmlKeyAsAttribute()) {
613
            $this->setXmlKeyAsAttribute($propertyMetadata->useXmlKeyAsAttribute());
614
        }
615
616
        if ($propertyMetadata->hasXmlKeyAsNode()) {
617
            $this->setXmlKeyAsNode($propertyMetadata->useXmlKeyAsNode());
618
        }
619
    }
620
621
    /**
622
     * {@inheritdoc}
623
     */
624
    public function serialize()
625
    {
626
        return serialize([
627
            $this->name,
628
            $this->class,
629
            $this->alias,
630
            $this->type,
631
            $this->readable,
632
            $this->writable,
633
            $this->accessor,
634
            $this->mutator,
635
            $this->since,
636
            $this->until,
637
            $this->maxDepth,
638
            $this->groups,
639
            $this->xmlAttribute,
640
            $this->xmlValue,
641
            $this->xmlInline,
642
            $this->xmlEntry,
643
            $this->xmlEntryAttribute,
644
            $this->xmlKeyAsAttribute,
645
            $this->xmlKeyAsNode,
646
        ]);
647
    }
648
649
    /**
650
     * {@inheritdoc}
651
     */
652
    public function unserialize($serialized)
653
    {
654
        list(
655
            $this->name,
656
            $this->class,
657
            $this->alias,
658
            $this->type,
659
            $this->readable,
660
            $this->writable,
661
            $this->accessor,
662
            $this->mutator,
663
            $this->since,
664
            $this->until,
665
            $this->maxDepth,
666
            $this->groups,
667
            $this->xmlAttribute,
668
            $this->xmlValue,
669
            $this->xmlInline,
670
            $this->xmlEntry,
671
            $this->xmlEntryAttribute,
672
            $this->xmlKeyAsAttribute,
673
            $this->xmlKeyAsNode
674
        ) = unserialize($serialized);
675
    }
676
}
677