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