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 $initializable; |
38
|
|
|
/** |
39
|
|
|
* @var null |
40
|
|
|
*/ |
41
|
|
|
private $default; |
42
|
|
|
/** |
43
|
|
|
* @var null |
44
|
|
|
*/ |
45
|
|
|
private $example; |
46
|
|
|
|
47
|
|
|
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 $initializable = null, $default = null, $example = null) |
48
|
|
|
{ |
49
|
|
|
$this->type = $type; |
50
|
|
|
$this->description = $description; |
51
|
|
|
$this->readable = $readable; |
52
|
|
|
$this->writable = $writable; |
53
|
|
|
$this->readableLink = $readableLink; |
54
|
|
|
$this->writableLink = $writableLink; |
55
|
|
|
$this->required = $required; |
56
|
|
|
$this->identifier = $identifier; |
57
|
|
|
$this->iri = $iri; |
58
|
|
|
$this->childInherited = $childInherited; |
59
|
|
|
$this->attributes = $attributes; |
60
|
|
|
$this->subresource = $subresource; |
61
|
|
|
$this->initializable = $initializable; |
62
|
|
|
$this->default = $default; |
63
|
|
|
$this->example = $example; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Gets type. |
68
|
|
|
*/ |
69
|
|
|
public function getType(): ?Type |
70
|
|
|
{ |
71
|
|
|
return $this->type; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns a new instance with the given type. |
76
|
|
|
*/ |
77
|
|
|
public function withType(Type $type): self |
78
|
|
|
{ |
79
|
|
|
$metadata = clone $this; |
80
|
|
|
$metadata->type = $type; |
81
|
|
|
|
82
|
|
|
return $metadata; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Gets description. |
87
|
|
|
*/ |
88
|
|
|
public function getDescription(): ?string |
89
|
|
|
{ |
90
|
|
|
return $this->description; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns a new instance with the given description. |
95
|
|
|
*/ |
96
|
|
|
public function withDescription(string $description): self |
97
|
|
|
{ |
98
|
|
|
$metadata = clone $this; |
99
|
|
|
$metadata->description = $description; |
100
|
|
|
|
101
|
|
|
return $metadata; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Is readable? |
106
|
|
|
*/ |
107
|
|
|
public function isReadable(): ?bool |
108
|
|
|
{ |
109
|
|
|
return $this->readable; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns a new instance of Metadata with the given readable flag. |
114
|
|
|
*/ |
115
|
|
|
public function withReadable(bool $readable): self |
116
|
|
|
{ |
117
|
|
|
$metadata = clone $this; |
118
|
|
|
$metadata->readable = $readable; |
119
|
|
|
|
120
|
|
|
return $metadata; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Is writable? |
125
|
|
|
*/ |
126
|
|
|
public function isWritable(): ?bool |
127
|
|
|
{ |
128
|
|
|
return $this->writable; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns a new instance with the given writable flag. |
133
|
|
|
*/ |
134
|
|
|
public function withWritable(bool $writable): self |
135
|
|
|
{ |
136
|
|
|
$metadata = clone $this; |
137
|
|
|
$metadata->writable = $writable; |
138
|
|
|
|
139
|
|
|
return $metadata; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Is required? |
144
|
|
|
*/ |
145
|
|
|
public function isRequired(): ?bool |
146
|
|
|
{ |
147
|
|
|
if (true === $this->required && false === $this->writable) { |
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->required; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns a new instance with the given required flag. |
156
|
|
|
*/ |
157
|
|
|
public function withRequired(bool $required): self |
158
|
|
|
{ |
159
|
|
|
$metadata = clone $this; |
160
|
|
|
$metadata->required = $required; |
161
|
|
|
|
162
|
|
|
return $metadata; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Should an IRI or an object be provided in write context? |
167
|
|
|
*/ |
168
|
|
|
public function isWritableLink(): ?bool |
169
|
|
|
{ |
170
|
|
|
return $this->writableLink; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Returns a new instance with the given writable link flag. |
175
|
|
|
*/ |
176
|
|
|
public function withWritableLink(bool $writableLink): self |
177
|
|
|
{ |
178
|
|
|
$metadata = clone $this; |
179
|
|
|
$metadata->writableLink = $writableLink; |
180
|
|
|
|
181
|
|
|
return $metadata; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Is an IRI or an object generated in read context? |
186
|
|
|
*/ |
187
|
|
|
public function isReadableLink(): ?bool |
188
|
|
|
{ |
189
|
|
|
return $this->readableLink; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Returns a new instance with the given readable link flag. |
194
|
|
|
*/ |
195
|
|
|
public function withReadableLink(bool $readableLink): self |
196
|
|
|
{ |
197
|
|
|
$metadata = clone $this; |
198
|
|
|
$metadata->readableLink = $readableLink; |
199
|
|
|
|
200
|
|
|
return $metadata; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Gets IRI of this property. |
205
|
|
|
*/ |
206
|
|
|
public function getIri(): ?string |
207
|
|
|
{ |
208
|
|
|
return $this->iri; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Returns a new instance with the given IRI. |
213
|
|
|
*/ |
214
|
|
|
public function withIri(string $iri = null): self |
215
|
|
|
{ |
216
|
|
|
$metadata = clone $this; |
217
|
|
|
$metadata->iri = $iri; |
218
|
|
|
|
219
|
|
|
return $metadata; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Is this attribute an identifier? |
224
|
|
|
*/ |
225
|
|
|
public function isIdentifier(): ?bool |
226
|
|
|
{ |
227
|
|
|
return $this->identifier; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Returns a new instance with the given identifier flag. |
232
|
|
|
*/ |
233
|
|
|
public function withIdentifier(bool $identifier): self |
234
|
|
|
{ |
235
|
|
|
$metadata = clone $this; |
236
|
|
|
$metadata->identifier = $identifier; |
237
|
|
|
|
238
|
|
|
return $metadata; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Gets attributes. |
243
|
|
|
*/ |
244
|
|
|
public function getAttributes(): ?array |
245
|
|
|
{ |
246
|
|
|
return $this->attributes; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Gets an attribute. |
251
|
|
|
* |
252
|
|
|
* @param mixed|null $defaultValue |
253
|
|
|
*/ |
254
|
|
|
public function getAttribute(string $key, $defaultValue = null) |
255
|
|
|
{ |
256
|
|
|
return $this->attributes[$key] ?? $defaultValue; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Returns a new instance with the given attribute. |
261
|
|
|
*/ |
262
|
|
|
public function withAttributes(array $attributes): self |
263
|
|
|
{ |
264
|
|
|
$metadata = clone $this; |
265
|
|
|
$metadata->attributes = $attributes; |
266
|
|
|
|
267
|
|
|
return $metadata; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Gets child inherited. |
272
|
|
|
*/ |
273
|
|
|
public function getChildInherited(): ?string |
274
|
|
|
{ |
275
|
|
|
return $this->childInherited; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Is the property inherited from a child class? |
280
|
|
|
*/ |
281
|
|
|
public function hasChildInherited(): bool |
282
|
|
|
{ |
283
|
|
|
return null !== $this->childInherited; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Is the property inherited from a child class? |
288
|
|
|
* |
289
|
|
|
* @deprecated since version 2.4, to be removed in 3.0. |
290
|
|
|
*/ |
291
|
|
|
public function isChildInherited(): ?string |
292
|
|
|
{ |
293
|
|
|
@trigger_error(sprintf('The use of "%1$s::isChildInherited()" is deprecated since 2.4 and will be removed in 3.0. Use "%1$s::getChildInherited()" or "%1$s::hasChildInherited()" directly instead.', __CLASS__), E_USER_DEPRECATED); |
294
|
|
|
|
295
|
|
|
return $this->getChildInherited(); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Returns a new instance with the given child inherited class. |
300
|
|
|
*/ |
301
|
|
|
public function withChildInherited(string $childInherited): self |
302
|
|
|
{ |
303
|
|
|
$metadata = clone $this; |
304
|
|
|
$metadata->childInherited = $childInherited; |
305
|
|
|
|
306
|
|
|
return $metadata; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Represents whether the property has a subresource. |
311
|
|
|
*/ |
312
|
|
|
public function hasSubresource(): bool |
313
|
|
|
{ |
314
|
|
|
return null !== $this->subresource; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Gets the subresource metadata. |
319
|
|
|
*/ |
320
|
|
|
public function getSubresource(): ?SubresourceMetadata |
321
|
|
|
{ |
322
|
|
|
return $this->subresource; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Returns a new instance with the given subresource. |
327
|
|
|
* |
328
|
|
|
* @param SubresourceMetadata $subresource |
329
|
|
|
*/ |
330
|
|
|
public function withSubresource(SubresourceMetadata $subresource = null): self |
331
|
|
|
{ |
332
|
|
|
$metadata = clone $this; |
333
|
|
|
$metadata->subresource = $subresource; |
334
|
|
|
|
335
|
|
|
return $metadata; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Is initializable? |
340
|
|
|
*/ |
341
|
|
|
public function isInitializable(): ?bool |
342
|
|
|
{ |
343
|
|
|
return $this->initializable; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Returns a new instance with the given initializable flag. |
348
|
|
|
*/ |
349
|
|
|
public function withInitializable(bool $initializable): self |
350
|
|
|
{ |
351
|
|
|
$metadata = clone $this; |
352
|
|
|
$metadata->initializable = $initializable; |
353
|
|
|
|
354
|
|
|
return $metadata; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Returns the default value of the property or NULL if the property doesn't have a default value. |
359
|
|
|
*/ |
360
|
|
|
public function getDefault() |
361
|
|
|
{ |
362
|
|
|
return $this->default; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Returns a new instance with the given default value for the property. |
367
|
|
|
*/ |
368
|
|
|
public function withDefault($default): self |
369
|
|
|
{ |
370
|
|
|
$metadata = clone $this; |
371
|
|
|
$metadata->default = $default; |
372
|
|
|
|
373
|
|
|
return $metadata; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Returns an example of the value of the property. |
378
|
|
|
*/ |
379
|
|
|
public function getExample() |
380
|
|
|
{ |
381
|
|
|
return $this->example; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Returns a new instance with the given example. |
386
|
|
|
*/ |
387
|
|
|
public function withExample($example): self |
388
|
|
|
{ |
389
|
|
|
$metadata = clone $this; |
390
|
|
|
$metadata->example = $example; |
391
|
|
|
|
392
|
|
|
return $metadata; |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
|