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