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 $attributes; |
29
|
|
|
|
30
|
|
|
public function __construct(string $shortName = null, string $description = null, string $iri = null, array $itemOperations = null, array $collectionOperations = null, array $attributes = null) |
31
|
|
|
{ |
32
|
|
|
$this->shortName = $shortName; |
33
|
|
|
$this->description = $description; |
34
|
|
|
$this->iri = $iri; |
35
|
|
|
$this->itemOperations = $itemOperations; |
36
|
|
|
$this->collectionOperations = $collectionOperations; |
37
|
|
|
$this->attributes = $attributes; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Gets the short name. |
42
|
|
|
* |
43
|
|
|
* @return string|null |
44
|
|
|
*/ |
45
|
|
|
public function getShortName() |
46
|
|
|
{ |
47
|
|
|
return $this->shortName; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns a new instance with the given short name. |
52
|
|
|
* |
53
|
|
|
* @param string $shortName |
54
|
|
|
* |
55
|
|
|
* @return self |
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
|
|
|
* @param string $description |
79
|
|
|
* |
80
|
|
|
* @return self |
81
|
|
|
*/ |
82
|
|
|
public function withDescription(string $description): self |
83
|
|
|
{ |
84
|
|
|
$metadata = clone $this; |
85
|
|
|
$metadata->description = $description; |
86
|
|
|
|
87
|
|
|
return $metadata; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Gets the associated IRI. |
92
|
|
|
* |
93
|
|
|
* @return string|null |
94
|
|
|
*/ |
95
|
|
|
public function getIri() |
96
|
|
|
{ |
97
|
|
|
return $this->iri; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns a new instance with the given IRI. |
102
|
|
|
* |
103
|
|
|
* @param string $iri |
104
|
|
|
* |
105
|
|
|
* @return self |
106
|
|
|
*/ |
107
|
|
|
public function withIri(string $iri): self |
108
|
|
|
{ |
109
|
|
|
$metadata = clone $this; |
110
|
|
|
$metadata->iri = $iri; |
111
|
|
|
|
112
|
|
|
return $metadata; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Gets item operations. |
117
|
|
|
* |
118
|
|
|
* @return array|null |
119
|
|
|
*/ |
120
|
|
|
public function getItemOperations() |
121
|
|
|
{ |
122
|
|
|
return $this->itemOperations; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns a new instance with the given item operations. |
127
|
|
|
* |
128
|
|
|
* @param array $itemOperations |
129
|
|
|
* |
130
|
|
|
* @return self |
131
|
|
|
*/ |
132
|
|
|
public function withItemOperations(array $itemOperations): self |
133
|
|
|
{ |
134
|
|
|
$metadata = clone $this; |
135
|
|
|
$metadata->itemOperations = $itemOperations; |
136
|
|
|
|
137
|
|
|
return $metadata; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Gets collection operations. |
142
|
|
|
* |
143
|
|
|
* @return array|null |
144
|
|
|
*/ |
145
|
|
|
public function getCollectionOperations() |
146
|
|
|
{ |
147
|
|
|
return $this->collectionOperations; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Returns a new instance with the given collection operations. |
152
|
|
|
* |
153
|
|
|
* @param array $collectionOperations |
154
|
|
|
* |
155
|
|
|
* @return self |
156
|
|
|
*/ |
157
|
|
|
public function withCollectionOperations(array $collectionOperations): self |
158
|
|
|
{ |
159
|
|
|
$metadata = clone $this; |
160
|
|
|
$metadata->collectionOperations = $collectionOperations; |
161
|
|
|
|
162
|
|
|
return $metadata; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Gets a collection operation attribute, optionally fallback to a resource attribute. |
167
|
|
|
* |
168
|
|
|
* @param string|null $operationName |
169
|
|
|
* @param string $key |
170
|
|
|
* @param mixed $defaultValue |
171
|
|
|
* @param bool $resourceFallback |
172
|
|
|
* |
173
|
|
|
* @return mixed |
174
|
|
|
*/ |
175
|
|
|
public function getCollectionOperationAttribute(string $operationName = null, string $key, $defaultValue = null, bool $resourceFallback = false) |
176
|
|
|
{ |
177
|
|
|
return $this->getOperationAttribute($this->collectionOperations, $operationName, $key, $defaultValue, $resourceFallback); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Gets an item operation attribute, optionally fallback to a resource attribute. |
182
|
|
|
* |
183
|
|
|
* @param string|null $operationName |
184
|
|
|
* @param string $key |
185
|
|
|
* @param mixed $defaultValue |
186
|
|
|
* @param bool $resourceFallback |
187
|
|
|
* |
188
|
|
|
* @return mixed |
189
|
|
|
*/ |
190
|
|
|
public function getItemOperationAttribute(string $operationName = null, string $key, $defaultValue = null, bool $resourceFallback = false) |
191
|
|
|
{ |
192
|
|
|
return $this->getOperationAttribute($this->itemOperations, $operationName, $key, $defaultValue, $resourceFallback); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Gets an operation attribute, optionally fallback to a resource attribute. |
197
|
|
|
* |
198
|
|
|
* @param array|null $operations |
199
|
|
|
* @param string|null $operationName |
200
|
|
|
* @param string $key |
201
|
|
|
* @param mixed $defaultValue |
202
|
|
|
* @param bool $resourceFallback |
203
|
|
|
* |
204
|
|
|
* @return mixed |
205
|
|
|
*/ |
206
|
|
|
private function getOperationAttribute(array $operations = null, string $operationName = null, string $key, $defaultValue = null, bool $resourceFallback = false) |
207
|
|
|
{ |
208
|
|
|
if (null !== $operationName && isset($operations[$operationName][$key])) { |
209
|
|
|
return $operations[$operationName][$key]; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if ($resourceFallback && isset($this->attributes[$key])) { |
213
|
|
|
return $this->attributes[$key]; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $defaultValue; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Gets attributes. |
221
|
|
|
* |
222
|
|
|
* @return array|null |
223
|
|
|
*/ |
224
|
|
|
public function getAttributes() |
225
|
|
|
{ |
226
|
|
|
return $this->attributes; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Gets an attribute. |
231
|
|
|
* |
232
|
|
|
* @param string $key |
233
|
|
|
* @param mixed $defaultValue |
234
|
|
|
* |
235
|
|
|
* @return mixed |
236
|
|
|
*/ |
237
|
|
|
public function getAttribute(string $key, $defaultValue = null) |
238
|
|
|
{ |
239
|
|
|
if (isset($this->attributes[$key])) { |
240
|
|
|
return $this->attributes[$key]; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return $defaultValue; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Returns a new instance with the given attribute. |
248
|
|
|
* |
249
|
|
|
* @param array $attributes |
250
|
|
|
* |
251
|
|
|
* @return self |
252
|
|
|
*/ |
253
|
|
|
public function withAttributes(array $attributes): self |
254
|
|
|
{ |
255
|
|
|
$metadata = clone $this; |
256
|
|
|
$metadata->attributes = $attributes; |
257
|
|
|
|
258
|
|
|
return $metadata; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|