Completed
Pull Request — master (#3329)
by Andrii
07:04
created

PropertyMetadata::withSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
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
    /**
35
     * @deprecated since 2.6, to be removed in 3.0
36
     */
37
    private $childInherited;
38
    private $attributes;
39
    private $subresource;
40
    private $initializable;
41
    /**
42
     * @var null
43
     */
44
    private $default;
45
    /**
46
     * @var null
47
     */
48
    private $example;
49
50
    /**
51
     * @var array
52
     */
53
    private $schema;
54
55
    public function __construct(
56
        Type $type = null,
57
        string $description = null,
58
        bool $readable = null,
59
        bool $writable = null,
60
        bool $readableLink = null,
61
        bool $writableLink = null,
62
        bool $required = null,
63
        bool $identifier = null,
64
        string $iri = null,
65
        $childInherited = null,
66
        array $attributes = null,
67
        SubresourceMetadata $subresource = null,
68
        bool $initializable = null,
69
        $default = null,
70
        $example = null,
71
        array $schema = null
72
    ) {
73
        $this->type = $type;
74
        $this->description = $description;
75
        $this->readable = $readable;
76
        $this->writable = $writable;
77
        $this->readableLink = $readableLink;
78
        $this->writableLink = $writableLink;
79
        $this->required = $required;
80
        $this->identifier = $identifier;
81
        $this->iri = $iri;
82
        if (null !== $childInherited) {
83
            @trigger_error(sprintf('Providing a non-null value for the 10th argument ($childInherited) of the "%s" constructor is deprecated since 2.6 and will not be supported in 3.0.', __CLASS__), E_USER_DEPRECATED);
84
        }
85
        $this->childInherited = $childInherited;
0 ignored issues
show
Deprecated Code introduced by
The property ApiPlatform\Core\Metadat...tadata::$childInherited has been deprecated: since 2.6, to be removed in 3.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

85
        /** @scrutinizer ignore-deprecated */ $this->childInherited = $childInherited;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
86
        $this->attributes = $attributes;
87
        $this->subresource = $subresource;
88
        $this->initializable = $initializable;
89
        $this->default = $default;
90
        $this->example = $example;
91
        $this->schema = $schema;
92
    }
93
94
    /**
95
     * Gets type.
96
     */
97
    public function getType(): ?Type
98
    {
99
        return $this->type;
100
    }
101
102
    /**
103
     * Returns a new instance with the given type.
104
     */
105
    public function withType(Type $type): self
106
    {
107
        $metadata = clone $this;
108
        $metadata->type = $type;
109
110
        return $metadata;
111
    }
112
113
    /**
114
     * Gets description.
115
     */
116
    public function getDescription(): ?string
117
    {
118
        return $this->description;
119
    }
120
121
    /**
122
     * Returns a new instance with the given description.
123
     */
124
    public function withDescription(string $description): self
125
    {
126
        $metadata = clone $this;
127
        $metadata->description = $description;
128
129
        return $metadata;
130
    }
131
132
    /**
133
     * Is readable?
134
     */
135
    public function isReadable(): ?bool
136
    {
137
        return $this->readable;
138
    }
139
140
    /**
141
     * Returns a new instance of Metadata with the given readable flag.
142
     */
143
    public function withReadable(bool $readable): self
144
    {
145
        $metadata = clone $this;
146
        $metadata->readable = $readable;
147
148
        return $metadata;
149
    }
150
151
    /**
152
     * Is writable?
153
     */
154
    public function isWritable(): ?bool
155
    {
156
        return $this->writable;
157
    }
158
159
    /**
160
     * Returns a new instance with the given writable flag.
161
     */
162
    public function withWritable(bool $writable): self
163
    {
164
        $metadata = clone $this;
165
        $metadata->writable = $writable;
166
167
        return $metadata;
168
    }
169
170
    /**
171
     * Is required?
172
     */
173
    public function isRequired(): ?bool
174
    {
175
        if (true === $this->required && false === $this->writable) {
176
            return false;
177
        }
178
179
        return $this->required;
180
    }
181
182
    /**
183
     * Returns a new instance with the given required flag.
184
     */
185
    public function withRequired(bool $required): self
186
    {
187
        $metadata = clone $this;
188
        $metadata->required = $required;
189
190
        return $metadata;
191
    }
192
193
    /**
194
     * Should an IRI or an object be provided in write context?
195
     */
196
    public function isWritableLink(): ?bool
197
    {
198
        return $this->writableLink;
199
    }
200
201
    /**
202
     * Returns a new instance with the given writable link flag.
203
     */
204
    public function withWritableLink(bool $writableLink): self
205
    {
206
        $metadata = clone $this;
207
        $metadata->writableLink = $writableLink;
208
209
        return $metadata;
210
    }
211
212
    /**
213
     * Is an IRI or an object generated in read context?
214
     */
215
    public function isReadableLink(): ?bool
216
    {
217
        return $this->readableLink;
218
    }
219
220
    /**
221
     * Returns a new instance with the given readable link flag.
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
    public function getIri(): ?string
235
    {
236
        return $this->iri;
237
    }
238
239
    /**
240
     * Returns a new instance with the given IRI.
241
     */
242
    public function withIri(string $iri = null): self
243
    {
244
        $metadata = clone $this;
245
        $metadata->iri = $iri;
246
247
        return $metadata;
248
    }
249
250
    /**
251
     * Is this attribute an identifier?
252
     */
253
    public function isIdentifier(): ?bool
254
    {
255
        return $this->identifier;
256
    }
257
258
    /**
259
     * Returns a new instance with the given identifier flag.
260
     */
261
    public function withIdentifier(bool $identifier): self
262
    {
263
        $metadata = clone $this;
264
        $metadata->identifier = $identifier;
265
266
        return $metadata;
267
    }
268
269
    /**
270
     * Gets attributes.
271
     */
272
    public function getAttributes(): ?array
273
    {
274
        return $this->attributes;
275
    }
276
277
    /**
278
     * Gets an attribute.
279
     *
280
     * @param mixed|null $defaultValue
281
     */
282
    public function getAttribute(string $key, $defaultValue = null)
283
    {
284
        return $this->attributes[$key] ?? $defaultValue;
285
    }
286
287
    /**
288
     * Returns a new instance with the given attribute.
289
     */
290
    public function withAttributes(array $attributes): self
291
    {
292
        $metadata = clone $this;
293
        $metadata->attributes = $attributes;
294
295
        return $metadata;
296
    }
297
298
    /**
299
     * @deprecated since 2.6, to be removed in 3.0
300
     */
301
    public function getChildInherited(): ?string
302
    {
303
        return $this->childInherited;
0 ignored issues
show
Deprecated Code introduced by
The property ApiPlatform\Core\Metadat...tadata::$childInherited has been deprecated: since 2.6, to be removed in 3.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

303
        return /** @scrutinizer ignore-deprecated */ $this->childInherited;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
304
    }
305
306
    /**
307
     * @deprecated since 2.6, to be removed in 3.0
308
     */
309
    public function hasChildInherited(): bool
310
    {
311
        return null !== $this->childInherited;
0 ignored issues
show
Deprecated Code introduced by
The property ApiPlatform\Core\Metadat...tadata::$childInherited has been deprecated: since 2.6, to be removed in 3.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

311
        return null !== /** @scrutinizer ignore-deprecated */ $this->childInherited;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
312
    }
313
314
    /**
315
     * @deprecated since 2.4, to be removed in 3.0
316
     */
317
    public function isChildInherited(): ?string
318
    {
319
        @trigger_error(sprintf('"%s::%s" is deprecated since 2.4 and will be removed in 3.0.', __CLASS__, __METHOD__), E_USER_DEPRECATED);
320
321
        return $this->getChildInherited();
0 ignored issues
show
Deprecated Code introduced by
The function ApiPlatform\Core\Metadat...ta::getChildInherited() has been deprecated: since 2.6, to be removed in 3.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

321
        return /** @scrutinizer ignore-deprecated */ $this->getChildInherited();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
322
    }
323
324
    /**
325
     * @deprecated since 2.6, to be removed in 3.0
326
     */
327
    public function withChildInherited(string $childInherited): self
328
    {
329
        @trigger_error(sprintf('"%s::%s" is deprecated since 2.6 and will be removed in 3.0.', __CLASS__, __METHOD__), E_USER_DEPRECATED);
330
331
        $metadata = clone $this;
332
        $metadata->childInherited = $childInherited;
0 ignored issues
show
Deprecated Code introduced by
The property ApiPlatform\Core\Metadat...tadata::$childInherited has been deprecated: since 2.6, to be removed in 3.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

332
        /** @scrutinizer ignore-deprecated */ $metadata->childInherited = $childInherited;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
333
334
        return $metadata;
335
    }
336
337
    /**
338
     * Represents whether the property has a subresource.
339
     */
340
    public function hasSubresource(): bool
341
    {
342
        return null !== $this->subresource;
343
    }
344
345
    /**
346
     * Gets the subresource metadata.
347
     */
348
    public function getSubresource(): ?SubresourceMetadata
349
    {
350
        return $this->subresource;
351
    }
352
353
    /**
354
     * Returns a new instance with the given subresource.
355
     *
356
     * @param SubresourceMetadata $subresource
357
     */
358
    public function withSubresource(SubresourceMetadata $subresource = null): self
359
    {
360
        $metadata = clone $this;
361
        $metadata->subresource = $subresource;
362
363
        return $metadata;
364
    }
365
366
    /**
367
     * Is initializable?
368
     */
369
    public function isInitializable(): ?bool
370
    {
371
        return $this->initializable;
372
    }
373
374
    /**
375
     * Returns a new instance with the given initializable flag.
376
     */
377
    public function withInitializable(bool $initializable): self
378
    {
379
        $metadata = clone $this;
380
        $metadata->initializable = $initializable;
381
382
        return $metadata;
383
    }
384
385
    /**
386
     * Returns the default value of the property or NULL if the property doesn't have a default value.
387
     */
388
    public function getDefault()
389
    {
390
        return $this->default;
391
    }
392
393
    /**
394
     * Returns a new instance with the given default value for the property.
395
     */
396
    public function withDefault($default): self
397
    {
398
        $metadata = clone $this;
399
        $metadata->default = $default;
400
401
        return $metadata;
402
    }
403
404
    /**
405
     * Returns an example of the value of the property.
406
     */
407
    public function getExample()
408
    {
409
        return $this->example;
410
    }
411
412
    /**
413
     * Returns a new instance with the given example.
414
     */
415
    public function withExample($example): self
416
    {
417
        $metadata = clone $this;
418
        $metadata->example = $example;
419
420
        return $metadata;
421
    }
422
423
    /**
424
     * @return array
425
     */
426
    public function getSchema(): ?array
427
    {
428
        return $this->schema;
429
    }
430
431
    /**
432
     * Returns a new instance with the given schema.
433
     */
434
    public function withSchema(array $schema = null): self
435
    {
436
        $metadata = clone $this;
437
        $metadata->schema = $schema;
438
439
        return $metadata;
440
    }
441
}
442