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