Completed
Push — master ( b27dd7...71d714 )
by Nate
04:54
created

VirtualProperty::getSerializedName()   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
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
declare(strict_types=1);
8
9
namespace Tebru\Gson\Annotation;
10
11
use Tebru\AnnotationReader\AbstractAnnotation;
12
13
/**
14
 * Class VirtualProperty
15
 *
16
 * This allows a method to be used as a property during serialization only. This
17
 * is helpful if your serialized models need to contain extra properties.  For example,
18
 * an aggregate of two separate properties on the model.
19
 *
20
 * If used on a class it will add a wrapped property for serialization or deserialization.
21
 *
22
 * For example, if you had this json
23
 *
24
 * {"data": {"id": 1}}
25
 *
26
 * You could deserialize into a class with an id property that has @VirtualProperty("data") on the
27
 * class. Similarly, serializing that object would produce the above json.
28
 *
29
 * The value of this annotation acts as the serialized name. If a SerializedName annotation also exists, that will
30
 * take precedence. Please note that this will always wrap classes of the specified type, regardless of where they
31
 * occur in the tree.
32
 *
33
 * @author Nate Brunette <[email protected]>
34
 * @Annotation
35
 * @Target({"CLASS", "METHOD"})
36
 */
37
class VirtualProperty extends AbstractAnnotation
38
{
39
    /**
40
     * Initialize annotation data
41
     */
42 2
    protected function init(): void
43
    {
44 2
        $this->value = $this->data['value'] ?? null;
45 2
    }
46
47
    /**
48
     * Return the serialized name if it exists or null
49
     *
50
     * @return null|string
51
     */
52 1
    public function getSerializedName(): ?string
53
    {
54 1
        return $this->value;
55
    }
56
}
57