Completed
Push — grapqlops ( caf158 )
by Kévin
14:43
created

ResourceMetadata::getGraphql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 $graphql;
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 $graphql = null)
32
    {
33
        $this->shortName = $shortName;
34
        $this->description = $description;
35
        $this->iri = $iri;
36
        $this->itemOperations = $itemOperations;
37
        $this->collectionOperations = $collectionOperations;
38
        $this->graphql = $graphql;
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
     * @return mixed
199
     */
200
    public function getGraphqlAttribute(string $operationName, string $key, $defaultValue = null, bool $resourceFallback = false)
201
    {
202
        if (isset($this->graphql[$operationName][$key])) {
203
            return $this->graphql[$operationName][$key];
204
        }
205
206
        if ($resourceFallback && isset($this->attributes[$key])) {
207
            return $this->attributes[$key];
208
        }
209
210
        return $defaultValue;
211
    }
212
213
    /**
214
     * Gets attributes.
215
     *
216
     * @return array|null
217
     */
218
    public function getAttributes()
219
    {
220
        return $this->attributes;
221
    }
222
223
    /**
224
     * Gets an attribute.
225
     *
226
     * @param mixed $defaultValue
227
     *
228
     * @return mixed
229
     */
230
    public function getAttribute(string $key, $defaultValue = null)
231
    {
232
        if (isset($this->attributes[$key])) {
233
            return $this->attributes[$key];
234
        }
235
236
        return $defaultValue;
237
    }
238
239
    /**
240
     * Returns a new instance with the given attribute.
241
     */
242
    public function withAttributes(array $attributes): self
243
    {
244
        $metadata = clone $this;
245
        $metadata->attributes = $attributes;
246
247
        return $metadata;
248
    }
249
250
    /**
251
     * Gets options of for the GraphQL query.
252
     *
253
     * @return array|null
254
     */
255
    public function getGraphql()
256
    {
257
        return $this->graphql;
258
    }
259
260
    /**
261
     * Returns a new instance with the given GraphQL options.
262
     */
263
    public function withGraphql(array $graphql): self
264
    {
265
        $metadata = clone $this;
266
        $metadata->graphql = $graphql;
267
268
        return $metadata;
269
    }
270
}
271