Passed
Push — master ( b4f2b9...02a3b1 )
by Nate
05:21
created

DefaultClassMetadata   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getName() 0 4 1
A getAnnotations() 0 4 1
A getAnnotation() 0 4 1
A getPropertyMetadata() 0 4 1
A getProperty() 0 10 3
A addPropertyMetadata() 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
namespace Tebru\Gson\Internal;
8
9
use Tebru\Gson\ClassMetadata;
10
use Tebru\Gson\Internal\Data\AnnotationSet;
11
use Tebru\Gson\PropertyMetadata;
12
13
/**
14
 * Class DefaultClassMetadata
15
 *
16
 * Represents a class an its annotations
17
 *
18
 * @author Nate Brunette <[email protected]>
19
 */
20
final class DefaultClassMetadata implements ClassMetadata
21
{
22
    /**
23
     * The class name
24
     *
25
     * @var string
26
     */
27
    private $name;
28
29
    /**
30
     * The class annotations
31
     *
32
     * @var AnnotationSet
33
     */
34
    private $annotations;
35
36
    /**
37
     * An array of [@see PropertyMetadata] objects
38
     *
39
     * @var PropertyMetadata[]
40
     */
41
    private $propertyMetadata;
42
43
    /**
44
     * Constructor
45
     *
46
     * @param string $name
47
     * @param AnnotationSet $annotations
48
     */
49 8
    public function __construct(string $name, AnnotationSet $annotations)
50
    {
51 8
        $this->name = $name;
52 8
        $this->annotations = $annotations;
53 8
    }
54
55
    /**
56
     * Get the class name as a string
57
     *
58
     * @return string
59
     */
60 2
    public function getName(): string
61
    {
62 2
        return $this->name;
63
    }
64
65
    /**
66
     * Get all class annotations
67
     *
68
     * @return AnnotationSet
69
     */
70 1
    public function getAnnotations(): AnnotationSet
71
    {
72 1
        return $this->annotations;
73
    }
74
75
    /**
76
     * Get a specific annotation by class name, returns null if the annotation
77
     * doesn't exist.
78
     *
79
     * @param string $annotationClass
80
     * @return null|object
81
     */
82 2
    public function getAnnotation(string $annotationClass)
83
    {
84 2
        return $this->annotations->getAnnotation($annotationClass, AnnotationSet::TYPE_CLASS);
85
    }
86
87
    /**
88
     * Returns an array of [@see PropertyMetadata] objects
89
     *
90
     * @return array
91
     */
92 1
    public function getPropertyMetadata(): array
93
    {
94 1
        return $this->propertyMetadata;
95
    }
96
97
    /**
98
     * Get [@see PropertyMetadata] by property name
99
     *
100
     * @param string $propertyName
101
     * @return PropertyMetadata|null
102
     */
103 1
    public function getProperty(string $propertyName): ?PropertyMetadata
104
    {
105 1
        foreach ($this->propertyMetadata as $property) {
106 1
            if ($property->getName() === $propertyName) {
107 1
                return $property;
108
            }
109
        }
110
111 1
        return null;
112
    }
113
114
    /**
115
     * Add [@see PropertyMetadata] link
116
     *
117
     * @param PropertyMetadata $propertyMetadata
118
     * @return void
119
     */
120 5
    public function addPropertyMetadata(PropertyMetadata $propertyMetadata): void
121
    {
122 5
        $this->propertyMetadata[] = $propertyMetadata;
123 5
    }
124
}
125