Completed
Pull Request — master (#1492)
by Kévin
03:13
created

ResourceMetadata::withGraphqlQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
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 $itemOperations;
27
    private $collectionOperations;
28
    private $graphqlQuery;
29
    private $attributes;
30
31
    public function __construct(string $shortName = null, string $description = null, string $iri = null, array $itemOperations = null, array $collectionOperations = null, array $attributes = null, array $graphqlQuery = null)
32
    {
33
        $this->shortName = $shortName;
34
        $this->description = $description;
35
        $this->iri = $iri;
36
        $this->itemOperations = $itemOperations;
37
        $this->collectionOperations = $collectionOperations;
38
        $this->graphqlQuery = $graphqlQuery;
39
        $this->attributes = $attributes;
40
    }
41
42
    /**
43
     * Gets the short name.
44
     *
45
     * @return string|null
46
     */
47
    public function getShortName()
48
    {
49
        return $this->shortName;
50
    }
51
52
    /**
53
     * Returns a new instance with the given short name.
54
     *
55
     * @param string $shortName
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 a collection operation attribute, optionally fallback to a resource attribute.
151
     *
152
     * @param string|null $operationName
153
     * @param mixed       $defaultValue
154
     *
155
     * @return mixed
156
     */
157
    public function getCollectionOperationAttribute(string $operationName = null, string $key, $defaultValue = null, bool $resourceFallback = false)
158
    {
159
        return $this->getOperationAttribute($this->collectionOperations, $operationName, $key, $defaultValue, $resourceFallback);
160
    }
161
162
    /**
163
     * Gets an item operation attribute, optionally fallback to a resource attribute.
164
     *
165
     * @param string|null $operationName
166
     * @param mixed       $defaultValue
167
     *
168
     * @return mixed
169
     */
170
    public function getItemOperationAttribute(string $operationName = null, string $key, $defaultValue = null, bool $resourceFallback = false)
171
    {
172
        return $this->getOperationAttribute($this->itemOperations, $operationName, $key, $defaultValue, $resourceFallback);
173
    }
174
175
    /**
176
     * Gets an operation attribute, optionally fallback to a resource attribute.
177
     *
178
     * @param array|null  $operations
179
     * @param string|null $operationName
180
     * @param mixed       $defaultValue
181
     *
182
     * @return mixed
183
     */
184
    private function getOperationAttribute(array $operations = null, string $operationName = null, string $key, $defaultValue = null, bool $resourceFallback = false)
185
    {
186
        if (null !== $operationName && isset($operations[$operationName][$key])) {
187
            return $operations[$operationName][$key];
188
        }
189
190
        if ($resourceFallback && isset($this->attributes[$key])) {
191
            return $this->attributes[$key];
192
        }
193
194
        return $defaultValue;
195
    }
196
197
    /**
198
     * @param mixed $defaultValue
199
     *
200
     * @return mixed
201
     */
202
    public function getGraphqlQueryAttribute(string $key, $defaultValue = null, bool $resourceFallback = false)
203
    {
204
        if (isset($this->graphqlQuery[$key])) {
205
            return $this->graphqlQuery[$key];
206
        }
207
208
        if ($resourceFallback && isset($this->attributes[$key])) {
209
            return $this->attributes[$key];
210
        }
211
212
        return $defaultValue;
213
    }
214
215
    /**
216
     * Gets attributes.
217
     *
218
     * @return array|null
219
     */
220
    public function getAttributes()
221
    {
222
        return $this->attributes;
223
    }
224
225
    /**
226
     * Gets an attribute.
227
     *
228
     * @param mixed $defaultValue
229
     *
230
     * @return mixed
231
     */
232
    public function getAttribute(string $key, $defaultValue = null)
233
    {
234
        if (isset($this->attributes[$key])) {
235
            return $this->attributes[$key];
236
        }
237
238
        return $defaultValue;
239
    }
240
241
    /**
242
     * Returns a new instance with the given attribute.
243
     */
244
    public function withAttributes(array $attributes): self
245
    {
246
        $metadata = clone $this;
247
        $metadata->attributes = $attributes;
248
249
        return $metadata;
250
    }
251
252
    /**
253
     * Gets options of for the GraphQL query.
254
     *
255
     * @return array|null
256
     */
257
    public function getGraphqlQuery()
258
    {
259
        return $this->graphqlQuery;
260
    }
261
262
    /**
263
     * Returns a new instance with the given GraphQL query options.
264
     */
265
    public function withGraphqlQuery(array $graphqlQuery): self
266
    {
267
        $metadata = clone $this;
268
        $metadata->graphqlQuery = $graphqlQuery;
269
270
        return $metadata;
271
    }
272
}
273