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\Data; |
8
|
|
|
|
9
|
|
|
use Tebru\Gson\Internal\GetterStrategy; |
10
|
|
|
use Tebru\Gson\PhpType; |
11
|
|
|
use Tebru\Gson\Internal\SetterStrategy; |
12
|
|
|
use Tebru\Gson\JsonReadable; |
13
|
|
|
use Tebru\Gson\JsonWritable; |
14
|
|
|
use Tebru\Gson\TypeAdapter; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Property |
18
|
|
|
* |
19
|
|
|
* Represents static information about an object property. Instances of this class may be |
20
|
|
|
* cached for later use. |
21
|
|
|
* |
22
|
|
|
* @author Nate Brunette <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class Property |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* The actual name of the property |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $realName; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The serialized version of the property name |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $serializedName; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The property type |
42
|
|
|
* |
43
|
|
|
* @var PhpType |
44
|
|
|
*/ |
45
|
|
|
private $type; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The method for getting values from this property |
49
|
|
|
* |
50
|
|
|
* @var GetterStrategy |
51
|
|
|
*/ |
52
|
|
|
private $getterStrategy; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The method for setting values to this property |
56
|
|
|
* |
57
|
|
|
* @var SetterStrategy |
58
|
|
|
*/ |
59
|
|
|
private $setterStrategy; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* A set of annotations |
63
|
|
|
* |
64
|
|
|
* @var AnnotationSet |
65
|
|
|
*/ |
66
|
|
|
private $annotations; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* An integer that represents what modifiers are associated with the property |
70
|
|
|
* |
71
|
|
|
* These constants are defined in [@see \ReflectionProperty] |
72
|
|
|
* |
73
|
|
|
* @var int |
74
|
|
|
*/ |
75
|
|
|
private $modifiers; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* True if the property should be skipped during serialization |
79
|
|
|
* |
80
|
|
|
* @var bool |
81
|
|
|
*/ |
82
|
|
|
private $skipSerialize = false; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* True if the property should be skipped during deserialization |
86
|
|
|
* |
87
|
|
|
* @var bool |
88
|
|
|
*/ |
89
|
|
|
private $skipDeserialize = false; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* The type adapter that should be used for this property |
93
|
|
|
* |
94
|
|
|
* @var TypeAdapter |
95
|
|
|
*/ |
96
|
|
|
private $typeAdapter; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* If the property is a virtual property |
100
|
|
|
* @var bool |
101
|
|
|
*/ |
102
|
|
|
private $virtual; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Constructor |
106
|
|
|
* |
107
|
|
|
* @param string $realName |
108
|
|
|
* @param string $serializedName |
109
|
|
|
* @param PhpType $type |
110
|
|
|
* @param GetterStrategy $getterStrategy |
111
|
|
|
* @param SetterStrategy $setterStrategy |
112
|
|
|
* @param AnnotationSet $annotations |
113
|
|
|
* @param int $modifiers |
114
|
|
|
* @param bool $virtual |
115
|
|
|
*/ |
116
|
10 |
View Code Duplication |
public function __construct( |
|
|
|
|
117
|
|
|
string $realName, |
118
|
|
|
string $serializedName, |
119
|
|
|
PhpType $type, |
120
|
|
|
GetterStrategy $getterStrategy, |
121
|
|
|
SetterStrategy $setterStrategy, |
122
|
|
|
AnnotationSet $annotations, |
123
|
|
|
int $modifiers, |
124
|
|
|
bool $virtual |
125
|
|
|
) |
126
|
|
|
{ |
127
|
10 |
|
$this->realName = $realName; |
128
|
10 |
|
$this->serializedName = $serializedName; |
129
|
10 |
|
$this->type = $type; |
130
|
10 |
|
$this->getterStrategy = $getterStrategy; |
131
|
10 |
|
$this->setterStrategy = $setterStrategy; |
132
|
10 |
|
$this->annotations = $annotations; |
133
|
10 |
|
$this->modifiers = $modifiers; |
134
|
10 |
|
$this->virtual = $virtual; |
135
|
10 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the real name of the property |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
1 |
|
public function getRealName(): string |
143
|
|
|
{ |
144
|
1 |
|
return $this->realName; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the serialized name of the property |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
1 |
|
public function getSerializedName(): string |
153
|
|
|
{ |
154
|
1 |
|
return $this->serializedName; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the property type |
159
|
|
|
* |
160
|
|
|
* @return PhpType |
161
|
|
|
*/ |
162
|
1 |
|
public function getType(): PhpType |
163
|
|
|
{ |
164
|
1 |
|
return $this->type; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Return the collection of annotations |
169
|
|
|
* |
170
|
|
|
* @return AnnotationSet |
171
|
|
|
*/ |
172
|
1 |
|
public function getAnnotations(): AnnotationSet |
173
|
|
|
{ |
174
|
1 |
|
return $this->annotations; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* The property modifiers |
179
|
|
|
* |
180
|
|
|
* @return int |
181
|
|
|
*/ |
182
|
1 |
|
public function getModifiers(): int |
183
|
|
|
{ |
184
|
1 |
|
return $this->modifiers; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns true if the property is virtual |
189
|
|
|
* |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
1 |
|
public function isVirtual(): bool |
193
|
|
|
{ |
194
|
1 |
|
return $this->virtual; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Set the type adapter |
199
|
|
|
* |
200
|
|
|
* This method exists so we can create a Property object and use it as a data bag to determine |
201
|
|
|
* if the property should be excluded before trying to resolve the type adapter for the property. |
202
|
|
|
* This avoids infinite loops on circular references. |
203
|
|
|
* |
204
|
|
|
* @param TypeAdapter $typeAdapter |
205
|
|
|
*/ |
206
|
2 |
|
public function setTypeAdapter(TypeAdapter $typeAdapter) |
207
|
|
|
{ |
208
|
2 |
|
$this->typeAdapter = $typeAdapter; |
209
|
2 |
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Returns should if we should skip during serialization |
213
|
|
|
* |
214
|
|
|
* @return bool |
215
|
|
|
*/ |
216
|
2 |
|
public function skipSerialize(): bool |
217
|
|
|
{ |
218
|
2 |
|
return $this->skipSerialize; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Set whether we should skip during serialization |
223
|
|
|
* |
224
|
|
|
* @param bool $skipSerialize |
225
|
|
|
*/ |
226
|
1 |
|
public function setSkipSerialize(bool $skipSerialize): void |
227
|
|
|
{ |
228
|
1 |
|
$this->skipSerialize = $skipSerialize; |
229
|
1 |
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Returns should if we should skip during deserialization |
233
|
|
|
* |
234
|
|
|
* @return bool |
235
|
|
|
*/ |
236
|
2 |
|
public function skipDeserialize(): bool |
237
|
|
|
{ |
238
|
2 |
|
return $this->skipDeserialize; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Set whether we should skip during deserialization |
243
|
|
|
* |
244
|
|
|
* @param bool $skipDeserialize |
245
|
|
|
*/ |
246
|
1 |
|
public function setSkipDeserialize(bool $skipDeserialize): void |
247
|
|
|
{ |
248
|
1 |
|
$this->skipDeserialize = $skipDeserialize; |
249
|
1 |
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Read the next value using the type adapter registered to property |
253
|
|
|
* and set it to the object |
254
|
|
|
* |
255
|
|
|
* @param JsonReadable $reader |
256
|
|
|
* @param mixed $object |
257
|
|
|
*/ |
258
|
1 |
|
public function read(JsonReadable $reader, $object) |
259
|
|
|
{ |
260
|
1 |
|
$value = $this->typeAdapter->read($reader); |
261
|
1 |
|
$this->set($object, $value); |
262
|
1 |
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Write the next value using the type adapter registered to property by |
266
|
|
|
* getting it from the property |
267
|
|
|
* |
268
|
|
|
* @param JsonWritable $writer |
269
|
|
|
* @param mixed $object |
270
|
|
|
*/ |
271
|
1 |
|
public function write(JsonWritable $writer, $object) |
272
|
|
|
{ |
273
|
1 |
|
$this->typeAdapter->write($writer, $this->get($object)); |
274
|
1 |
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Given an object, get the value at this property |
278
|
|
|
* |
279
|
|
|
* @param object $object |
280
|
|
|
* @return mixed |
281
|
|
|
*/ |
282
|
8 |
|
public function get($object) |
283
|
|
|
{ |
284
|
8 |
|
return $this->getterStrategy->get($object); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Given an object an value, set the value to the object at this property |
289
|
|
|
* |
290
|
|
|
* @param object $object |
291
|
|
|
* @param mixed $value |
292
|
|
|
*/ |
293
|
7 |
|
public function set($object, $value): void |
294
|
|
|
{ |
295
|
7 |
|
if (null === $value) { |
296
|
1 |
|
return; |
297
|
|
|
} |
298
|
|
|
|
299
|
6 |
|
$this->setterStrategy->set($object, $value); |
300
|
6 |
|
} |
301
|
|
|
} |
302
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.