• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.62
/src/Metadata/ApiProperty.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Metadata;
15

16
use ApiPlatform\Metadata\Util\PropertyInfoToTypeInfoHelper;
17
use Symfony\Component\PropertyInfo\Type as LegacyType;
18
use Symfony\Component\Serializer\Attribute\Context;
19
use Symfony\Component\Serializer\Attribute\Groups;
20
use Symfony\Component\Serializer\Attribute\Ignore;
21
use Symfony\Component\Serializer\Attribute\MaxDepth;
22
use Symfony\Component\Serializer\Attribute\SerializedName;
23
use Symfony\Component\Serializer\Attribute\SerializedPath;
24
use Symfony\Component\TypeInfo\Type;
25

26
/**
27
 * ApiProperty annotation.
28
 *
29
 * @author Kévin Dunglas <dunglas@gmail.com>
30
 */
31
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::TARGET_PARAMETER | \Attribute::TARGET_CLASS_CONSTANT | \Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
32
final class ApiProperty
33
{
34
    private ?array $types;
35
    private ?array $serialize;
36
    private ?Type $nativeType;
37

38
    /**
39
     * @param bool|null                                                                                                                                   $readableLink            https://api-platform.com/docs/core/serialization/#force-iri-with-relations-of-the-same-type-parentchilds-relations
40
     * @param bool|null                                                                                                                                   $writableLink            https://api-platform.com/docs/core/serialization/#force-iri-with-relations-of-the-same-type-parentchilds-relations
41
     * @param bool|null                                                                                                                                   $required                https://api-platform.com/docs/admin/validation/#client-side-validation
42
     * @param bool|null                                                                                                                                   $identifier              https://api-platform.com/docs/core/identifiers/
43
     * @param mixed                                                                                                                                       $example                 https://api-platform.com/docs/core/openapi/#using-the-openapi-and-swagger-contexts
44
     * @param string|null                                                                                                                                 $deprecationReason       https://api-platform.com/docs/core/deprecations/#deprecating-resource-classes-operations-and-properties
45
     * @param bool|null                                                                                                                                   $fetchEager              https://api-platform.com/docs/core/performance/#eager-loading
46
     * @param array|null                                                                                                                                  $jsonldContext           https://api-platform.com/docs/core/extending-jsonld-context/#extending-json-ld-and-hydra-contexts
47
     * @param array|null                                                                                                                                  $openapiContext          https://api-platform.com/docs/core/openapi/#using-the-openapi-and-swagger-contexts
48
     * @param bool|null                                                                                                                                   $push                    https://api-platform.com/docs/core/push-relations/
49
     * @param string|\Stringable|null                                                                                                                     $security                https://api-platform.com/docs/core/security
50
     * @param string|\Stringable|null                                                                                                                     $securityPostDenormalize https://api-platform.com/docs/core/security/#executing-access-control-rules-after-denormalization
51
     * @param string[]|null                                                                                                                               $types                   the RDF types of this property
52
     * @param string[]|null                                                                                                                               $iris
53
     * @param LegacyType[]|null                                                                                                                           $builtinTypes
54
     * @param string|null                                                                                                                                 $uriTemplate             (experimental) whether to return the subRessource collection IRI instead of an iterable of IRI
55
     * @param string|null                                                                                                                                 $property                The property name
56
     * @param Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth|array<array-key, Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth> $serialize               Serializer attributes
57
     * @param Type|null                                                                                                                                   $nativeType              The internal PHP type
58
     */
59
    public function __construct(
60
        private ?string $description = null,
61
        private ?bool $readable = null,
62
        private ?bool $writable = null,
63
        private ?bool $readableLink = null,
64
        private ?bool $writableLink = null,
65
        private ?bool $required = null,
66
        private ?bool $identifier = null,
67
        private mixed $default = null,
68
        private mixed $example = null,
69
        /**
70
         * The `deprecationReason` option deprecates the current operation with a deprecation message.
71
         *
72
         * <div data-code-selector>
73
         *
74
         * ```php
75
         * <?php
76
         * // api/src/Entity/Review.php
77
         * use ApiPlatform\Metadata\ApiProperty;
78
         * use ApiPlatform\Metadata\ApiResource;
79
         *
80
         * #[ApiResource]
81
         * class Review
82
         * {
83
         *     #[ApiProperty(deprecationReason: "Use the rating property instead")]
84
         *     public string $letter;
85
         * }
86
         * ```
87
         *
88
         * ```yaml
89
         * # api/config/api_platform/properties.yaml
90
         * properties:
91
         *     App\Entity\Review:
92
         *         letter:
93
         *             deprecationReason: 'Create a Book instead'
94
         * ```
95
         *
96
         * ```xml
97
         * <?xml version="1.0" encoding="UTF-8" ?>
98
         * <!-- api/config/api_platform/properties.xml -->
99
         *
100
         * <properties
101
         *         xmlns="https://api-platform.com/schema/metadata/properties-3.0"
102
         *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
103
         *         xsi:schemaLocation="https://api-platform.com/schema/metadata/properties-3.0
104
         *         https://api-platform.com/schema/metadata/properties-3.0.xsd">
105
         *     <property resource="App\Entity\Review" name="letter" deprecationReason="Create a Book instead" />
106
         * </properties>
107
         * ```
108
         *
109
         * </div>
110
         *
111
         * - With JSON-lD / Hydra, [an `owl:deprecated` annotation property](https://www.w3.org/TR/owl2-syntax/#Annotation_Properties) will be added to the appropriate data structure
112
         * - With Swagger / OpenAPI, [a `deprecated` property](https://swagger.io/docs/specification/2-0/paths-and-operations/) will be added
113
         * - With GraphQL, the [`isDeprecated` and `deprecationReason` properties](https://facebook.github.io/graphql/June2018/#sec-Deprecation) will be added to the schema
114
         */
115
        private ?string $deprecationReason = null,
116
        private ?bool $fetchable = null,
117
        private ?bool $fetchEager = null,
118
        private ?array $jsonldContext = null,
119
        private ?array $openapiContext = null,
120
        private ?array $jsonSchemaContext = null,
121
        private ?bool $push = null,
122
        /**
123
         * The `security` option defines the access to the current property, on normalization process, based on Symfony Security.
124
         * It receives an `object` variable related to the current object, and a `property` variable related to the current property.
125
         *
126
         * <div data-code-selector>
127
         *
128
         * ```php
129
         * <?php
130
         * // api/src/Entity/Review.php
131
         * use ApiPlatform\Metadata\ApiProperty;
132
         * use ApiPlatform\Metadata\ApiResource;
133
         *
134
         * #[ApiResource]
135
         * class Review
136
         * {
137
         *     #[ApiProperty(security: 'is_granted("ROLE_ADMIN")')]
138
         *     public string $letter;
139
         * }
140
         * ```
141
         *
142
         * ```yaml
143
         * # api/config/api_platform/properties.yaml
144
         * properties:
145
         *     App\Entity\Review:
146
         *         letter:
147
         *             security: 'is_granted("ROLE_ADMIN")'
148
         * ```
149
         *
150
         * ```xml
151
         * <?xml version="1.0" encoding="UTF-8" ?>
152
         * <!-- api/config/api_platform/properties.xml -->
153
         *
154
         * <properties
155
         *         xmlns="https://api-platform.com/schema/metadata/properties-3.0"
156
         *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
157
         *         xsi:schemaLocation="https://api-platform.com/schema/metadata/properties-3.0
158
         *         https://api-platform.com/schema/metadata/properties-3.0.xsd">
159
         *     <property resource="App\Entity\Review" name="letter" security="is_granted('ROLE_ADMIN')" />
160
         * </properties>
161
         * ```
162
         *
163
         * </div>
164
         */
165
        private string|\Stringable|null $security = null,
166
        /**
167
         * The `securityPostDenormalize` option defines access to the current property after the denormalization process, based on Symfony Security.
168
         * It receives an `object` variable related to the current object, and a `property` variable related to the current property.
169
         *
170
         * <div data-code-selector>
171
         *
172
         * ```php
173
         * <?php
174
         * // api/src/Entity/Review.php
175
         * use ApiPlatform\Metadata\ApiProperty;
176
         * use ApiPlatform\Metadata\ApiResource;
177
         *
178
         * #[ApiResource]
179
         * class Review
180
         * {
181
         *     #[ApiProperty(securityPostDenormalize: 'is_granted("ROLE_ADMIN")')]
182
         *     public string $letter;
183
         * }
184
         * ```
185
         *
186
         * ```yaml
187
         * # api/config/api_platform/properties.yaml
188
         * properties:
189
         *     App\Entity\Review:
190
         *         letter:
191
         *             securityPostDenormalize: 'is_granted("ROLE_ADMIN")'
192
         * ```
193
         *
194
         * ```xml
195
         * <?xml version="1.0" encoding="UTF-8" ?>
196
         * <!-- api/config/api_platform/properties.xml -->
197
         *
198
         * <properties
199
         *         xmlns="https://api-platform.com/schema/metadata/properties-3.0"
200
         *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
201
         *         xsi:schemaLocation="https://api-platform.com/schema/metadata/properties-3.0
202
         *         https://api-platform.com/schema/metadata/properties-3.0.xsd">
203
         *     <property resource="App\Entity\Review" name="letter" securityPostDenormalize="is_granted('ROLE_ADMIN')" />
204
         * </properties>
205
         * ```
206
         *
207
         * </div>
208
         */
209
        private string|\Stringable|null $securityPostDenormalize = null,
210
        array|string|null $types = null,
211
        /*
212
         * The related php types.
213
         *
214
         * deprecated since 4.2, use "nativeType" instead.
215
         */
216
        private ?array $builtinTypes = null,
217
        private ?array $schema = null,
218
        private ?bool $initializable = null,
219
        private $iris = null,
220
        private ?bool $genId = null,
221
        private ?string $uriTemplate = null,
222
        private ?string $property = null,
223
        private ?string $policy = null,
224
        array|Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth|null $serialize = null,
225
        /**
226
         * Whether to document this property as a hydra:supportedProperty.
227
         */
228
        private ?bool $hydra = null,
229
        ?Type $nativeType = null,
230
        private array $extraProperties = [],
231
    ) {
232
        $this->types = \is_string($types) ? (array) $types : $types;
214✔
233
        $this->serialize = (null === $serialize || \is_array($serialize)) ? $serialize : [$serialize];
214✔
234
        $this->nativeType = $nativeType;
214✔
235

236
        if ($this->builtinTypes) {
214✔
237
            trigger_deprecation('api_platform/metadata', '4.2', \sprintf('The "builtinTypes" argument of "%s" is deprecated, use "nativeType" instead.', __CLASS__));
×
238
            $this->nativeType ??= PropertyInfoToTypeInfoHelper::convertLegacyTypesToType($this->builtinTypes);
×
239
        } elseif ($this->nativeType) {
214✔
240
            $this->builtinTypes = PropertyInfoToTypeInfoHelper::convertTypeToLegacyTypes($this->nativeType) ?? [];
×
241
        }
242
    }
243

244
    public function getProperty(): ?string
245
    {
246
        return $this->property;
64✔
247
    }
248

249
    public function withProperty(string $property): static
250
    {
251
        $self = clone $this;
4✔
252
        $self->property = $property;
4✔
253

254
        return $self;
4✔
255
    }
256

257
    public function getDescription(): ?string
258
    {
259
        return $this->description;
184✔
260
    }
261

262
    public function withDescription(string $description): static
263
    {
264
        $self = clone $this;
80✔
265
        $self->description = $description;
80✔
266

267
        return $self;
80✔
268
    }
269

270
    public function isReadable(): ?bool
271
    {
272
        return $this->readable;
542✔
273
    }
274

275
    public function withReadable(bool $readable): static
276
    {
277
        $self = clone $this;
202✔
278
        $self->readable = $readable;
202✔
279

280
        return $self;
202✔
281
    }
282

283
    public function isWritable(): ?bool
284
    {
285
        return $this->writable;
212✔
286
    }
287

288
    public function withWritable(bool $writable): static
289
    {
290
        $self = clone $this;
198✔
291
        $self->writable = $writable;
198✔
292

293
        return $self;
198✔
294
    }
295

296
    public function isReadableLink(): ?bool
297
    {
298
        return $this->readableLink;
198✔
299
    }
300

301
    public function withReadableLink(bool $readableLink): static
302
    {
303
        $self = clone $this;
86✔
304
        $self->readableLink = $readableLink;
86✔
305

306
        return $self;
86✔
307
    }
308

309
    public function isWritableLink(): ?bool
310
    {
311
        return $this->writableLink;
88✔
312
    }
313

314
    public function withWritableLink(bool $writableLink): static
315
    {
316
        $self = clone $this;
82✔
317
        $self->writableLink = $writableLink;
82✔
318

319
        return $self;
82✔
320
    }
321

322
    public function isRequired(): ?bool
323
    {
324
        return $this->required;
228✔
325
    }
326

327
    public function withRequired(bool $required): static
328
    {
329
        $self = clone $this;
172✔
330
        $self->required = $required;
172✔
331

332
        return $self;
172✔
333
    }
334

335
    public function isIdentifier(): ?bool
336
    {
337
        return $this->identifier;
194✔
338
    }
339

340
    public function withIdentifier(bool $identifier): static
341
    {
342
        $self = clone $this;
142✔
343
        $self->identifier = $identifier;
142✔
344

345
        return $self;
142✔
346
    }
347

348
    public function getDefault(): mixed
349
    {
350
        return $this->default;
172✔
351
    }
352

353
    public function withDefault(mixed $default): static
354
    {
355
        $self = clone $this;
120✔
356
        $self->default = $default;
120✔
357

358
        return $self;
120✔
359
    }
360

361
    public function getExample(): mixed
362
    {
363
        return $this->example;
172✔
364
    }
365

366
    public function withExample(mixed $example): static
367
    {
368
        $self = clone $this;
2✔
369
        $self->example = $example;
2✔
370

371
        return $self;
2✔
372
    }
373

374
    public function getDeprecationReason(): ?string
375
    {
376
        return $this->deprecationReason;
184✔
377
    }
378

379
    public function withDeprecationReason($deprecationReason): static
380
    {
381
        $self = clone $this;
8✔
382
        $self->deprecationReason = $deprecationReason;
8✔
383

384
        return $self;
8✔
385
    }
386

387
    public function isFetchable(): ?bool
388
    {
389
        return $this->fetchable;
64✔
390
    }
391

392
    public function withFetchable($fetchable): static
393
    {
394
        $self = clone $this;
×
395
        $self->fetchable = $fetchable;
×
396

397
        return $self;
×
398
    }
399

400
    public function getFetchEager(): ?bool
401
    {
402
        return $this->fetchEager;
76✔
403
    }
404

405
    public function withFetchEager($fetchEager): static
406
    {
407
        $self = clone $this;
×
408
        $self->fetchEager = $fetchEager;
×
409

410
        return $self;
×
411
    }
412

413
    public function getJsonldContext(): ?array
414
    {
415
        return $this->jsonldContext;
92✔
416
    }
417

418
    public function withJsonldContext($jsonldContext): static
419
    {
420
        $self = clone $this;
16✔
421
        $self->jsonldContext = $jsonldContext;
16✔
422

423
        return $self;
16✔
424
    }
425

426
    public function getOpenapiContext(): ?array
427
    {
428
        return $this->openapiContext;
186✔
429
    }
430

431
    public function withOpenapiContext($openapiContext): static
432
    {
433
        $self = clone $this;
4✔
434
        $self->openapiContext = $openapiContext;
4✔
435

436
        return $self;
4✔
437
    }
438

439
    public function getJsonSchemaContext(): ?array
440
    {
441
        return $this->jsonSchemaContext;
210✔
442
    }
443

444
    public function withJsonSchemaContext($jsonSchemaContext): static
445
    {
446
        $self = clone $this;
14✔
447
        $self->jsonSchemaContext = $jsonSchemaContext;
14✔
448

449
        return $self;
14✔
450
    }
451

452
    public function getPush(): ?bool
453
    {
454
        return $this->push;
72✔
455
    }
456

457
    public function withPush($push): static
458
    {
459
        $self = clone $this;
10✔
460
        $self->push = $push;
10✔
461

462
        return $self;
10✔
463
    }
464

465
    public function getSecurity(): ?string
466
    {
467
        return $this->security instanceof \Stringable ? (string) $this->security : $this->security;
484✔
468
    }
469

470
    public function withSecurity($security): static
471
    {
472
        $self = clone $this;
4✔
473
        $self->security = $security;
4✔
474

475
        return $self;
4✔
476
    }
477

478
    public function getSecurityPostDenormalize(): ?string
479
    {
480
        return $this->securityPostDenormalize instanceof \Stringable ? (string) $this->securityPostDenormalize : $this->securityPostDenormalize;
78✔
481
    }
482

483
    public function withSecurityPostDenormalize($securityPostDenormalize): static
484
    {
485
        $self = clone $this;
4✔
486
        $self->securityPostDenormalize = $securityPostDenormalize;
4✔
487

488
        return $self;
4✔
489
    }
490

491
    public function getTypes(): ?array
492
    {
493
        return $this->types;
172✔
494
    }
495

496
    /**
497
     * @param string[]|string $types
498
     */
499
    public function withTypes(array|string $types = []): static
500
    {
501
        $self = clone $this;
8✔
502
        $self->types = (array) $types;
8✔
503

504
        return $self;
8✔
505
    }
506

507
    /**
508
     * deprecated since 4.2, use "getNativeType" instead.
509
     *
510
     * @return LegacyType[]|null
511
     */
512
    public function getBuiltinTypes(): ?array
513
    {
514
        trigger_deprecation('api-platform/metadata', '4.2', 'The "%s()" method is deprecated, use "%s::getNativeType()" instead.', __METHOD__, self::class);
×
515

516
        return $this->builtinTypes;
×
517
    }
518

519
    /**
520
     * deprecated since 4.2, use "withNativeType" instead.
521
     *
522
     * @param LegacyType[] $builtinTypes
523
     */
524
    public function withBuiltinTypes(array $builtinTypes = []): static
525
    {
526
        trigger_deprecation('api-platform/metadata', '4.2', 'The "%s()" method is deprecated, use "%s::withNativeType()" instead.', __METHOD__, self::class);
×
527

528
        $self = clone $this;
×
529
        $self->builtinTypes = $builtinTypes;
×
530
        $self->nativeType = PropertyInfoToTypeInfoHelper::convertLegacyTypesToType($builtinTypes);
×
531

532
        return $self;
×
533
    }
534

535
    public function getNativeType(): ?Type
536
    {
537
        return $this->nativeType;
544✔
538
    }
539

540
    public function withNativeType(?Type $nativeType): self
541
    {
542
        $self = clone $this;
200✔
543
        $self->nativeType = $nativeType;
200✔
544
        $self->builtinTypes = PropertyInfoToTypeInfoHelper::convertTypeToLegacyTypes($nativeType) ?? [];
200✔
545

546
        return $self;
200✔
547
    }
548

549
    public function getSchema(): ?array
550
    {
551
        return $this->schema;
224✔
552
    }
553

554
    public function withSchema(array $schema = []): static
555
    {
556
        $self = clone $this;
172✔
557
        $self->schema = $schema;
172✔
558

559
        return $self;
172✔
560
    }
561

562
    public function withInitializable(?bool $initializable): static
563
    {
564
        $self = clone $this;
172✔
565
        $self->initializable = $initializable;
172✔
566

567
        return $self;
172✔
568
    }
569

570
    public function isInitializable(): ?bool
571
    {
572
        return $this->initializable;
186✔
573
    }
574

575
    public function getExtraProperties(): array
576
    {
577
        return $this->extraProperties;
232✔
578
    }
579

580
    public function withExtraProperties(array $extraProperties = []): static
581
    {
582
        $self = clone $this;
64✔
583
        $self->extraProperties = $extraProperties;
64✔
584

585
        return $self;
64✔
586
    }
587

588
    /**
589
     * Gets IRI of this property.
590
     *
591
     * @return string[]|null
592
     */
593
    public function getIris()
594
    {
595
        return $this->iris;
92✔
596
    }
597

598
    /**
599
     * Returns a new instance with the given IRI.
600
     *
601
     * @param string|string[] $iris
602
     */
603
    public function withIris(string|array $iris): static
604
    {
605
        $metadata = clone $this;
20✔
606
        $metadata->iris = (array) $iris;
20✔
607

608
        return $metadata;
20✔
609
    }
610

611
    /**
612
     * Whether to generate a skolem iri on anonymous resources.
613
     */
614
    public function getGenId(): ?bool
615
    {
616
        return $this->genId;
266✔
617
    }
618

619
    public function withGenId(bool $genId): static
620
    {
621
        $metadata = clone $this;
8✔
622
        $metadata->genId = $genId;
8✔
623

624
        return $metadata;
8✔
625
    }
626

627
    /**
628
     * Whether to return the subRessource collection IRI instead of an iterable of IRI.
629
     *
630
     * @experimental
631
     */
632
    public function getUriTemplate(): ?string
633
    {
634
        return $this->uriTemplate;
206✔
635
    }
636

637
    public function withUriTemplate(?string $uriTemplate): static
638
    {
639
        $metadata = clone $this;
×
640
        $metadata->uriTemplate = $uriTemplate;
×
641

642
        return $metadata;
×
643
    }
644

645
    public function getPolicy(): ?string
646
    {
647
        return $this->policy;
480✔
648
    }
649

650
    public function withPolicy(?string $policy): static
651
    {
652
        $self = clone $this;
×
653
        $self->policy = $policy;
×
654

655
        return $self;
×
656
    }
657

658
    public function getSerialize(): ?array
659
    {
660
        return $this->serialize;
64✔
661
    }
662

663
    /**
664
     * @param Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth|array<array-key, Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth> $serialize
665
     */
666
    public function withSerialize(array|Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth $serialize): static
667
    {
668
        $self = clone $this;
×
669
        $self->serialize = (array) $serialize;
×
670

671
        return $self;
×
672
    }
673

674
    public function getHydra(): ?bool
675
    {
676
        return $this->hydra;
64✔
677
    }
678

679
    public function withHydra(bool $hydra): static
680
    {
681
        $self = clone $this;
4✔
682
        $self->hydra = $hydra;
4✔
683

684
        return $self;
4✔
685
    }
686
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc