Completed
Push — master ( d683d0...091865 )
by
unknown
16s
created

ResourceMetadata::getSubresourceOperations()   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 $subresourceOperations;
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 $subresourceOperations = null)
32
    {
33
        $this->shortName = $shortName;
34
        $this->description = $description;
35
        $this->iri = $iri;
36
        $this->itemOperations = $itemOperations;
37
        $this->collectionOperations = $collectionOperations;
38
        $this->subresourceOperations = $subresourceOperations;
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
    public function withShortName(string $shortName): self
56
    {
57
        $metadata = clone $this;
58
        $metadata->shortName = $shortName;
59
60
        return $metadata;
61
    }
62
63
    /**
64
     * Gets the description.
65
     *
66
     * @return string|null
67
     */
68
    public function getDescription()
69
    {
70
        return $this->description;
71
    }
72
73
    /**
74
     * Returns a new instance with the given description.
75
     */
76
    public function withDescription(string $description): self
77
    {
78
        $metadata = clone $this;
79
        $metadata->description = $description;
80
81
        return $metadata;
82
    }
83
84
    /**
85
     * Gets the associated IRI.
86
     *
87
     * @return string|null
88
     */
89
    public function getIri()
90
    {
91
        return $this->iri;
92
    }
93
94
    /**
95
     * Returns a new instance with the given IRI.
96
     */
97
    public function withIri(string $iri): self
98
    {
99
        $metadata = clone $this;
100
        $metadata->iri = $iri;
101
102
        return $metadata;
103
    }
104
105
    /**
106
     * Gets item operations.
107
     *
108
     * @return array|null
109
     */
110
    public function getItemOperations()
111
    {
112
        return $this->itemOperations;
113
    }
114
115
    /**
116
     * Returns a new instance with the given item operations.
117
     */
118
    public function withItemOperations(array $itemOperations): self
119
    {
120
        $metadata = clone $this;
121
        $metadata->itemOperations = $itemOperations;
122
123
        return $metadata;
124
    }
125
126
    /**
127
     * Gets collection operations.
128
     *
129
     * @return array|null
130
     */
131
    public function getCollectionOperations()
132
    {
133
        return $this->collectionOperations;
134
    }
135
136
    /**
137
     * Returns a new instance with the given collection operations.
138
     */
139
    public function withCollectionOperations(array $collectionOperations): self
140
    {
141
        $metadata = clone $this;
142
        $metadata->collectionOperations = $collectionOperations;
143
144
        return $metadata;
145
    }
146
147
    /**
148
     * Gets subresource operations.
149
     *
150
     * @return array|null
151
     */
152
    public function getSubresourceOperations()
153
    {
154
        return $this->subresourceOperations;
155
    }
156
157
    /**
158
     * Returns a new instance with the given subresource operations.
159
     */
160
    public function withSubresourceOperations(array $subresourceOperations): self
161
    {
162
        $metadata = clone $this;
163
        $metadata->subresourceOperations = $subresourceOperations;
164
165
        return $metadata;
166
    }
167
168
    /**
169
     * Gets a collection operation attribute, optionally fallback to a resource attribute.
170
     *
171
     * @param mixed $defaultValue
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 mixed $defaultValue
184
     *
185
     * @return mixed
186
     */
187
    public function getItemOperationAttribute(string $operationName = null, string $key, $defaultValue = null, bool $resourceFallback = false)
188
    {
189
        return $this->getOperationAttribute($this->itemOperations, $operationName, $key, $defaultValue, $resourceFallback);
190
    }
191
192
    /**
193
     * Gets a subresource operation attribute, optionally fallback to a resource attribute.
194
     *
195
     * @param mixed $defaultValue
196
     *
197
     * @return mixed
198
     */
199
    public function getSubresourceOperationAttribute(string $operationName = null, string $key, $defaultValue = null, bool $resourceFallback = false)
200
    {
201
        return $this->getOperationAttribute($this->subresourceOperations, $operationName, $key, $defaultValue, $resourceFallback);
202
    }
203
204
    /**
205
     * Gets an operation attribute, optionally fallback to a resource attribute.
206
     *
207
     * @param mixed $defaultValue
208
     *
209
     * @return mixed
210
     */
211
    private function getOperationAttribute(array $operations = null, string $operationName = null, string $key, $defaultValue = null, bool $resourceFallback = false)
212
    {
213
        if (null !== $operationName && isset($operations[$operationName][$key])) {
214
            return $operations[$operationName][$key];
215
        }
216
217
        if ($resourceFallback && isset($this->attributes[$key])) {
218
            return $this->attributes[$key];
219
        }
220
221
        return $defaultValue;
222
    }
223
224
    /**
225
     * Gets attributes.
226
     *
227
     * @return array|null
228
     */
229
    public function getAttributes()
230
    {
231
        return $this->attributes;
232
    }
233
234
    /**
235
     * Gets an attribute.
236
     *
237
     * @param mixed $defaultValue
238
     *
239
     * @return mixed
240
     */
241
    public function getAttribute(string $key, $defaultValue = null)
242
    {
243
        if (isset($this->attributes[$key])) {
244
            return $this->attributes[$key];
245
        }
246
247
        return $defaultValue;
248
    }
249
250
    /**
251
     * Returns a new instance with the given attribute.
252
     */
253
    public function withAttributes(array $attributes): self
254
    {
255
        $metadata = clone $this;
256
        $metadata->attributes = $attributes;
257
258
        return $metadata;
259
    }
260
}
261