Completed
Push — grapqlops ( 8457ea )
by Kévin
02:47
created

ResourceMetadata::getAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
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->getOperationAttribute($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->getOperationAttribute($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->getOperationAttribute($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 getOperationAttribute(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 attributes.
244
     *
245
     * @return array|null
246
     */
247
    public function getAttributes()
248
    {
249
        return $this->attributes;
250
    }
251
252
    /**
253
     * Gets an attribute.
254
     *
255
     * @param mixed $defaultValue
256
     *
257
     * @return mixed
258
     */
259
    public function getAttribute(string $key, $defaultValue = null)
260
    {
261
        if (isset($this->attributes[$key])) {
262
            return $this->attributes[$key];
263
        }
264
265
        return $defaultValue;
266
    }
267
268
    /**
269
     * Returns a new instance with the given attribute.
270
     */
271
    public function withAttributes(array $attributes): self
272
    {
273
        $metadata = clone $this;
274
        $metadata->attributes = $attributes;
275
276
        return $metadata;
277
    }
278
279
    /**
280
     * Gets options of for the GraphQL query.
281
     *
282
     * @return array|null
283
     */
284
    public function getGraphql()
285
    {
286
        return $this->graphql;
287
    }
288
289
    /**
290
     * Returns a new instance with the given GraphQL options.
291
     */
292
    public function withGraphql(array $graphql): self
293
    {
294
        $metadata = clone $this;
295
        $metadata->graphql = $graphql;
296
297
        return $metadata;
298
    }
299
}
300