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