1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Serializer\Metadata; |
4
|
|
|
|
5
|
|
|
use Bdf\Serializer\PropertyAccessor\PropertyAccessorInterface; |
6
|
|
|
use Bdf\Serializer\Type\Type; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* PropertyMetadata |
10
|
|
|
* |
11
|
|
|
* @author Seb |
12
|
|
|
*/ |
13
|
|
|
class PropertyMetadata |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The owner class name |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
public $class; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The property name |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
public $name; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The property alias |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
public $alias; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The property type infos |
38
|
|
|
* |
39
|
|
|
* @var Type |
40
|
|
|
*/ |
41
|
|
|
public $type; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The property groups |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
public $groups = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The property accessor |
52
|
|
|
* |
53
|
|
|
* @var PropertyAccessorInterface |
54
|
|
|
*/ |
55
|
|
|
public $accessor; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The version when the property has been added. |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
public $since; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* The version when the property has been removed. |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
public $until; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The read only state of the property. |
73
|
|
|
* |
74
|
|
|
* @var bool |
75
|
|
|
*/ |
76
|
|
|
public $readOnly = false; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* The inline state of the property. |
80
|
|
|
* |
81
|
|
|
* @var bool |
82
|
|
|
*/ |
83
|
|
|
public $inline = false; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* The default value of the property. |
87
|
|
|
* |
88
|
|
|
* @var mixed |
89
|
|
|
*/ |
90
|
|
|
public $defaultValue; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* The context options for normalization. |
94
|
|
|
* |
95
|
|
|
* @var null|array |
96
|
|
|
*/ |
97
|
|
|
public $normalizationOptions; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* The context options for denormalization. |
101
|
|
|
* |
102
|
|
|
* @var null|array |
103
|
|
|
*/ |
104
|
|
|
public $denormalizationOptions; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* PropertyMetadata constructor. |
108
|
|
|
* |
109
|
|
|
* @param string $class |
110
|
|
|
* @param string $name |
111
|
|
|
*/ |
112
|
|
|
public function __construct(string $class, string $name) |
113
|
|
|
{ |
114
|
|
|
$this->class = $class; |
115
|
|
|
$this->name = $name; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Check whether the property should be skipped |
120
|
|
|
* |
121
|
|
|
* @param array $groups |
122
|
|
|
* |
123
|
|
|
* @return boolean True if the property has one of the groups |
124
|
|
|
*/ |
125
|
|
|
public function hasGroups(array $groups) |
126
|
|
|
{ |
127
|
|
|
foreach ($groups as $group) { |
128
|
|
|
if (isset($this->groups[$group])) { |
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the property class name |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function className() |
142
|
|
|
{ |
143
|
|
|
return $this->class; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the property name |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public function name() |
152
|
|
|
{ |
153
|
|
|
return $this->name; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Set the property type infos |
158
|
|
|
* |
159
|
|
|
* @param Type $type |
160
|
|
|
*/ |
161
|
|
|
public function setType(Type $type) |
162
|
|
|
{ |
163
|
|
|
$this->type = $type; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get the property type |
168
|
|
|
* |
169
|
|
|
* @return Type |
170
|
|
|
*/ |
171
|
|
|
public function type() |
172
|
|
|
{ |
173
|
|
|
return $this->type; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Set the property alias |
178
|
|
|
* |
179
|
|
|
* @param string $alias |
180
|
|
|
*/ |
181
|
|
|
public function setAlias($alias) |
182
|
|
|
{ |
183
|
|
|
$this->alias = $alias; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get the property alias |
188
|
|
|
* |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
|
|
public function alias() |
192
|
|
|
{ |
193
|
|
|
return $this->alias; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Set the property groups |
198
|
|
|
* |
199
|
|
|
* @param array $groups |
200
|
|
|
*/ |
201
|
|
|
public function setGroups(array $groups) |
202
|
|
|
{ |
203
|
|
|
foreach ($groups as $group) { |
204
|
|
|
$this->groups[$group] = $group; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get the property groups |
210
|
|
|
* |
211
|
|
|
* @return array |
212
|
|
|
*/ |
213
|
|
|
public function groups() |
214
|
|
|
{ |
215
|
|
|
return $this->groups; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Set the property accessor |
220
|
|
|
* |
221
|
|
|
* @param PropertyAccessorInterface $accessor |
222
|
|
|
*/ |
223
|
|
|
public function setAccessor(PropertyAccessorInterface $accessor) |
224
|
|
|
{ |
225
|
|
|
$this->accessor = $accessor; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Get the property accessor |
230
|
|
|
* |
231
|
|
|
* @return PropertyAccessorInterface |
232
|
|
|
*/ |
233
|
|
|
public function accessor() |
234
|
|
|
{ |
235
|
|
|
return $this->accessor; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Set the version when the property has been added |
240
|
|
|
* |
241
|
|
|
* @param string $version |
242
|
|
|
*/ |
243
|
|
|
public function setSince($version) |
244
|
|
|
{ |
245
|
|
|
$this->since = $version; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Get the version when the property has been added |
250
|
|
|
* |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
|
|
public function since() |
254
|
|
|
{ |
255
|
|
|
return $this->since; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Set the version when the property has been removed |
260
|
|
|
* |
261
|
|
|
* @param string $version |
262
|
|
|
*/ |
263
|
|
|
public function setUntil($version) |
264
|
|
|
{ |
265
|
|
|
$this->until = $version; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Get the version when the property has been removed |
270
|
|
|
* |
271
|
|
|
* @return string |
272
|
|
|
*/ |
273
|
|
|
public function until() |
274
|
|
|
{ |
275
|
|
|
return $this->until; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Test if the property match to the version |
280
|
|
|
* |
281
|
|
|
* @param string $version |
282
|
|
|
* |
283
|
|
|
* @return boolean |
284
|
|
|
*/ |
285
|
|
|
public function matchVersion($version) |
286
|
|
|
{ |
287
|
|
|
if (null !== $this->since && version_compare($version, $this->since, '<')) { |
288
|
|
|
return false; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
if (null !== $this->until && version_compare($version, $this->until, '>')) { |
292
|
|
|
return false; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return true; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Set the value of the read only flag. |
300
|
|
|
* |
301
|
|
|
* @param bool $flag |
302
|
|
|
*/ |
303
|
|
|
public function setReadOnly($flag) |
304
|
|
|
{ |
305
|
|
|
$this->readOnly = $flag; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Get the value of the read only flag. |
310
|
|
|
* |
311
|
|
|
* @return bool |
312
|
|
|
*/ |
313
|
|
|
public function readOnly() |
314
|
|
|
{ |
315
|
|
|
return $this->readOnly; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Set the property inline. |
320
|
|
|
* |
321
|
|
|
* The result of the normalization of this property |
322
|
|
|
* will be add on the same layer of the property. |
323
|
|
|
* Works only if the normalization of this property is an array. |
324
|
|
|
* |
325
|
|
|
* @param bool $flag |
326
|
|
|
*/ |
327
|
|
|
public function setInline(bool $flag) |
328
|
|
|
{ |
329
|
|
|
$this->inline = $flag; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Get the inline state of the property. |
334
|
|
|
* |
335
|
|
|
* @return bool |
336
|
|
|
*/ |
337
|
|
|
public function inline(): bool |
338
|
|
|
{ |
339
|
|
|
return $this->inline; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Set the value to skip. |
344
|
|
|
* |
345
|
|
|
* @param mixed $defaultValue |
346
|
|
|
*/ |
347
|
|
|
public function setDefaultValue($defaultValue) |
348
|
|
|
{ |
349
|
|
|
$this->defaultValue = $defaultValue; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Get the value to skip. |
354
|
|
|
* |
355
|
|
|
* @return mixed |
356
|
|
|
*/ |
357
|
|
|
public function defaultValue() |
358
|
|
|
{ |
359
|
|
|
return $this->defaultValue; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Set the options for normalization context |
364
|
|
|
* |
365
|
|
|
* @param null|array $options |
366
|
|
|
*/ |
367
|
|
|
public function setNormalizationOptions($options) |
368
|
|
|
{ |
369
|
|
|
$this->normalizationOptions = $options; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Get property options for normalization context. |
374
|
|
|
* |
375
|
|
|
* @return null|array |
376
|
|
|
*/ |
377
|
|
|
public function normalizationOptions() |
378
|
|
|
{ |
379
|
|
|
return $this->normalizationOptions; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Set the options for denormalization context |
384
|
|
|
* |
385
|
|
|
* @param null|array $options |
386
|
|
|
*/ |
387
|
|
|
public function setDenormalizationOptions($options) |
388
|
|
|
{ |
389
|
|
|
$this->denormalizationOptions = $options; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Get property options for denormalization context. |
394
|
|
|
* |
395
|
|
|
* @return null|array |
396
|
|
|
*/ |
397
|
|
|
public function denormalizationOptions() |
398
|
|
|
{ |
399
|
|
|
return $this->denormalizationOptions; |
400
|
|
|
} |
401
|
|
|
} |
402
|
|
|
|