|
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\Internal; |
|
10
|
|
|
|
|
11
|
|
|
use Tebru\AnnotationReader\AbstractAnnotation; |
|
12
|
|
|
use Tebru\AnnotationReader\AnnotationCollection; |
|
13
|
|
|
use Tebru\Gson\ClassMetadata; |
|
14
|
|
|
use Tebru\Gson\Internal\Data\PropertyCollection; |
|
15
|
|
|
use Tebru\Gson\PropertyMetadata; |
|
16
|
|
|
use Tebru\Gson\PropertyMetadataCollection; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class DefaultClassMetadata |
|
20
|
|
|
* |
|
21
|
|
|
* Represents a class an its annotations |
|
22
|
|
|
* |
|
23
|
|
|
* This class contains public properties to improve performance. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Nate Brunette <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
final class DefaultClassMetadata implements ClassMetadata |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* The class name |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
public $name; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The class annotations |
|
38
|
|
|
* |
|
39
|
|
|
* @var AnnotationCollection |
|
40
|
|
|
*/ |
|
41
|
|
|
public $annotations; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var PropertyCollection |
|
45
|
|
|
*/ |
|
46
|
|
|
public $properties; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var bool |
|
50
|
|
|
*/ |
|
51
|
|
|
public $skipSerialize = false; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var bool |
|
55
|
|
|
*/ |
|
56
|
|
|
public $skipDeserialize = false; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Constructor |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $name |
|
62
|
|
|
* @param AnnotationCollection $annotations |
|
63
|
|
|
* @param PropertyCollection $properties |
|
64
|
|
|
*/ |
|
65
|
11 |
|
public function __construct(string $name, AnnotationCollection $annotations, PropertyCollection $properties) |
|
66
|
|
|
{ |
|
67
|
11 |
|
$this->name = $name; |
|
68
|
11 |
|
$this->annotations = $annotations; |
|
69
|
11 |
|
$this->properties = $properties; |
|
70
|
11 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get the class name as a string |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function getName(): string |
|
78
|
|
|
{ |
|
79
|
1 |
|
return $this->name; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get all class annotations |
|
84
|
|
|
* |
|
85
|
|
|
* @return AnnotationCollection |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function getAnnotations(): AnnotationCollection |
|
88
|
|
|
{ |
|
89
|
1 |
|
return $this->annotations; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get a specific annotation by class name, returns null if the annotation |
|
94
|
|
|
* doesn't exist. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $annotationClass |
|
97
|
|
|
* @return null|AbstractAnnotation |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function getAnnotation(string $annotationClass): ?AbstractAnnotation |
|
100
|
|
|
{ |
|
101
|
2 |
|
return $this->annotations->get($annotationClass); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get the [@see PropertyMetadataCollection] for class |
|
106
|
|
|
* |
|
107
|
|
|
* @return PropertyMetadataCollection |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function getPropertyMetadataCollection(): PropertyMetadataCollection |
|
110
|
|
|
{ |
|
111
|
1 |
|
return $this->properties; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get the [@see PropertyCollection] for class |
|
116
|
|
|
* |
|
117
|
|
|
* @return PropertyCollection |
|
118
|
|
|
*/ |
|
119
|
1 |
|
public function getPropertyCollection(): PropertyCollection |
|
120
|
|
|
{ |
|
121
|
1 |
|
return $this->properties; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Returns an array of [@see PropertyMetadata] objects |
|
126
|
|
|
* |
|
127
|
|
|
* @return PropertyMetadata[] |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function getPropertyMetadata(): array |
|
130
|
|
|
{ |
|
131
|
1 |
|
return $this->properties->toArray(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get [@see PropertyMetadata] by property name |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $propertyName |
|
138
|
|
|
* @return PropertyMetadata|null |
|
139
|
|
|
*/ |
|
140
|
2 |
|
public function getProperty(string $propertyName): ?PropertyMetadata |
|
141
|
|
|
{ |
|
142
|
2 |
|
return $this->properties->getByName($propertyName); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* If the class should be skipped during serialization |
|
147
|
|
|
* |
|
148
|
|
|
* @return bool |
|
149
|
|
|
*/ |
|
150
|
1 |
|
public function skipSerialize(): bool |
|
151
|
|
|
{ |
|
152
|
1 |
|
return $this->skipSerialize; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Set if we should skip serialization |
|
157
|
|
|
* |
|
158
|
|
|
* @param bool $skipSerialize |
|
159
|
|
|
*/ |
|
160
|
3 |
|
public function setSkipSerialize(bool $skipSerialize): void |
|
161
|
|
|
{ |
|
162
|
3 |
|
$this->skipSerialize = $skipSerialize; |
|
163
|
3 |
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* If the class should be skipped during deserialization |
|
167
|
|
|
* |
|
168
|
|
|
* @return bool |
|
169
|
|
|
*/ |
|
170
|
1 |
|
public function skipDeserialize(): bool |
|
171
|
|
|
{ |
|
172
|
1 |
|
return $this->skipDeserialize; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Set if we should skip deserialization |
|
177
|
|
|
* |
|
178
|
|
|
* @param bool $skipDeserialize |
|
179
|
|
|
*/ |
|
180
|
3 |
|
public function setSkipDeserialize(bool $skipDeserialize): void |
|
181
|
|
|
{ |
|
182
|
3 |
|
$this->skipDeserialize = $skipDeserialize; |
|
183
|
3 |
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|