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
|
|
|
* @return Type|null |
70
|
|
|
*/ |
71
|
|
|
public function getType() |
72
|
|
|
{ |
73
|
|
|
return $this->type; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns a new instance with the given type. |
78
|
|
|
*/ |
79
|
|
|
public function withType(Type $type): self |
80
|
|
|
{ |
81
|
|
|
$metadata = clone $this; |
82
|
|
|
$metadata->type = $type; |
83
|
|
|
|
84
|
|
|
return $metadata; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Gets description. |
89
|
|
|
* |
90
|
|
|
* @return string|null |
91
|
|
|
*/ |
92
|
|
|
public function getDescription() |
93
|
|
|
{ |
94
|
|
|
return $this->description; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns a new instance with the given description. |
99
|
|
|
* |
100
|
|
|
* @param string $description |
101
|
|
|
*/ |
102
|
|
|
public function withDescription($description): self |
103
|
|
|
{ |
104
|
|
|
$metadata = clone $this; |
105
|
|
|
$metadata->description = $description; |
106
|
|
|
|
107
|
|
|
return $metadata; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Is readable? |
112
|
|
|
* |
113
|
|
|
* @return bool|null |
114
|
|
|
*/ |
115
|
|
|
public function isReadable() |
116
|
|
|
{ |
117
|
|
|
return $this->readable; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns a new instance of Metadata with the given readable flag. |
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
|
|
|
public function withWritable(bool $writable): self |
145
|
|
|
{ |
146
|
|
|
$metadata = clone $this; |
147
|
|
|
$metadata->writable = $writable; |
148
|
|
|
|
149
|
|
|
return $metadata; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Is required? |
154
|
|
|
* |
155
|
|
|
* @return bool|null |
156
|
|
|
*/ |
157
|
|
|
public function isRequired() |
158
|
|
|
{ |
159
|
|
|
if (true === $this->required && false === $this->writable) { |
160
|
|
|
return false; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $this->required; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns a new instance with the given required flag. |
168
|
|
|
*/ |
169
|
|
|
public function withRequired(bool $required): self |
170
|
|
|
{ |
171
|
|
|
$metadata = clone $this; |
172
|
|
|
$metadata->required = $required; |
173
|
|
|
|
174
|
|
|
return $metadata; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Should an IRI or an object be provided in write context? |
179
|
|
|
* |
180
|
|
|
* @return bool|null |
181
|
|
|
*/ |
182
|
|
|
public function isWritableLink() |
183
|
|
|
{ |
184
|
|
|
return $this->writableLink; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns a new instance with the given writable link flag. |
189
|
|
|
*/ |
190
|
|
|
public function withWritableLink(bool $writableLink): self |
191
|
|
|
{ |
192
|
|
|
$metadata = clone $this; |
193
|
|
|
$metadata->writableLink = $writableLink; |
194
|
|
|
|
195
|
|
|
return $metadata; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Is an IRI or an object generated in read context? |
200
|
|
|
* |
201
|
|
|
* @return bool|null |
202
|
|
|
*/ |
203
|
|
|
public function isReadableLink() |
204
|
|
|
{ |
205
|
|
|
return $this->readableLink; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Returns a new instance with the given readable link flag. |
210
|
|
|
*/ |
211
|
|
|
public function withReadableLink(bool $readableLink): self |
212
|
|
|
{ |
213
|
|
|
$metadata = clone $this; |
214
|
|
|
$metadata->readableLink = $readableLink; |
215
|
|
|
|
216
|
|
|
return $metadata; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Gets IRI of this property. |
221
|
|
|
* |
222
|
|
|
* @return string|null |
223
|
|
|
*/ |
224
|
|
|
public function getIri() |
225
|
|
|
{ |
226
|
|
|
return $this->iri; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Returns a new instance with the given IRI. |
231
|
|
|
*/ |
232
|
|
|
public function withIri(string $iri = null): self |
233
|
|
|
{ |
234
|
|
|
$metadata = clone $this; |
235
|
|
|
$metadata->iri = $iri; |
236
|
|
|
|
237
|
|
|
return $metadata; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Is this attribute an identifier? |
242
|
|
|
* |
243
|
|
|
* @return bool|null |
244
|
|
|
*/ |
245
|
|
|
public function isIdentifier() |
246
|
|
|
{ |
247
|
|
|
return $this->identifier; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Returns a new instance with the given identifier flag. |
252
|
|
|
*/ |
253
|
|
|
public function withIdentifier(bool $identifier): self |
254
|
|
|
{ |
255
|
|
|
$metadata = clone $this; |
256
|
|
|
$metadata->identifier = $identifier; |
257
|
|
|
|
258
|
|
|
return $metadata; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Gets attributes. |
263
|
|
|
* |
264
|
|
|
* @return array|null |
265
|
|
|
*/ |
266
|
|
|
public function getAttributes() |
267
|
|
|
{ |
268
|
|
|
return $this->attributes; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Gets an attribute. |
273
|
|
|
*/ |
274
|
|
|
public function getAttribute(string $key, $defaultValue = null) |
275
|
|
|
{ |
276
|
|
|
if (isset($this->attributes[$key])) { |
277
|
|
|
return $this->attributes[$key]; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $defaultValue; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Returns a new instance with the given attribute. |
285
|
|
|
*/ |
286
|
|
|
public function withAttributes(array $attributes): self |
287
|
|
|
{ |
288
|
|
|
$metadata = clone $this; |
289
|
|
|
$metadata->attributes = $attributes; |
290
|
|
|
|
291
|
|
|
return $metadata; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Is the property inherited from a child class? |
296
|
|
|
* |
297
|
|
|
* @return string|null |
298
|
|
|
*/ |
299
|
|
|
public function isChildInherited() |
300
|
|
|
{ |
301
|
|
|
return $this->childInherited; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Returns a new instance with the given child inherited class. |
306
|
|
|
*/ |
307
|
|
|
public function withChildInherited(string $childInherited): self |
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
|
|
|
* @return SubresourceMetadata|null |
327
|
|
|
*/ |
328
|
|
|
public function getSubresource() |
329
|
|
|
{ |
330
|
|
|
return $this->subresource; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Returns a new instance with the given subresource. |
335
|
|
|
* |
336
|
|
|
* @param SubresourceMetadata $subresource |
337
|
|
|
*/ |
338
|
|
|
public function withSubresource(SubresourceMetadata $subresource = null): self |
339
|
|
|
{ |
340
|
|
|
$metadata = clone $this; |
341
|
|
|
$metadata->subresource = $subresource; |
342
|
|
|
|
343
|
|
|
return $metadata; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Is initializable? |
348
|
|
|
* |
349
|
|
|
* @return bool|null |
350
|
|
|
*/ |
351
|
|
|
public function isInitializable() |
352
|
|
|
{ |
353
|
|
|
return $this->initializable; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Returns a new instance with the given initializable flag. |
358
|
|
|
*/ |
359
|
|
|
public function withInitializable(bool $initializable): self |
360
|
|
|
{ |
361
|
|
|
$metadata = clone $this; |
362
|
|
|
$metadata->initializable = $initializable; |
363
|
|
|
|
364
|
|
|
return $metadata; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Returns the default value of the property or NULL if the property doesn't have a default value. |
369
|
|
|
*/ |
370
|
|
|
public function getDefault() |
371
|
|
|
{ |
372
|
|
|
return $this->default; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Returns a new instance with the given default value for the property. |
377
|
|
|
*/ |
378
|
|
|
public function withDefault($default): self |
379
|
|
|
{ |
380
|
|
|
$metadata = clone $this; |
381
|
|
|
$metadata->default = $default; |
382
|
|
|
|
383
|
|
|
return $metadata; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Returns an example of the value of the property. |
388
|
|
|
*/ |
389
|
|
|
public function getExample() |
390
|
|
|
{ |
391
|
|
|
return $this->example; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Returns a new instance with the given example. |
396
|
|
|
*/ |
397
|
|
|
public function withExample($example): self |
398
|
|
|
{ |
399
|
|
|
$metadata = clone $this; |
400
|
|
|
$metadata->example = $example; |
401
|
|
|
|
402
|
|
|
return $metadata; |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
|