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\Resource; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Api\OperationType; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Resource metadata. |
20
|
|
|
* |
21
|
|
|
* @author Kévin Dunglas <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class ResourceMetadata |
24
|
|
|
{ |
25
|
|
|
private $shortName; |
26
|
|
|
private $description; |
27
|
|
|
private $iri; |
28
|
|
|
private $itemOperations; |
29
|
|
|
private $collectionOperations; |
30
|
|
|
private $subresourceOperations; |
31
|
|
|
private $graphql; |
32
|
|
|
private $attributes; |
33
|
|
|
private $interface; |
34
|
|
|
private $implements; |
35
|
|
|
|
36
|
|
|
public function __construct(string $shortName = null, string $description = null, string $iri = null, array $itemOperations = null, array $collectionOperations = null, array $attributes = null, array $subresourceOperations = null, array $graphql = null, bool $interface = null, array $implements = null) |
37
|
|
|
{ |
38
|
|
|
$this->shortName = $shortName; |
39
|
|
|
$this->description = $description; |
40
|
|
|
$this->iri = $iri; |
41
|
|
|
$this->itemOperations = $itemOperations; |
42
|
|
|
$this->collectionOperations = $collectionOperations; |
43
|
|
|
$this->subresourceOperations = $subresourceOperations; |
44
|
|
|
$this->graphql = $graphql; |
45
|
|
|
$this->attributes = $attributes; |
46
|
|
|
$this->interface = $interface; |
47
|
|
|
$this->implements = $implements; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Gets the short name. |
52
|
|
|
*/ |
53
|
|
|
public function getShortName(): ?string |
54
|
|
|
{ |
55
|
|
|
return $this->shortName; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns a new instance with the given short name. |
60
|
|
|
*/ |
61
|
|
|
public function withShortName(string $shortName): self |
62
|
|
|
{ |
63
|
|
|
$metadata = clone $this; |
64
|
|
|
$metadata->shortName = $shortName; |
65
|
|
|
|
66
|
|
|
return $metadata; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Gets the description. |
71
|
|
|
*/ |
72
|
|
|
public function getDescription(): ?string |
73
|
|
|
{ |
74
|
|
|
return $this->description; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns a new instance with the given description. |
79
|
|
|
*/ |
80
|
|
|
public function withDescription(string $description): self |
81
|
|
|
{ |
82
|
|
|
$metadata = clone $this; |
83
|
|
|
$metadata->description = $description; |
84
|
|
|
|
85
|
|
|
return $metadata; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Gets the associated IRI. |
90
|
|
|
*/ |
91
|
|
|
public function getIri(): ?string |
92
|
|
|
{ |
93
|
|
|
return $this->iri; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function isInterface(): ?bool |
97
|
|
|
{ |
98
|
|
|
return $this->interface; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getImplements(): ?array |
102
|
|
|
{ |
103
|
|
|
return $this->implements; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns a new instance with the given IRI. |
108
|
|
|
*/ |
109
|
|
|
public function withIri(string $iri): self |
110
|
|
|
{ |
111
|
|
|
$metadata = clone $this; |
112
|
|
|
$metadata->iri = $iri; |
113
|
|
|
|
114
|
|
|
return $metadata; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Gets item operations. |
119
|
|
|
*/ |
120
|
|
|
public function getItemOperations(): ?array |
121
|
|
|
{ |
122
|
|
|
return $this->itemOperations; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns a new instance with the given item operations. |
127
|
|
|
*/ |
128
|
|
|
public function withItemOperations(array $itemOperations): self |
129
|
|
|
{ |
130
|
|
|
$metadata = clone $this; |
131
|
|
|
$metadata->itemOperations = $itemOperations; |
132
|
|
|
|
133
|
|
|
return $metadata; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Gets collection operations. |
138
|
|
|
*/ |
139
|
|
|
public function getCollectionOperations(): ?array |
140
|
|
|
{ |
141
|
|
|
return $this->collectionOperations; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Returns a new instance with the given collection operations. |
146
|
|
|
*/ |
147
|
|
|
public function withCollectionOperations(array $collectionOperations): self |
148
|
|
|
{ |
149
|
|
|
$metadata = clone $this; |
150
|
|
|
$metadata->collectionOperations = $collectionOperations; |
151
|
|
|
|
152
|
|
|
return $metadata; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Gets subresource operations. |
157
|
|
|
*/ |
158
|
|
|
public function getSubresourceOperations(): ?array |
159
|
|
|
{ |
160
|
|
|
return $this->subresourceOperations; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Returns a new instance with the given subresource operations. |
165
|
|
|
*/ |
166
|
|
|
public function withSubresourceOperations(array $subresourceOperations): self |
167
|
|
|
{ |
168
|
|
|
$metadata = clone $this; |
169
|
|
|
$metadata->subresourceOperations = $subresourceOperations; |
170
|
|
|
|
171
|
|
|
return $metadata; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Gets a collection operation attribute, optionally fallback to a resource attribute. |
176
|
|
|
* |
177
|
|
|
* @param mixed|null $defaultValue |
178
|
|
|
*/ |
179
|
|
|
public function getCollectionOperationAttribute(?string $operationName, string $key, $defaultValue = null, bool $resourceFallback = false) |
180
|
|
|
{ |
181
|
|
|
return $this->findOperationAttribute($this->collectionOperations, $operationName, $key, $defaultValue, $resourceFallback); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Gets an item operation attribute, optionally fallback to a resource attribute. |
186
|
|
|
* |
187
|
|
|
* @param mixed|null $defaultValue |
188
|
|
|
*/ |
189
|
|
|
public function getItemOperationAttribute(?string $operationName, string $key, $defaultValue = null, bool $resourceFallback = false) |
190
|
|
|
{ |
191
|
|
|
return $this->findOperationAttribute($this->itemOperations, $operationName, $key, $defaultValue, $resourceFallback); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Gets a subresource operation attribute, optionally fallback to a resource attribute. |
196
|
|
|
* |
197
|
|
|
* @param mixed|null $defaultValue |
198
|
|
|
*/ |
199
|
|
|
public function getSubresourceOperationAttribute(?string $operationName, string $key, $defaultValue = null, bool $resourceFallback = false) |
200
|
|
|
{ |
201
|
|
|
return $this->findOperationAttribute($this->subresourceOperations, $operationName, $key, $defaultValue, $resourceFallback); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function getGraphqlAttribute(string $operationName, string $key, $defaultValue = null, bool $resourceFallback = false) |
205
|
|
|
{ |
206
|
|
|
if (isset($this->graphql[$operationName][$key])) { |
207
|
|
|
return $this->graphql[$operationName][$key]; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if ($resourceFallback && isset($this->attributes[$key])) { |
211
|
|
|
return $this->attributes[$key]; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $defaultValue; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Gets the first available operation attribute according to the following order: collection, item, subresource, optionally fallback to a default value. |
219
|
|
|
* |
220
|
|
|
* @param mixed|null $defaultValue |
221
|
|
|
*/ |
222
|
|
|
public function getOperationAttribute(array $attributes, string $key, $defaultValue = null, bool $resourceFallback = false) |
223
|
|
|
{ |
224
|
|
|
if (isset($attributes['collection_operation_name'])) { |
225
|
|
|
return $this->getCollectionOperationAttribute($attributes['collection_operation_name'], $key, $defaultValue, $resourceFallback); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
if (isset($attributes['item_operation_name'])) { |
229
|
|
|
return $this->getItemOperationAttribute($attributes['item_operation_name'], $key, $defaultValue, $resourceFallback); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
if (isset($attributes['subresource_operation_name'])) { |
233
|
|
|
return $this->getSubresourceOperationAttribute($attributes['subresource_operation_name'], $key, $defaultValue, $resourceFallback); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if ($resourceFallback && isset($this->attributes[$key])) { |
237
|
|
|
return $this->attributes[$key]; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $defaultValue; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Gets an attribute for a given operation type and operation name. |
245
|
|
|
* |
246
|
|
|
* @param mixed|null $defaultValue |
247
|
|
|
*/ |
248
|
|
|
public function getTypedOperationAttribute(string $operationType, string $operationName, string $key, $defaultValue = null, bool $resourceFallback = false) |
249
|
|
|
{ |
250
|
|
|
switch ($operationType) { |
251
|
|
|
case OperationType::COLLECTION: |
252
|
|
|
return $this->getCollectionOperationAttribute($operationName, $key, $defaultValue, $resourceFallback); |
253
|
|
|
case OperationType::ITEM: |
254
|
|
|
return $this->getItemOperationAttribute($operationName, $key, $defaultValue, $resourceFallback); |
255
|
|
|
default: |
256
|
|
|
return $this->getSubresourceOperationAttribute($operationName, $key, $defaultValue, $resourceFallback); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Gets attributes. |
262
|
|
|
*/ |
263
|
|
|
public function getAttributes(): ?array |
264
|
|
|
{ |
265
|
|
|
return $this->attributes; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Gets an attribute. |
270
|
|
|
* |
271
|
|
|
* @param mixed|null $defaultValue |
272
|
|
|
*/ |
273
|
|
|
public function getAttribute(string $key, $defaultValue = null) |
274
|
|
|
{ |
275
|
|
|
return $this->attributes[$key] ?? $defaultValue; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Returns a new instance with the given attribute. |
280
|
|
|
*/ |
281
|
|
|
public function withAttributes(array $attributes): self |
282
|
|
|
{ |
283
|
|
|
$metadata = clone $this; |
284
|
|
|
$metadata->attributes = $attributes; |
285
|
|
|
|
286
|
|
|
return $metadata; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Gets options of for the GraphQL query. |
291
|
|
|
*/ |
292
|
|
|
public function getGraphql(): ?array |
293
|
|
|
{ |
294
|
|
|
return $this->graphql; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Returns a new instance with the given GraphQL options. |
299
|
|
|
*/ |
300
|
|
|
public function withGraphql(array $graphql): self |
301
|
|
|
{ |
302
|
|
|
$metadata = clone $this; |
303
|
|
|
$metadata->graphql = $graphql; |
304
|
|
|
|
305
|
|
|
return $metadata; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Gets an operation attribute, optionally fallback to a resource attribute. |
310
|
|
|
* |
311
|
|
|
* @param mixed|null $defaultValue |
312
|
|
|
*/ |
313
|
|
|
private function findOperationAttribute(?array $operations, ?string $operationName, string $key, $defaultValue, bool $resourceFallback) |
314
|
|
|
{ |
315
|
|
|
if (null !== $operationName && isset($operations[$operationName][$key])) { |
316
|
|
|
return $operations[$operationName][$key]; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
if ($resourceFallback && isset($this->attributes[$key])) { |
320
|
|
|
return $this->attributes[$key]; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
return $defaultValue; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|