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

VirtualProperty   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 20
ccs 5
cts 5
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A getSerializedName() 0 4 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