|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace JMS\Serializer\Metadata; |
|
6
|
|
|
|
|
7
|
|
|
use JMS\Serializer\Exception\InvalidMetadataException; |
|
8
|
|
|
use JMS\Serializer\Expression\Expression; |
|
9
|
|
|
use Metadata\PropertyMetadata as BasePropertyMetadata; |
|
10
|
|
|
|
|
11
|
|
|
class PropertyMetadata extends BasePropertyMetadata |
|
12
|
|
|
{ |
|
13
|
|
|
public const ACCESS_TYPE_PROPERTY = 'property'; |
|
14
|
|
|
public const ACCESS_TYPE_PUBLIC_METHOD = 'public_method'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string|null |
|
18
|
|
|
*/ |
|
19
|
|
|
public $sinceVersion; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string|null |
|
23
|
|
|
*/ |
|
24
|
|
|
public $untilVersion; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string[]|null |
|
28
|
|
|
*/ |
|
29
|
|
|
public $groups; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string|null |
|
33
|
|
|
*/ |
|
34
|
|
|
public $serializedName; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array|null |
|
38
|
|
|
*/ |
|
39
|
|
|
public $type; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var bool |
|
43
|
|
|
*/ |
|
44
|
|
|
public $xmlCollection = false; |
|
45
|
|
|
|
|
46
|
280 |
|
/** |
|
47
|
|
|
* @var bool |
|
48
|
280 |
|
*/ |
|
49
|
|
|
public $xmlCollectionInline = false; |
|
50
|
|
|
|
|
51
|
280 |
|
/** |
|
52
|
280 |
|
* @var bool |
|
53
|
|
|
*/ |
|
54
|
|
|
public $xmlCollectionSkipWhenEmpty = true; |
|
55
|
280 |
|
|
|
56
|
|
|
/** |
|
57
|
280 |
|
* @var string|null |
|
58
|
|
|
*/ |
|
59
|
280 |
|
public $xmlEntryName; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
260 |
|
* @var string|null |
|
63
|
|
|
*/ |
|
64
|
260 |
|
public $xmlEntryNamespace; |
|
65
|
14 |
|
|
|
66
|
|
|
/** |
|
67
|
14 |
|
* @var string|null |
|
68
|
10 |
|
*/ |
|
69
|
6 |
|
public $xmlKeyAttribute; |
|
70
|
4 |
|
|
|
71
|
1 |
|
/** |
|
72
|
3 |
|
* @var bool |
|
73
|
1 |
|
*/ |
|
74
|
|
|
public $xmlAttribute = false; |
|
75
|
2 |
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var bool |
|
78
|
|
|
*/ |
|
79
|
12 |
|
public $xmlValue = false; |
|
80
|
6 |
|
|
|
81
|
5 |
|
/** |
|
82
|
|
|
* @var string|null |
|
83
|
1 |
|
*/ |
|
84
|
|
|
public $xmlNamespace; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @var bool |
|
88
|
257 |
|
*/ |
|
89
|
257 |
|
public $xmlKeyValuePairs = false; |
|
90
|
257 |
|
|
|
91
|
|
|
/** |
|
92
|
217 |
|
* @var bool |
|
93
|
|
|
*/ |
|
94
|
217 |
|
public $xmlElementCData = true; |
|
95
|
217 |
|
|
|
96
|
|
|
/** |
|
97
|
17 |
|
* @var string|null |
|
98
|
|
|
*/ |
|
99
|
17 |
|
public $getter; |
|
100
|
17 |
|
|
|
101
|
17 |
|
/** |
|
102
|
17 |
|
* @var string|null |
|
103
|
|
|
*/ |
|
104
|
|
|
public $setter; |
|
105
|
17 |
|
|
|
106
|
|
|
/** |
|
107
|
17 |
|
* @var bool |
|
108
|
17 |
|
*/ |
|
109
|
17 |
|
public $inline = false; |
|
110
|
17 |
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @var bool |
|
113
|
1 |
|
*/ |
|
114
|
|
|
public $skipWhenEmpty = false; |
|
115
|
1 |
|
|
|
116
|
1 |
|
/** |
|
117
|
1 |
|
* @var bool |
|
118
|
1 |
|
*/ |
|
119
|
1 |
|
public $readOnly = false; |
|
120
|
1 |
|
|
|
121
|
1 |
|
/** |
|
122
|
1 |
|
* @var bool |
|
123
|
1 |
|
*/ |
|
124
|
1 |
|
public $xmlAttributeMap = false; |
|
125
|
1 |
|
|
|
126
|
1 |
|
/** |
|
127
|
1 |
|
* @var int|null |
|
128
|
1 |
|
*/ |
|
129
|
1 |
|
public $maxDepth = null; |
|
130
|
1 |
|
|
|
131
|
1 |
|
/** |
|
132
|
1 |
|
* @var string|Expression|null |
|
133
|
1 |
|
*/ |
|
134
|
1 |
|
public $excludeIf = null; |
|
135
|
1 |
|
|
|
136
|
1 |
|
/** |
|
137
|
1 |
|
* @var bool|null |
|
138
|
1 |
|
*/ |
|
139
|
1 |
|
public $hasDefault; |
|
140
|
1 |
|
|
|
141
|
1 |
|
/** |
|
142
|
|
|
* @var mixed|null |
|
143
|
|
|
*/ |
|
144
|
|
|
public $defaultValue; |
|
145
|
1 |
|
|
|
146
|
|
|
/** |
|
147
|
1 |
|
* @internal |
|
148
|
|
|
* |
|
149
|
1 |
|
* @var bool |
|
150
|
1 |
|
*/ |
|
151
|
1 |
|
public $forceReflectionAccess = false; |
|
152
|
1 |
|
|
|
153
|
1 |
|
public function __construct(string $class, string $name) |
|
154
|
1 |
|
{ |
|
155
|
1 |
|
parent::__construct($class, $name); |
|
156
|
1 |
|
|
|
157
|
1 |
|
try { |
|
158
|
1 |
|
$class = $this->getReflection()->getDeclaringClass(); |
|
159
|
1 |
|
$this->forceReflectionAccess = $class->isInternal() || $class->getProperty($name)->isStatic(); |
|
160
|
1 |
|
} catch (\ReflectionException $e) { |
|
|
|
|
|
|
161
|
1 |
|
} |
|
162
|
1 |
|
} |
|
163
|
1 |
|
|
|
164
|
1 |
|
private function getReflection(): \ReflectionProperty |
|
165
|
1 |
|
{ |
|
166
|
1 |
|
return new \ReflectionProperty($this->class, $this->name); |
|
167
|
1 |
|
} |
|
168
|
1 |
|
|
|
169
|
|
|
public function setAccessor(string $type, ?string $getter = null, ?string $setter = null): void |
|
170
|
1 |
|
{ |
|
171
|
|
|
if (self::ACCESS_TYPE_PUBLIC_METHOD === $type) { |
|
172
|
1 |
|
$class = $this->getReflection()->getDeclaringClass(); |
|
173
|
|
|
|
|
174
|
|
|
if (empty($getter)) { |
|
175
|
1 |
|
if ($class->hasMethod('get' . $this->name) && $class->getMethod('get' . $this->name)->isPublic()) { |
|
176
|
1 |
|
$getter = 'get' . $this->name; |
|
177
|
|
|
} elseif ($class->hasMethod('is' . $this->name) && $class->getMethod('is' . $this->name)->isPublic()) { |
|
178
|
1 |
|
$getter = 'is' . $this->name; |
|
179
|
|
|
} elseif ($class->hasMethod('has' . $this->name) && $class->getMethod('has' . $this->name)->isPublic()) { |
|
180
|
|
|
$getter = 'has' . $this->name; |
|
181
|
1 |
|
} else { |
|
182
|
1 |
|
throw new InvalidMetadataException(sprintf('There is neither a public %s method, nor a public %s method, nor a public %s method in class %s. Please specify which public method should be used for retrieving the value of the property %s.', 'get' . ucfirst($this->name), 'is' . ucfirst($this->name), 'has' . ucfirst($this->name), $this->class, $this->name)); |
|
183
|
|
|
} |
|
184
|
1 |
|
} |
|
185
|
1 |
|
|
|
186
|
|
|
if (empty($setter) && !$this->readOnly) { |
|
187
|
|
|
if ($class->hasMethod('set' . $this->name) && $class->getMethod('set' . $this->name)->isPublic()) { |
|
188
|
1 |
|
$setter = 'set' . $this->name; |
|
189
|
1 |
|
} else { |
|
190
|
|
|
throw new InvalidMetadataException(sprintf('There is no public %s method in class %s. Please specify which public method should be used for setting the value of the property %s.', 'set' . ucfirst($this->name), $this->class, $this->name)); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$this->getter = $getter; |
|
196
|
|
|
$this->setter = $setter; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function setType(array $type): void |
|
200
|
|
|
{ |
|
201
|
|
|
$this->type = $type; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public static function isCollectionList(?array $type = null): bool |
|
205
|
|
|
{ |
|
206
|
|
|
return is_array($type) |
|
207
|
|
|
&& 'array' === $type['name'] |
|
208
|
|
|
&& isset($type['params'][0]) |
|
209
|
|
|
&& !isset($type['params'][1]); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
public static function isCollectionMap(?array $type = null): bool |
|
213
|
|
|
{ |
|
214
|
|
|
return is_array($type) |
|
215
|
|
|
&& 'array' === $type['name'] |
|
216
|
|
|
&& isset($type['params'][0]) |
|
217
|
|
|
&& isset($type['params'][1]); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
protected function serializeToArray(): array |
|
221
|
|
|
{ |
|
222
|
|
|
return [ |
|
223
|
|
|
$this->sinceVersion, |
|
224
|
|
|
$this->untilVersion, |
|
225
|
|
|
$this->groups, |
|
226
|
|
|
$this->serializedName, |
|
227
|
|
|
$this->type, |
|
228
|
|
|
$this->xmlCollection, |
|
229
|
|
|
$this->xmlCollectionInline, |
|
230
|
|
|
$this->xmlEntryName, |
|
231
|
|
|
$this->xmlKeyAttribute, |
|
232
|
|
|
$this->xmlAttribute, |
|
233
|
|
|
$this->xmlValue, |
|
234
|
|
|
$this->xmlNamespace, |
|
235
|
|
|
$this->xmlKeyValuePairs, |
|
236
|
|
|
$this->xmlElementCData, |
|
237
|
|
|
$this->getter, |
|
238
|
|
|
$this->setter, |
|
239
|
|
|
$this->inline, |
|
240
|
|
|
$this->readOnly, |
|
241
|
|
|
$this->xmlAttributeMap, |
|
242
|
|
|
$this->maxDepth, |
|
243
|
|
|
$this->xmlEntryNamespace, |
|
244
|
|
|
$this->xmlCollectionSkipWhenEmpty, |
|
245
|
|
|
$this->excludeIf, |
|
246
|
|
|
$this->skipWhenEmpty, |
|
247
|
|
|
$this->forceReflectionAccess, |
|
248
|
|
|
$this->hasDefault, |
|
249
|
|
|
$this->defaultValue, |
|
250
|
|
|
parent::serializeToArray(), |
|
251
|
|
|
]; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
protected function unserializeFromArray(array $data): void |
|
255
|
|
|
{ |
|
256
|
|
|
[ |
|
257
|
|
|
$this->sinceVersion, |
|
258
|
|
|
$this->untilVersion, |
|
259
|
|
|
$this->groups, |
|
260
|
|
|
$this->serializedName, |
|
261
|
|
|
$this->type, |
|
262
|
|
|
$this->xmlCollection, |
|
263
|
|
|
$this->xmlCollectionInline, |
|
264
|
|
|
$this->xmlEntryName, |
|
265
|
|
|
$this->xmlKeyAttribute, |
|
266
|
|
|
$this->xmlAttribute, |
|
267
|
|
|
$this->xmlValue, |
|
268
|
|
|
$this->xmlNamespace, |
|
269
|
|
|
$this->xmlKeyValuePairs, |
|
270
|
|
|
$this->xmlElementCData, |
|
271
|
|
|
$this->getter, |
|
272
|
|
|
$this->setter, |
|
273
|
|
|
$this->inline, |
|
274
|
|
|
$this->readOnly, |
|
275
|
|
|
$this->xmlAttributeMap, |
|
276
|
|
|
$this->maxDepth, |
|
277
|
|
|
$this->xmlEntryNamespace, |
|
278
|
|
|
$this->xmlCollectionSkipWhenEmpty, |
|
279
|
|
|
$this->excludeIf, |
|
280
|
|
|
$this->skipWhenEmpty, |
|
281
|
|
|
$this->forceReflectionAccess, |
|
282
|
|
|
$this->hasDefault, |
|
283
|
|
|
$this->defaultValue, |
|
284
|
|
|
$parentData, |
|
285
|
|
|
] = $data; |
|
286
|
|
|
|
|
287
|
|
|
parent::unserializeFromArray($parentData); |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|