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

api-platform / core / 9710673650

25 Jun 2024 02:01PM UTC coverage: 61.693% (-0.9%) from 62.637%
9710673650

push

github

web-flow
feat(laravel): laravel component (#5882)

* feat(laravel): laravel component

* try to skip laravel

* feat(jsonapi): component

* feat(laravel): json api support (needs review)

* work on relations

* relations (needs toMany) + skolem + IRI to resource

* links handler

* ulid

* validation

* slug post

* remove deprecations

* move classes

* fix tests

* fix tests metadata

* phpstan

* missing class

* fix laravel tests

* fix stan

33 of 77 new or added lines in 20 files covered. (42.86%)

140 existing lines in 15 files now uncovered.

10787 of 17485 relevant lines covered (61.69%)

59.64 hits per line

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

93.64
/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 Symfony\Component\PropertyInfo\Type;
17

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

215
    public function getProperty(): ?string
216
    {
217
        return $this->property;
48✔
218
    }
219

220
    public function withProperty(string $property): self
221
    {
NEW
222
        $self = clone $this;
×
NEW
223
        $self->property = $property;
×
224

NEW
225
        return $self;
×
226
    }
227

228
    public function getDescription(): ?string
229
    {
230
        return $this->description;
172✔
231
    }
232

233
    public function withDescription(string $description): self
234
    {
235
        $self = clone $this;
112✔
236
        $self->description = $description;
112✔
237

238
        return $self;
112✔
239
    }
240

241
    public function isReadable(): ?bool
242
    {
243
        return $this->readable;
400✔
244
    }
245

246
    public function withReadable(bool $readable): self
247
    {
248
        $self = clone $this;
240✔
249
        $self->readable = $readable;
240✔
250

251
        return $self;
240✔
252
    }
253

254
    public function isWritable(): ?bool
255
    {
256
        return $this->writable;
196✔
257
    }
258

259
    public function withWritable(bool $writable): self
260
    {
261
        $self = clone $this;
228✔
262
        $self->writable = $writable;
228✔
263

264
        return $self;
228✔
265
    }
266

267
    public function isReadableLink(): ?bool
268
    {
269
        return $this->readableLink;
264✔
270
    }
271

272
    public function withReadableLink(bool $readableLink): self
273
    {
274
        $self = clone $this;
116✔
275
        $self->readableLink = $readableLink;
116✔
276

277
        return $self;
116✔
278
    }
279

280
    public function isWritableLink(): ?bool
281
    {
282
        return $this->writableLink;
68✔
283
    }
284

285
    public function withWritableLink(bool $writableLink): self
286
    {
287
        $self = clone $this;
112✔
288
        $self->writableLink = $writableLink;
112✔
289

290
        return $self;
112✔
291
    }
292

293
    public function isRequired(): ?bool
294
    {
295
        return $this->required;
416✔
296
    }
297

298
    public function withRequired(bool $required): self
299
    {
300
        $self = clone $this;
328✔
301
        $self->required = $required;
328✔
302

303
        return $self;
328✔
304
    }
305

306
    public function isIdentifier(): ?bool
307
    {
308
        return $this->identifier;
192✔
309
    }
310

311
    public function withIdentifier(bool $identifier): self
312
    {
313
        $self = clone $this;
84✔
314
        $self->identifier = $identifier;
84✔
315

316
        return $self;
84✔
317
    }
318

319
    public function getDefault()
320
    {
321
        return $this->default;
144✔
322
    }
323

324
    public function withDefault($default): self
325
    {
326
        $self = clone $this;
76✔
327
        $self->default = $default;
76✔
328

329
        return $self;
76✔
330
    }
331

332
    public function getExample(): mixed
333
    {
334
        return $this->example;
144✔
335
    }
336

337
    public function withExample(mixed $example): self
338
    {
339
        $self = clone $this;
8✔
340
        $self->example = $example;
8✔
341

342
        return $self;
8✔
343
    }
344

345
    public function getDeprecationReason(): ?string
346
    {
347
        return $this->deprecationReason;
172✔
348
    }
349

350
    public function withDeprecationReason($deprecationReason): self
351
    {
352
        $self = clone $this;
12✔
353
        $self->deprecationReason = $deprecationReason;
12✔
354

355
        return $self;
12✔
356
    }
357

358
    public function isFetchable(): ?bool
359
    {
360
        return $this->fetchable;
48✔
361
    }
362

363
    public function withFetchable($fetchable): self
364
    {
365
        $self = clone $this;
×
366
        $self->fetchable = $fetchable;
×
367

368
        return $self;
×
369
    }
370

371
    public function getFetchEager(): ?bool
372
    {
373
        return $this->fetchEager;
64✔
374
    }
375

376
    public function withFetchEager($fetchEager): self
377
    {
378
        $self = clone $this;
8✔
379
        $self->fetchEager = $fetchEager;
8✔
380

381
        return $self;
8✔
382
    }
383

384
    public function getJsonldContext(): ?array
385
    {
386
        return $this->jsonldContext;
104✔
387
    }
388

389
    public function withJsonldContext($jsonldContext): self
390
    {
391
        $self = clone $this;
32✔
392
        $self->jsonldContext = $jsonldContext;
32✔
393

394
        return $self;
32✔
395
    }
396

397
    public function getOpenapiContext(): ?array
398
    {
399
        return $this->openapiContext;
68✔
400
    }
401

402
    public function withOpenapiContext($openapiContext): self
403
    {
404
        $self = clone $this;
12✔
405
        $self->openapiContext = $openapiContext;
12✔
406

407
        return $self;
12✔
408
    }
409

410
    public function getJsonSchemaContext(): ?array
411
    {
412
        return $this->jsonSchemaContext;
160✔
413
    }
414

415
    public function withJsonSchemaContext($jsonSchemaContext): self
416
    {
417
        $self = clone $this;
20✔
418
        $self->jsonSchemaContext = $jsonSchemaContext;
20✔
419

420
        return $self;
20✔
421
    }
422

423
    public function getPush(): ?bool
424
    {
425
        return $this->push;
56✔
426
    }
427

428
    public function withPush($push): self
429
    {
430
        $self = clone $this;
16✔
431
        $self->push = $push;
16✔
432

433
        return $self;
16✔
434
    }
435

436
    public function getSecurity(): ?string
437
    {
438
        return $this->security instanceof \Stringable ? (string) $this->security : $this->security;
312✔
439
    }
440

441
    public function withSecurity($security): self
442
    {
443
        $self = clone $this;
8✔
444
        $self->security = $security;
8✔
445

446
        return $self;
8✔
447
    }
448

449
    public function getSecurityPostDenormalize(): ?string
450
    {
451
        return $this->securityPostDenormalize instanceof \Stringable ? (string) $this->securityPostDenormalize : $this->securityPostDenormalize;
60✔
452
    }
453

454
    public function withSecurityPostDenormalize($securityPostDenormalize): self
455
    {
456
        $self = clone $this;
8✔
457
        $self->securityPostDenormalize = $securityPostDenormalize;
8✔
458

459
        return $self;
8✔
460
    }
461

462
    public function getTypes(): ?array
463
    {
464
        return $this->types;
328✔
465
    }
466

467
    /**
468
     * @param string[]|string $types
469
     */
470
    public function withTypes(array|string $types = []): self
471
    {
472
        $self = clone $this;
40✔
473
        $self->types = (array) $types;
40✔
474

475
        return $self;
40✔
476
    }
477

478
    /**
479
     * @return Type[]
480
     */
481
    public function getBuiltinTypes(): ?array
482
    {
483
        return $this->builtinTypes;
668✔
484
    }
485

486
    /**
487
     * @param Type[] $builtinTypes
488
     */
489
    public function withBuiltinTypes(array $builtinTypes = []): self
490
    {
491
        $self = clone $this;
268✔
492
        $self->builtinTypes = $builtinTypes;
268✔
493

494
        return $self;
268✔
495
    }
496

497
    public function getSchema(): ?array
498
    {
499
        return $this->schema;
448✔
500
    }
501

502
    public function withSchema(array $schema = []): self
503
    {
504
        $self = clone $this;
296✔
505
        $self->schema = $schema;
296✔
506

507
        return $self;
296✔
508
    }
509

510
    public function withInitializable(?bool $initializable): self
511
    {
512
        $self = clone $this;
144✔
513
        $self->initializable = $initializable;
144✔
514

515
        return $self;
144✔
516
    }
517

518
    public function isInitializable(): ?bool
519
    {
520
        return $this->initializable;
160✔
521
    }
522

523
    public function getExtraProperties(): ?array
524
    {
525
        return $this->extraProperties;
568✔
526
    }
527

528
    public function withExtraProperties(array $extraProperties = []): self
529
    {
530
        $self = clone $this;
68✔
531
        $self->extraProperties = $extraProperties;
68✔
532

533
        return $self;
68✔
534
    }
535

536
    /**
537
     * Gets IRI of this property.
538
     */
539
    public function getIris()
540
    {
541
        return $this->iris;
104✔
542
    }
543

544
    /**
545
     * Returns a new instance with the given IRI.
546
     *
547
     * @param string|string[] $iris
548
     */
549
    public function withIris(string|array $iris): self
550
    {
551
        $metadata = clone $this;
24✔
552
        $metadata->iris = (array) $iris;
24✔
553

554
        return $metadata;
24✔
555
    }
556

557
    /**
558
     * Whether to generate a skolem iri on anonymous resources.
559
     */
560
    public function getGenId()
561
    {
562
        return $this->genId;
88✔
563
    }
564

565
    public function withGenId(bool $genId): self
566
    {
567
        $metadata = clone $this;
12✔
568
        $metadata->genId = $genId;
12✔
569

570
        return $metadata;
12✔
571
    }
572

573
    /**
574
     * Whether to return the subRessource collection IRI instead of an iterable of IRI.
575
     *
576
     * @experimental
577
     */
578
    public function getUriTemplate(): ?string
579
    {
580
        return $this->uriTemplate;
164✔
581
    }
582

583
    public function withUriTemplate(?string $uriTemplate): self
584
    {
585
        $metadata = clone $this;
8✔
586
        $metadata->uriTemplate = $uriTemplate;
8✔
587

588
        return $metadata;
8✔
589
    }
590
}
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