Completed
Pull Request — 2.0 (#1100)
by Antoine
08:11 queued 04:23
created

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