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

api-platform / core / 6148660584

11 Sep 2023 03:40PM UTC coverage: 37.077% (-0.1%) from 37.185%
6148660584

push

github

soyuka
chore(symfony): security after validate when validator installed

10090 of 27214 relevant lines covered (37.08%)

19.39 hits per line

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

90.57
/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)]
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|null $security                https://api-platform.com/docs/core/security
39
     * @param string|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
     */
45
    public function __construct(
46
        private ?string $description = null,
47
        private ?bool $readable = null,
48
        private ?bool $writable = null,
49
        private ?bool $readableLink = null,
50
        private ?bool $writableLink = null,
51
        private ?bool $required = null,
52
        private ?bool $identifier = null,
53
        private $default = null,
54
        private mixed $example = null,
55
        /**
56
         * The `deprecationReason` option deprecates the current operation with a deprecation message.
57
         *
58
         * <CodeSelector>
59
         * ```php
60
         * <?php
61
         * // api/src/Entity/Review.php
62
         * use ApiPlatform\Metadata\ApiProperty;
63
         * use ApiPlatform\Metadata\ApiResource;
64
         *
65
         * #[ApiResource]
66
         * class Review
67
         * {
68
         *     #[ApiProperty(deprecationReason: "Use the rating property instead")]
69
         *     public string $letter;
70
         * }
71
         * ```
72
         *
73
         * ```yaml
74
         * # api/config/api_platform/properties.yaml
75
         * properties:
76
         *     App\Entity\Review:
77
         *         letter:
78
         *             deprecationReason: 'Create a Book instead'
79
         * ```
80
         *
81
         * ```xml
82
         * <?xml version="1.0" encoding="UTF-8" ?>
83
         * <!-- api/config/api_platform/properties.xml -->
84
         *
85
         * <properties
86
         *         xmlns="https://api-platform.com/schema/metadata/properties-3.0"
87
         *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
88
         *         xsi:schemaLocation="https://api-platform.com/schema/metadata/properties-3.0
89
         *         https://api-platform.com/schema/metadata/properties-3.0.xsd">
90
         *     <property resource="App\Entity\Review" name="letter" deprecationReason="Create a Book instead" />
91
         * </properties>
92
         * ```
93
         * </CodeSelector>
94
         *
95
         * - 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
96
         * - With Swagger / OpenAPI, [a `deprecated` property](https://swagger.io/docs/specification/2-0/paths-and-operations/) will be added
97
         * - With GraphQL, the [`isDeprecated` and `deprecationReason` properties](https://facebook.github.io/graphql/June2018/#sec-Deprecation) will be added to the schema
98
         */
99
        private ?string $deprecationReason = null,
100
        private ?bool $fetchable = null,
101
        private ?bool $fetchEager = null,
102
        private ?array $jsonldContext = null,
103
        private ?array $openapiContext = null,
104
        private ?array $jsonSchemaContext = null,
105
        private ?bool $push = null,
106
        private ?string $security = null,
107
        private ?string $securityPostDenormalize = null,
108
        private array|string|null $types = null,
109
        /*
110
         * The related php types.
111
         */
112
        private ?array $builtinTypes = null,
113
        private ?array $schema = null,
114
        private ?bool $initializable = null,
115
        private $iris = null,
116
        private ?bool $genId = null,
117
        private ?string $uriTemplate = null,
118
        private array $extraProperties = [],
119
    ) {
120
        if (\is_string($types)) {
282✔
121
            $this->types = (array) $types;
×
122
        }
123
    }
124

125
    public function getDescription(): ?string
126
    {
127
        return $this->description;
30✔
128
    }
129

130
    public function withDescription(string $description): self
131
    {
132
        $self = clone $this;
72✔
133
        $self->description = $description;
72✔
134

135
        return $self;
72✔
136
    }
137

138
    public function isReadable(): ?bool
139
    {
140
        return $this->readable;
117✔
141
    }
142

143
    public function withReadable(bool $readable): self
144
    {
145
        $self = clone $this;
132✔
146
        $self->readable = $readable;
132✔
147

148
        return $self;
132✔
149
    }
150

151
    public function isWritable(): ?bool
152
    {
153
        return $this->writable;
72✔
154
    }
