Passed
Pull Request — master (#1685)
by Yanick
06:55
created

ResourceMetadata::withRoutePrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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 $routePrefix;
27
    private $itemOperations;
28
    private $collectionOperations;
29
    private $subresourceOperations;
30
    private $graphql;
31
    private $attributes;
32
33
    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, string $routePrefix = null)
34
    {
35
        $this->shortName = $shortName;
36
        $this->description = $description;
37
        $this->iri = $iri;
38
        $this->itemOperations = $itemOperations;
39
        $this->collectionOperations = $collectionOperations;
40
        $this->subresourceOperations = $subresourceOperations;
41
        $this->graphql = $graphql;
42
        $this->attributes = $attributes;
43
        $this->routePrefix = $routePrefix;
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 attributes.
270
     *
271
     * @return array|null
272
     */
273
    public function getAttributes()
274
    {
275
        return $this->attributes;
276
    }
277
278
    /**
279
     * Gets an attribute.
280
     *
281
     * @param mixed $defaultValue
282
     *
283
     * @return mixed
284
     */
285
    public function getAttribute(string $key, $defaultValue = null)
286
    {
287
        if (isset($this->attributes[$key])) {
288
            return $this->attributes[$key];
289
        }
290
291
        return $defaultValue;
292
    }
293
294
    /**
295
     * Returns a new instance with the given attribute.
296
     */
297
    public function withAttributes(array $attributes): self
298
    {
299
        $metadata = clone $this;
300
        $metadata->attributes = $attributes;
301
302
        return $metadata;
303
    }
304
305
    /**
306
     * Gets options of for the GraphQL query.
307
     *
308
     * @return array|null
309
     */
310
    public function getGraphql()
311
    {
312
        return $this->graphql;
313
    }
314
315
    /**
316
     * Returns a new instance with the given GraphQL options.
317
     */
318
    public function withGraphql(array $graphql): self
319
    {
320
        $metadata = clone $this;
321
        $metadata->graphql = $graphql;
322
323
        return $metadata;
324
    }
325
326
    /**
327
     * Gets the route prefix.
328
     *
329
     * @return string|null
330
     */
331
    public function getRoutePrefix()
332
    {
333
        return $this->routePrefix;
334
    }
335
336
    /**
337
     * Returns a new instance with the given route prefix.
338
     */
339
    public function withRoutePrefix(string $routePrefix): self
340
    {
341
        $metadata = clone $this;
342
        $metadata->routePrefix = $routePrefix;
343
344
        return $metadata;
345
    }
346
}
347