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