155

156
    public function withWritable(bool $writable): self
157
    {
158
        $self = clone $this;
105✔
159
        $self->writable = $writable;
105✔
160

161
        return $self;
105✔
162
    }
163

164
    public function isReadableLink(): ?bool
165
    {
166
        return $this->readableLink;
96✔
167
    }
168

169
    public function withReadableLink(bool $readableLink): self
170
    {
171
        $self = clone $this;
111✔
172
        $self->readableLink = $readableLink;
111✔
173

174
        return $self;
111✔
175
    }
176

177
    public function isWritableLink(): ?bool
178
    {
179
        return $this->writableLink;
9✔
180
    }
181

182
    public function withWritableLink(bool $writableLink): self
183
    {
184
        $self = clone $this;
60✔
185
        $self->writableLink = $writableLink;
60✔
186

187
        return $self;
60✔
188
    }
189

190
    public function isRequired(): ?bool
191
    {
192
        return $this->required;
189✔
193
    }
194

195
    public function withRequired(bool $required): self
196
    {
197
        $self = clone $this;
162✔
198
        $self->required = $required;
162✔
199

200
        return $self;
162✔
201
    }
202

203
    public function isIdentifier(): ?bool
204
    {
205
        return $this->identifier;
96✔
206
    }
207

208
    public function withIdentifier(bool $identifier): self
209
    {
210
        $self = clone $this;
48✔
211
        $self->identifier = $identifier;
48✔
212

213
        return $self;
48✔
214
    }
215

216
    public function getDefault()
217
    {
218
        return $this->default;
27✔
219
    }
220

221
    public function withDefault($default): self
222
    {
223
        $self = clone $this;
18✔
224
        $self->default = $default;
18✔
225

226
        return $self;
18✔
227
    }
228

229
    public function getExample(): mixed
230
    {
231
        return $this->example;
24✔
232
    }
233

234
    public function withExample(mixed $example): self
235
    {
236
        $self = clone $this;
×
237
        $self->example = $example;
×
238

239
        return $self;
×
240
    }
241

242
    public function getDeprecationReason(): ?string
243
    {
244
        return $this->deprecationReason;
30✔
245
    }
246

247
    public function withDeprecationReason($deprecationReason): self
248
    {
249
        $self = clone $this;
3✔
250
        $self->deprecationReason = $deprecationReason;
3✔
251

252
        return $self;
3✔
253
    }
254

255
    public function isFetchable(): ?bool
256
    {
257
        return $this->fetchable;
24✔
258
    }
259

260
    public function withFetchable($fetchable): self
261
    {
262
        $self = clone $this;
×
263
        $self->fetchable = $fetchable;
×
264

265
        return $self;
×
266
    }
267

268
    public function getFetchEager(): ?bool
269
    {
270
        return $this->fetchEager;
51✔
271
    }
272

273
    public function withFetchEager($fetchEager): self
274
    {
275
        $self = clone $this;
12✔
276
        $self->fetchEager = $fetchEager;
12✔
277

278
        return $self;
12✔
279
    }
280

281
    public function getJsonldContext(): ?array
282
    {
283
        return $this->jsonldContext;
45✔
284
    }
285

286
    public function withJsonldContext($jsonldContext): self
287
    {
288
        $self = clone $this;
15✔
289
        $self->jsonldContext = $jsonldContext;
15✔
290

291
        return $self;
15✔
292
    }
293

294
    public function getOpenapiContext(): ?array
295
    {
296
        return $this->openapiContext;
12✔
297
    }
298

299
    public function withOpenapiContext($openapiContext): self
300
    {
301
        $self = clone $this;
×
302
        $self->openapiContext = $openapiContext;
×
303

304
        return $self;
×
305
    }
306

307
    public function getJsonSchemaContext(): ?array
308
    {
309
        return $this->jsonSchemaContext;
36✔
310
    }
311

312
    public function withJsonSchemaContext($jsonSchemaContext): self
313
    {
314
        $self = clone $this;
3✔
315
        $self->jsonSchemaContext = $jsonSchemaContext;
3✔
316

317
        return $self;
3✔
318
    }
319

320
    public function getPush(): ?bool
321
    {
322
        return $this->push;
12✔
323
    }
