1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Metadata\Property; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\PropertyInfo\Type; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Property metadata. |
20
|
|
|
* |
21
|
|
|
* @author Kévin Dunglas <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class PropertyMetadata |
24
|
|
|
{ |
25
|
|
|
private $type; |
26
|
|
|
private $description; |
27
|
|
|
private $readable; |
28
|
|
|
private $writable; |
29
|
|
|
private $readableLink; |
30
|
|
|
private $writableLink; |
31
|
|
|
private $required; |
32
|
|
|
private $iri; |
33
|
|
|
private $identifier; |
34
|
|
|
private $childInherited; |
35
|
|
|
private $attributes; |
36
|
|
|
private $subresource; |
37
|
|
|
private $alwaysIdentifier; |
38
|
|
|
|
39
|
|
|
public function __construct(Type $type = null, string $description = null, bool $readable = null, bool $writable = null, bool $readableLink = null, bool $writableLink = null, bool $required = null, bool $identifier = null, string $iri = null, $childInherited = null, array $attributes = null, SubresourceMetadata $subresource = null, bool $alwaysIdentifier = null) |
40
|
|
|
{ |
41
|
|
|
$this->type = $type; |
42
|
|
|
$this->description = $description; |
43
|
|
|
$this->readable = $readable; |
44
|
|
|
$this->writable = $writable; |
45
|
|
|
$this->readableLink = $readableLink; |
46
|
|
|
$this->writableLink = $writableLink; |
47
|
|
|
$this->required = $required; |
48
|
|
|
$this->identifier = $identifier; |
49
|
|
|
$this->iri = $iri; |
50
|
|
|
$this->childInherited = $childInherited; |
51
|
|
|
$this->attributes = $attributes; |
52
|
|
|
$this->subresource = $subresource; |
53
|
|
|
$this->alwaysIdentifier = $alwaysIdentifier; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Gets type. |
58
|
|
|
* |
59
|
|
|
* @return Type|null |
60
|
|
|
*/ |
61
|
|
|
public function getType() |
62
|
|
|
{ |
63
|
|
|
return $this->type; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns a new instance with the given type. |
68
|
|
|
* |
69
|
|
|
* @param Type $type |
70
|
|
|
* |
71
|
|
|
* @return self |
72
|
|
|
*/ |
73
|
|
|
public function withType(Type $type): self |
74
|
|
|
{ |
75
|
|
|
$metadata = clone $this; |
76
|
|
|
$metadata->type = $type; |
77
|
|
|
|
78
|
|
|
return $metadata; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Gets description. |
83
|
|
|
* |
84
|
|
|
* @return string|null |
85
|
|
|
*/ |
86
|
|
|
public function getDescription() |
87
|
|
|
{ |
88
|
|
|
return $this->description; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns a new instance with the given description. |
93
|
|
|
* |
94
|
|
|
* @param string $description |
95
|
|
|
* |
96
|
|
|
* @return self |
97
|
|
|
*/ |
98
|
|
|
public function withDescription($description): self |
99
|
|
|
{ |
100
|
|
|
$metadata = clone $this; |
101
|
|
|
$metadata->description = $description; |
102
|
|
|
|
103
|
|
|
return $metadata; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Is readable? |
108
|
|
|
* |
109
|
|
|
* @return bool|null |
110
|
|
|
*/ |
111
|
|
|
public function isReadable() |
112
|
|
|
{ |
113
|
|
|
return $this->readable; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns a new instance of Metadata with the given readable flag. |
118
|
|
|
* |
119
|
|
|
* @param bool $readable |
120
|
|
|
* |
121
|
|
|
* @return self |
122
|
|
|
*/ |
123
|
|
|
public function withReadable(bool $readable): self |
124
|
|
|
{ |
125
|
|
|
$metadata = clone $this; |
126
|
|
|
$metadata->readable = $readable; |
127
|
|
|
|
128
|
|
|
return $metadata; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Is writable? |
133
|
|
|
* |
134
|
|
|
* @return bool|null |
135
|
|
|
*/ |
136
|
|
|
public function isWritable() |
137
|
|
|
{ |
138
|
|
|
return $this->writable; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns a new instance with the given writable flag. |
143
|
|
|
* |
144
|
|
|
* @param bool $writable |
145
|
|
|
* |
146
|
|
|
* @return self |
147
|
|
|
*/ |
148
|
|
|
public function withWritable(bool $writable): self |
149
|
|
|
{ |
150
|
|
|
$metadata = clone $this; |
151
|
|
|
$metadata->writable = $writable; |
152
|
|
|
|
153
|
|
|
return $metadata; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Is required? |
158
|
|
|
* |
159
|
|
|
* @return bool|null |
160
|
|
|
*/ |
161
|
|
|
public function isRequired() |
162
|
|
|
{ |
163
|
|
|
if (true === $this->required && false === $this->writable) { |
164
|
|
|
return false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $this->required; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Returns a new instance with the given required flag. |
172
|
|
|
* |
173
|
|
|
* @param bool $required |
174
|
|
|
* |
175
|
|
|
* @return self |
176
|
|
|
*/ |
177
|
|
|
public function withRequired(bool $required): self |
178
|
|
|
{ |
179
|
|
|
$metadata = clone $this; |
180
|
|
|
$metadata->required = $required; |
181
|
|
|
|
182
|
|
|
return $metadata; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Should an IRI or an object be provided in write context? |
187
|
|
|
* |
188
|
|
|
* @return bool|null |
189
|
|
|
*/ |
190
|
|
|
public function isWritableLink() |
191
|
|
|
{ |
192
|
|
|
return $this->writableLink; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Returns a new instance with the given writable link flag. |
197
|
|
|
* |
198
|
|
|
* @param bool $writableLink |
199
|
|
|
* |
200
|
|
|
* @return self |
201
|
|
|
*/ |
202
|
|
|
public function withWritableLink(bool $writableLink): self |
203
|
|
|
{ |
204
|
|
|
$metadata = clone $this; |
205
|
|
|
$metadata->writableLink = $writableLink; |
206
|
|
|
|
207
|
|
|
return $metadata; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Is an IRI or an object generated in read context? |
212
|
|
|
* |
213
|
|
|
* @return bool|null |
214
|
|
|
*/ |
215
|
|
|
public function isReadableLink() |
216
|
|
|
{ |
217
|
|
|
return $this->readableLink; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Returns a new instance with the given readable link flag. |
222
|
|
|
* |
223
|
|
|
* @param bool $readableLink |
224
|
|
|
* |
225
|
|
|
* @return self |
226
|
|
|
*/ |
227
|
|
|
public function withReadableLink(bool $readableLink): self |
228
|
|
|
{ |
229
|
|
|
$metadata = clone $this; |
230
|
|
|
$metadata->readableLink = $readableLink; |
231
|
|
|
|
232
|
|
|
return $metadata; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Gets IRI of this property. |
237
|
|
|
* |
238
|
|
|
* @return string|null |
239
|
|
|
*/ |
240
|
|
|
public function getIri() |
241
|
|
|
{ |
242
|
|
|
return $this->iri; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Returns a new instance with the given IRI. |
247
|
|
|
* |
248
|
|
|
* @param string|null $iri |
249
|
|
|
* |
250
|
|
|
* @return self |
251
|
|
|
*/ |
252
|
|
|
public function withIri(string $iri = null): self |
253
|
|
|
{ |
254
|
|
|
$metadata = clone $this; |
255
|
|
|
$metadata->iri = $iri; |
256
|
|
|
|
257
|
|
|
return $metadata; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Is this attribute an identifier? |
262
|
|
|
* |
263
|
|
|
* @return bool|null |
264
|
|
|
*/ |
265
|
|
|
public function isIdentifier() |
266
|
|
|
{ |
267
|
|
|
return $this->identifier; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Returns a new instance with the given identifier flag. |
272
|
|
|
* |
273
|
|
|
* @param bool $identifier |
274
|
|
|
* |
275
|
|
|
* @return self |
276
|
|
|
*/ |
277
|
|
|
public function withIdentifier(bool $identifier): self |
278
|
|
|
{ |
279
|
|
|
$metadata = clone $this; |
280
|
|
|
$metadata->identifier = $identifier; |
281
|
|
|
|
282
|
|
|
return $metadata; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Gets attributes. |
287
|
|
|
* |
288
|
|
|
* @return array|null |
289
|
|
|
*/ |
290
|
|
|
public function getAttributes() |
291
|
|
|
{ |
292
|
|
|
return $this->attributes; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Gets an attribute. |
297
|
|
|
* |
298
|
|
|
* @param string $key |
299
|
|
|
* @param mixed $defaultValue |
300
|
|
|
* |
301
|
|
|
* @return mixed |
302
|
|
|
*/ |
303
|
|
|
public function getAttribute(string $key, $defaultValue = null) |
304
|
|
|
{ |
305
|
|
|
if (isset($this->attributes[$key])) { |
306
|
|
|
return $this->attributes[$key]; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return $defaultValue; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Returns a new instance with the given attribute. |
314
|
|
|
* |
315
|
|
|
* @param array $attributes |
316
|
|
|
* |
317
|
|
|
* @return self |
318
|
|
|
*/ |
319
|
|
|
public function withAttributes(array $attributes): self |
320
|
|
|
{ |
321
|
|
|
$metadata = clone $this; |
322
|
|
|
$metadata->attributes = $attributes; |
323
|
|
|
|
324
|
|
|
return $metadata; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Is the property inherited from a child class? |
329
|
|
|
* |
330
|
|
|
* @return string|null |
331
|
|
|
*/ |
332
|
|
|
public function isChildInherited() |
333
|
|
|
{ |
334
|
|
|
return $this->childInherited; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Returns a new instance with the given child inherited class. |
339
|
|
|
* |
340
|
|
|
* @param string $childInherited |
341
|
|
|
* |
342
|
|
|
* @return self |
343
|
|
|
*/ |
344
|
|
|
public function withChildInherited(string $childInherited): self |
345
|
|
|
{ |
346
|
|
|
$metadata = clone $this; |
347
|
|
|
$metadata->childInherited = $childInherited; |
348
|
|
|
|
349
|
|
|
return $metadata; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Represents whether the property has a subresource. |
354
|
|
|
* |
355
|
|
|
* @return bool |
356
|
|
|
*/ |
357
|
|
|
public function hasSubresource(): bool |
358
|
|
|
{ |
359
|
|
|
return null !== $this->subresource; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Gets the subresource metadata. |
364
|
|
|
* |
365
|
|
|
* @return SubresourceMetadata|null |
366
|
|
|
*/ |
367
|
|
|
public function getSubresource() |
368
|
|
|
{ |
369
|
|
|
return $this->subresource; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Returns a new instance with the given subresource. |
374
|
|
|
* |
375
|
|
|
* @param SubresourceMetadata $subresource |
376
|
|
|
* |
377
|
|
|
* @return self |
378
|
|
|
*/ |
379
|
|
|
public function withSubresource(SubresourceMetadata $subresource = null): self |
380
|
|
|
{ |
381
|
|
|
$metadata = clone $this; |
382
|
|
|
$metadata->subresource = $subresource; |
383
|
|
|
|
384
|
|
|
return $metadata; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Returns a new instance with the given alwaysIdentifier flag. |
389
|
|
|
*/ |
390
|
|
|
public function withAlwaysIdentifier(bool $alwaysIdentifier): self |
391
|
|
|
{ |
392
|
|
|
$metadata = clone $this; |
393
|
|
|
$metadata->alwaysIdentifier = $alwaysIdentifier; |
394
|
|
|
|
395
|
|
|
return $metadata; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Is the property should be serialized as identifier ? |
400
|
|
|
* |
401
|
|
|
* @return bool|null |
402
|
|
|
*/ |
403
|
|
|
public function isAlwaysIdentifier() |
404
|
|
|
{ |
405
|
|
|
return $this->alwaysIdentifier; |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|