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\Data; |
10
|
|
|
|
11
|
|
|
use Tebru\AnnotationReader\AbstractAnnotation; |
12
|
|
|
use Tebru\AnnotationReader\AnnotationCollection; |
13
|
|
|
use Tebru\Gson\ClassMetadata; |
14
|
|
|
use Tebru\Gson\Internal\DefaultClassMetadata; |
15
|
|
|
use Tebru\Gson\Internal\GetterStrategy; |
16
|
|
|
use Tebru\Gson\Internal\SetterStrategy; |
17
|
|
|
use Tebru\Gson\PropertyMetadata; |
18
|
|
|
use Tebru\PhpType\TypeToken; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Property |
22
|
|
|
* |
23
|
|
|
* Represents static information about an object property. Instances of this class may be |
24
|
|
|
* cached for later use. |
25
|
|
|
* |
26
|
|
|
* This class contains public properties to improve performance. |
27
|
|
|
* |
28
|
|
|
* @author Nate Brunette <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final class Property implements PropertyMetadata |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* The actual name of the property |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
public $realName; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The serialized version of the property name |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
public $serializedName; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The property type |
48
|
|
|
* |
49
|
|
|
* @var TypeToken |
50
|
|
|
*/ |
51
|
|
|
public $type; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* If the property is a scalar type |
55
|
|
|
* |
56
|
|
|
* @var bool |
57
|
|
|
*/ |
58
|
|
|
public $isScalar; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The method for getting values from this property |
62
|
|
|
* |
63
|
|
|
* @var GetterStrategy |
64
|
|
|
*/ |
65
|
|
|
public $getterStrategy; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The method for setting values to this property |
69
|
|
|
* |
70
|
|
|
* @var SetterStrategy |
71
|
|
|
*/ |
72
|
|
|
public $setterStrategy; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* A set of annotations |
76
|
|
|
* |
77
|
|
|
* @var AnnotationCollection |
78
|
|
|
*/ |
79
|
|
|
public $annotations; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* An integer that represents what modifiers are associated with the property |
83
|
|
|
* |
84
|
|
|
* These constants are defined in [@see \ReflectionProperty] |
85
|
|
|
* |
86
|
|
|
* @var int |
87
|
|
|
*/ |
88
|
|
|
public $modifiers; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* The property's class metadata |
92
|
|
|
* |
93
|
|
|
* @var DefaultClassMetadata |
94
|
|
|
*/ |
95
|
|
|
public $classMetadata; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* True if the property should be skipped during serialization |
99
|
|
|
* |
100
|
|
|
* @var bool |
101
|
|
|
*/ |
102
|
|
|
public $skipSerialize = false; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* True if the property should be skipped during deserialization |
106
|
|
|
* |
107
|
|
|
* @var bool |
108
|
|
|
*/ |
109
|
|
|
public $skipDeserialize = false; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* If the property is a virtual property |
113
|
|
|
* @var bool |
114
|
|
|
*/ |
115
|
|
|
public $virtual; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Constructor |
119
|
|
|
* |
120
|
|
|
* @param string $realName |
121
|
|
|
* @param string $serializedName |
122
|
|
|
* @param TypeToken $type |
123
|
|
|
* @param GetterStrategy $getterStrategy |
124
|
|
|
* @param SetterStrategy $setterStrategy |
125
|
|
|
* @param AnnotationCollection $annotations |
126
|
|
|
* @param int $modifiers |
127
|
|
|
* @param bool $virtual |
128
|
|
|
* @param ClassMetadata $classMetadata |
129
|
|
|
*/ |
130
|
10 |
|
public function __construct( |
131
|
|
|
string $realName, |
132
|
|
|
string $serializedName, |
133
|
|
|
TypeToken $type, |
134
|
|
|
GetterStrategy $getterStrategy, |
135
|
|
|
SetterStrategy $setterStrategy, |
136
|
|
|
AnnotationCollection $annotations, |
137
|
|
|
int $modifiers, |
138
|
|
|
bool $virtual, |
139
|
|
|
ClassMetadata $classMetadata |
140
|
|
|
) { |
141
|
10 |
|
$this->realName = $realName; |
142
|
10 |
|
$this->serializedName = $serializedName; |
143
|
10 |
|
$this->type = $type; |
144
|
10 |
|
$this->isScalar = $type->isScalar() && $type->genericTypes === []; |
145
|
10 |
|
$this->getterStrategy = $getterStrategy; |
146
|
10 |
|
$this->setterStrategy = $setterStrategy; |
147
|
10 |
|
$this->annotations = $annotations; |
148
|
10 |
|
$this->modifiers = $modifiers; |
149
|
10 |
|
$this->virtual = $virtual; |
150
|
10 |
|
$this->classMetadata = $classMetadata; |
|
|
|
|
151
|
10 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get the real name of the property |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
1 |
|
public function getName(): string |
159
|
|
|
{ |
160
|
1 |
|
return $this->realName; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get the serialized name of the property |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
10 |
|
public function getSerializedName(): string |
169
|
|
|
{ |
170
|
10 |
|
return $this->serializedName; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get the property type |
175
|
|
|
* |
176
|
|
|
* @return TypeToken |
177
|
|
|
*/ |
178
|
1 |
|
public function getType(): TypeToken |
179
|
|
|
{ |
180
|
1 |
|
return $this->type; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get the property type as a string |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
1 |
|
public function getTypeName(): string |
189
|
|
|
{ |
190
|
1 |
|
return (string)$this->type; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* The property modifiers |
195
|
|
|
* |
196
|
|
|
* @return int |
197
|
|
|
*/ |
198
|
1 |
|
public function getModifiers(): int |
199
|
|
|
{ |
200
|
1 |
|
return $this->modifiers; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get full declaring class metadata |
205
|
|
|
* |
206
|
|
|
* @return ClassMetadata |
207
|
|
|
*/ |
208
|
1 |
|
public function getDeclaringClassMetadata(): ClassMetadata |
209
|
|
|
{ |
210
|
1 |
|
return $this->classMetadata; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get the declaring class name |
215
|
|
|
* |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
1 |
|
public function getDeclaringClassName(): string |
219
|
|
|
{ |
220
|
1 |
|
return $this->classMetadata->name; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Return the collection of annotations |
225
|
|
|
* |
226
|
|
|
* @return AnnotationCollection |
227
|
|
|
*/ |
228
|
3 |
|
public function getAnnotations(): AnnotationCollection |
229
|
|
|
{ |
230
|
3 |
|
return $this->annotations; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Get a single annotation, returns null if the annotation doesn't exist |
235
|
|
|
* |
236
|
|
|
* @param string $annotationName |
237
|
|
|
* @return null|AbstractAnnotation |
238
|
|
|
*/ |
239
|
1 |
|
public function getAnnotation(string $annotationName): ?AbstractAnnotation |
240
|
|
|
{ |
241
|
1 |
|
return $this->annotations->get($annotationName); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Returns true if the property is virtual |
246
|
|
|
* |
247
|
|
|
* @return bool |
248
|
|
|
*/ |
249
|
1 |
|
public function isVirtual(): bool |
250
|
|
|
{ |
251
|
1 |
|
return $this->virtual; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Returns should if we should skip during serialization |
256
|
|
|
* |
257
|
|
|
* @return bool |
258
|
|
|
*/ |
259
|
2 |
|
public function skipSerialize(): bool |
260
|
|
|
{ |
261
|
2 |
|
return $this->skipSerialize; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Set whether we should skip during serialization |
266
|
|
|
* |
267
|
|
|
* @param bool $skipSerialize |
268
|
|
|
* @return PropertyMetadata |
269
|
|
|
*/ |
270
|
3 |
|
public function setSkipSerialize(bool $skipSerialize): PropertyMetadata |
271
|
|
|
{ |
272
|
3 |
|
$this->skipSerialize = $skipSerialize; |
273
|
|
|
|
274
|
3 |
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Returns should if we should skip during deserialization |
279
|
|
|
* |
280
|
|
|
* @return bool |
281
|
|
|
*/ |
282
|
2 |
|
public function skipDeserialize(): bool |
283
|
|
|
{ |
284
|
2 |
|
return $this->skipDeserialize; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Set whether we should skip during deserialization |
289
|
|
|
* |
290
|
|
|
* @param bool $skipDeserialize |
291
|
|
|
* @return PropertyMetadata |
292
|
|
|
*/ |
293
|
3 |
|
public function setSkipDeserialize(bool $skipDeserialize): PropertyMetadata |
294
|
|
|
{ |
295
|
3 |
|
$this->skipDeserialize = $skipDeserialize; |
296
|
|
|
|
297
|
3 |
|
return $this; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Given an object, get the value at this property |
302
|
|
|
* |
303
|
|
|
* @param object $object |
304
|
|
|
* @return mixed |
305
|
|
|
*/ |
306
|
6 |
|
public function get($object) |
307
|
|
|
{ |
308
|
6 |
|
return $this->getterStrategy->get($object); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Given an object and value, set the value to the object at this property |
313
|
|
|
* |
314
|
|
|
* @param object $object |
315
|
|
|
* @param mixed $value |
316
|
|
|
*/ |
317
|
6 |
|
public function set($object, $value): void |
318
|
|
|
{ |
319
|
6 |
|
$this->setterStrategy->set($object, $value); |
320
|
6 |
|
} |
321
|
|
|
} |
322
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.