324

325
    public function withPush($push): self
326
    {
327
        $self = clone $this;
3✔
328
        $self->push = $push;
3✔
329

330
        return $self;
3✔
331
    }
332

333
    public function getSecurity(): ?string
334
    {
335
        return $this->security;
60✔
336
    }
337

338
    public function withSecurity($security): self
339
    {
340
        $self = clone $this;
3✔
341
        $self->security = $security;
3✔
342

343
        return $self;
3✔
344
    }
345

346
    public function getSecurityPostDenormalize(): ?string
347
    {
348
        return $this->securityPostDenormalize;
18✔
349
    }
350

351
    public function withSecurityPostDenormalize($securityPostDenormalize): self
352
    {
353
        $self = clone $this;
3✔
354
        $self->securityPostDenormalize = $securityPostDenormalize;
3✔
355

356
        return $self;
3✔
357
    }
358

359
    public function getTypes(): ?array
360
    {
361
        return $this->types;
162✔
362
    }
363

364
    /**
365
     * @param string[]|string $types
366
     */
367
    public function withTypes(array|string $types = []): self
368
    {
369
        $self = clone $this;
21✔
370
        $self->types = (array) $types;
21✔
371

372
        return $self;
21✔
373
    }
374

375
    /**
376
     * @return Type[]
377
     */
378
    public function getBuiltinTypes(): ?array
379
    {
380
        return $this->builtinTypes;
348✔
381
    }
382

383
    /**
384
     * @param Type[] $builtinTypes
385
     */
386
    public function withBuiltinTypes(array $builtinTypes = []): self
387
    {
388
        $self = clone $this;
129✔
389
        $self->builtinTypes = $builtinTypes;
129✔
390

391
        return $self;
129✔
392
    }
393

394
    public function getSchema(): ?array
395
    {
396
        return $this->schema;
216✔
397
    }
398

399
    public function withSchema(array $schema = []): self
400
    {
401
        $self = clone $this;
138✔
402
        $self->schema = $schema;
138✔
403

404
        return $self;
138✔
405
    }
406

407
    public function withInitializable(?bool $initializable): self
408
    {
409
        $self = clone $this;
24✔
410
        $self->initializable = $initializable;
24✔
411

412
        return $self;
24✔
413
    }
414

415
    public function isInitializable(): ?bool
416
    {
417
        return $this->initializable;
36✔
418
    }
419

420
    public function getExtraProperties(): ?array
421
    {
422
        return $this->extraProperties;
6✔
423
    }
424

425
    public function withExtraProperties(array $extraProperties = []): self
426
    {
427
        $self = clone $this;
6✔
428
        $self->extraProperties = $extraProperties;
6✔
429

430
        return $self;
6✔
431
    }
432

433
    /**
434
     * Gets IRI of this property.
435
     */
436
    public function getIris()
437
    {
438
        return $this->iris;
45✔
439
    }
440

441
    /**
442
     * Returns a new instance with the given IRI.
443
     *
444
     * @param string|string[] $iris
445
     */
446
    public function withIris(string|array $iris): self
447
    {
448
        $metadata = clone $this;
9✔
449
        $metadata->iris = (array) $iris;
9✔
450

451
        return $metadata;
9✔
452
    }
453

454
    /**
455
     * Whether to generate a skolem iri on anonymous resources.
456
     */
457
    public function getGenId()
458
    {
459
        return $this->genId;
18✔
460
    }
461

462
    public function withGenId(bool $genId): self
463
    {
464
        $metadata = clone $this;
6✔
465
        $metadata->genId = $genId;
6✔
466

467
        return $metadata;
6✔
468
    }
469

470
    /**
471
     * Whether to return the subRessource collection IRI instead of an iterable of IRI.
472
     *
473
     * @experimental
474
     */
475
    public function getUriTemplate(): ?string
476
    {
477
        return $this->uriTemplate;
78✔
478
    }
479

480
    public function withUriTemplate(?string $uriTemplate): self
481
    {
482
        $metadata = clone $this;
6✔
483
        $metadata->uriTemplate = $uriTemplate;
6✔
484

485
        return $metadata;
6✔
486
    }
487
}
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

© 2026 Coveralls, Inc