Passed
Pull Request — master (#2181)
by Samuel
03:09
created

PropertyMetadata::withAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
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\Property;
15
16
use Symfony\Component\PropertyInfo\Type;
17
18
/**
19
 * Property metadata.
20
 *
21
 * @author Kévin Dunglas <[email protected]>
22
 */
23
final class PropertyMetadata
24
{
25
    private $type;
26
    private $description;
27
    private $readable;
28
    private $writable;
29
    private $readableLink;
30
    private $writableLink;
31
    private $required;
32
    private $iri;
33
    private $identifier;
34
    private $childInherited;
35
    private $attributes;
36
    private $subresource;
37
    private $initializable;
38
39
    public function __construct(Type $type = null, string $description = null, bool $readable = null, bool $writable = null, bool $readableLink = null, bool $writableLink = null, bool $required = null, bool $identifier = null, string $iri = null, $childInherited = null, array $attributes = null, SubresourceMetadata $subresource = null, bool $initializable = null)
40
    {
41
        $this->type = $type;
42
        $this->description = $description;
43
        $this->readable = $readable;
44
        $this->writable = $writable;
45
        $this->readableLink = $readableLink;
46
        $this->writableLink = $writableLink;
47
        $this->required = $required;
48
        $this->identifier = $identifier;
49
        $this->iri = $iri;
50
        $this->childInherited = $childInherited;
51
        $this->attributes = $attributes;
52
        $this->subresource = $subresource;
53
        $this->initializable = $initializable;
54
    }
55
56
    /**
57
     * Gets type.
58
     *
59
     * @return Type|null
60
     */
61
    public function getType()
62
    {
63
        return $this->type;
64
    }
65
66
    /**
67
     * Returns a new instance with the given type.
68
     */
69
    public function withType(Type $type): self
70
    {
71
        $metadata = clone $this;
72
        $metadata->type = $type;
73
74
        return $metadata;
75
    }
76
77
    /**
78
     * Gets description.
79
     *
80
     * @return string|null
81
     */
82
    public function getDescription()
83
    {
84
        return $this->description;
85
    }
86
87
    /**
88
     * Returns a new instance with the given description.
89
     *
90
     * @param string $description
91
     */
92
    public function withDescription($description): self
93
    {
94
        $metadata = clone $this;
95
        $metadata->description = $description;
96
97
        return $metadata;
98
    }
99
100
    /**
101
     * Is readable?
102
     *
103
     * @return bool|null
104
     */
105
    public function isReadable()
106
    {
107
        return $this->readable;
108
    }
109
110
    /**
111
     * Returns a new instance of Metadata with the given readable flag.
112
     */
113
    public function withReadable(bool $readable): self
114
    {
115
        $metadata = clone $this;
116
        $metadata->readable = $readable;
117
118
        return $metadata;
119
    }
120
121
    /**
122
     * Is writable?
123
     *
124
     * @return bool|null
125
     */
126
    public function isWritable()
127
    {
128
        return $this->writable;
129
    }
130
131
    /**
132
     * Returns a new instance with the given writable flag.
133
     */
134
    public function withWritable(bool $writable): self
135
    {
136
        $metadata = clone $this;
137
        $metadata->writable = $writable;
138
139
        return $metadata;
140
    }
141
142
    /**
143
     * Is required?
144
     *
145
     * @return bool|null
146
     */
147
    public function isRequired()
148
    {
149
        if (true === $this->required && false === $this->writable) {
150
            return false;
151
        }
152
153
        return $this->required;
154
    }
155
156
    /**
157
     * Returns a new instance with the given required flag.
158
     */
159
    public function withRequired(bool $required): self
160
    {
161
        $metadata = clone $this;
162
        $metadata->required = $required;
163
164
        return $metadata;
165
    }
166
167
    /**
168
     * Should an IRI or an object be provided in write context?
169
     *
170
     * @return bool|null
171
     */
172
    public function isWritableLink()
173
    {
174
        return $this->writableLink;
175
    }
176
177
    /**
178
     * Returns a new instance with the given writable link flag.
179
     */
180
    public function withWritableLink(bool $writableLink): self
181
    {
182
        $metadata = clone $this;
183
        $metadata->writableLink = $writableLink;
184
185
        return $metadata;
186
    }
187
188
    /**
189
     * Is an IRI or an object generated in read context?
190
     *
191
     * @return bool|null
192
     */
193
    public function isReadableLink()
194
    {
195
        return $this->readableLink;
196
    }
197
198
    /**
199
     * Returns a new instance with the given readable link flag.
200
     */
201
    public function withReadableLink(bool $readableLink): self
202
    {
203
        $metadata = clone $this;
204
        $metadata->readableLink = $readableLink;
205
206
        return $metadata;
207
    }
208
209
    /**
210
     * Gets IRI of this property.
211
     *
212
     * @return string|null
213
     */
214
    public function getIri()
215
    {
216
        return $this->iri;
217
    }
218
219
    /**
220
     * Returns a new instance with the given IRI.
221
     */
222
    public function withIri(string $iri = null): self
223
    {
224
        $metadata = clone $this;
225
        $metadata->iri = $iri;
226
227
        return $metadata;
228
    }
229
230
    /**
231
     * Is this attribute an identifier?
232
     *
233
     * @return bool|null
234
     */
235
    public function isIdentifier()
236
    {
237
        return $this->identifier;
238
    }
239
240
    /**
241
     * Returns a new instance with the given identifier flag.
242
     */
243
    public function withIdentifier(bool $identifier): self
244
    {
245
        $metadata = clone $this;
246
        $metadata->identifier = $identifier;
247
248
        return $metadata;
249
    }
250
251
    /**
252
     * Gets attributes.
253
     *
254
     * @return array|null
255
     */
256
    public function getAttributes()
257
    {
258
        return $this->attributes;
259
    }
260
261
    /**
262
     * Gets an attribute.
263
     */
264
    public function getAttribute(string $key, $defaultValue = null)
265
    {
266
        if (isset($this->attributes[$key])) {
267
            return $this->attributes[$key];
268
        }
269
270
        return $defaultValue;
271
    }
272
273
    /**
274
     * Returns a new instance with the given attribute.
275
     */
276
    public function withAttributes(array $attributes): self
277
    {
278
        $metadata = clone $this;
279
        $metadata->attributes = $attributes;
280
281
        return $metadata;
282
    }
283
284
    /**
285
     * Is the property inherited from a child class?
286
     *
287
     * @return string|null
288
     */
289
    public function isChildInherited()
290
    {
291
        return $this->childInherited;
292
    }
293
294
    /**
295
     * Returns a new instance with the given child inherited class.
296
     */
297
    public function withChildInherited(string $childInherited): self
298
    {
299
        $metadata = clone $this;
300
        $metadata->childInherited = $childInherited;
301
302
        return $metadata;
303
    }
304
305
    /**
306
     * Represents whether the property has a subresource.
307
     */
308
    public function hasSubresource(): bool
309
    {
310
        return null !== $this->subresource;
311
    }
312
313
    /**
314
     * Gets the subresource metadata.
315
     *
316
     * @return SubresourceMetadata|null
317
     */
318
    public function getSubresource()
319
    {
320
        return $this->subresource;
321
    }
322
323
    /**
324
     * Returns a new instance with the given subresource.
325
     *
326
     * @param SubresourceMetadata $subresource
327
     */
328
    public function withSubresource(SubresourceMetadata $subresource = null): self
329
    {
330
        $metadata = clone $this;
331
        $metadata->subresource = $subresource;
332
333
        return $metadata;
334
    }
335
336
    /**
337
     * Is initializable?
338
     *
339
     * @return bool|null
340
     */
341
    public function isInitializable()
342
    {
343
        return $this->initializable;
344
    }
345
346
    /**
347
     * Returns a new instance with the given initializable flag.
348
     */
349
    public function withInitializable(bool $initializable): self
350
    {
351
        $metadata = clone $this;
352
        $metadata->initializable = $initializable;
353
354
        return $metadata;
355
    }
356
}
357