Skip to content

Commit 29bffd0

Browse files
Catgolindunglas
andauthored
Change annotations to attributes (#1428)
* docs: Change annotations to attribute in core and admin pages * doc: fix typos and brackets Apply suggestions from code review Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> * doc: removed attribute key * docs: Change annotations to attribute in core and admin pages * doc: fix typos and brackets Apply suggestions from code review Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> * doc: removed attribute key Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
1 parent f2948d4 commit 29bffd0

24 files changed

+296
-413
lines changed

admin/performance.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ use Doctrine\ORM\Mapping as ORM;
2828
/**
2929
* @ORM\Entity
3030
*/
31-
#[ApiResource]
31+
#[ApiResource]
3232
class Author
3333
{
3434
/**
3535
* @ORM\Column(type="integer")
3636
* @ORM\GeneratedValue
3737
* @ORM\Id
38-
* @ApiFilter(SearchFilter::class, strategy="exact")
3938
*/
39+
#[ApiFilter(SearchFilter::class, strategy: "exact")]
4040
public $id;
4141

4242
/**

admin/schema.org.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ To configure which property should be shown to represent your entity, map the pr
1717
```php
1818
// api/src/Entity/Person.php
1919

20-
/**
21-
* @ApiProperty(iri="http://schema.org/name")
22-
*/
20+
#[ApiProperty(iri="http://schema.org/name")]
2321
private $name;
2422
```
2523

admin/validation.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,10 @@ For instance, with API Platform Core as backend, if you write the following:
1616
use ApiPlatform\Core\Annotation\ApiResource;
1717
use Symfony\Component\Validator\Constraints as Assert;
1818

19-
/**
20-
* @ApiResource
21-
*/
19+
#[ApiResource]
2220
class Book
2321
{
24-
/**
25-
* @Assert\NotBlank
26-
*/
22+
#[Assert\NotBlank]
2723
public ?string $title = null;
2824
}
2925
```
@@ -51,14 +47,10 @@ For example if you have this code:
5147
use ApiPlatform\Core\Annotation\ApiResource;
5248
use Symfony\Component\Validator\Constraints as Assert;
5349

54-
/**
55-
* @ApiResource
56-
*/
50+
#[ApiResource]
5751
class Book
5852
{
59-
/**
60-
* @Assert\Isbn
61-
*/
53+
#[Assert\Isbn]
6254
public ?string $isbn = null;
6355
}
6456
```

core/data-providers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Both implementations can also implement a third, optional, interface called
2323
if you want to limit their effects to a single resource or operation.
2424

2525
In the following examples we will create custom data providers for an entity class called `App\Entity\BlogPost`.
26-
Note, that if your entity is not Doctrine-related, you need to flag the identifier property by using `@ApiProperty(identifier=true)` for things to work properly (see also [Entity Identifier Case](serialization.md#entity-identifier-case)).
26+
Note, that if your entity is not Doctrine-related, you need to flag the identifier property by using `#[ApiProperty(identifier: true)` for things to work properly (see also [Entity Identifier Case](serialization.md#entity-identifier-case)).
2727

2828
## Custom Collection Data Provider
2929

core/default-order.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ namespace App\Entity;
1414

1515
use ApiPlatform\Core\Annotation\ApiResource;
1616

17-
/**
18-
* @ApiResource(attributes={"order"={"foo": "ASC"}})
19-
*/
17+
#[ApiResource(order: ["foo" => "ASC"])]
2018
class Book
2119
{
2220
// ...
@@ -52,8 +50,7 @@ namespace App\Entity;
5250
5351
use ApiPlatform\Core\Annotation\ApiResource;
5452
55-
/**
56-
* @ApiResource(attributes={"order"={"foo", "bar"}})
53+
#[ApiResource(order: ["foo", "bar"])]
5754
*/
5855
class Book
5956
{
@@ -93,9 +90,7 @@ namespace App\Entity;
9390
9491
use ApiPlatform\Core\Annotation\ApiResource;
9592
96-
/**
97-
* @ApiResource(attributes={"order"={"author.username"}})
98-
*/
93+
#[ApiResource(order: ["author.username"])]
9994
class Book
10095
{
10196
// ...
@@ -123,13 +118,21 @@ Another possibility is to apply the default order for a specific collection oper
123118
[codeSelector]
124119

125120
```php
126-
/**
127-
* collectionOperations={
128-
* "get",
129-
* "get_desc_custom"={"method"="GET", "path"="custom_collection_desc_foos", "order"={"name"="DESC"}},
130-
* "get_asc_custom"={"method"="GET", "path"="custom_collection_asc_foos", "order"={ "name"="ASC"}},
131-
* }
132-
*/
121+
#[ApiResource(
122+
collectionOperations: [
123+
"get",
124+
"get_desc_custom" => [
125+
"method" => "GET",
126+
"path" => "custom_collection_desc_foos",
127+
"order" => ["name" => "DESC"]
128+
],
129+
"get_asc_custom" => [
130+
"method" => "GET",
131+
"path" => "custom_collection_asc_foos",
132+
"order" => ["name" => "ASC"]
133+
],
134+
]
135+
)]
133136
class Book
134137
{
135138
// ...

core/deprecations.md

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ namespace App\Entity;
2727

2828
use ApiPlatform\Core\Annotation\ApiResource;
2929

30-
/**
31-
* @ApiResource(deprecationReason="Create a Book instead")
32-
*/
30+
#[ApiResource(deprecationReason: "Create a Book instead")]
3331
class Parchment
3432
{
3533
// ...
@@ -60,11 +58,7 @@ namespace App\Entity;
6058

6159
use ApiPlatform\Core\Annotation\ApiResource;
6260

63-
/**
64-
* @ApiResource(itemOperations={
65-
* "get"={"deprecation_reason"="Retrieve a Book instead"}
66-
* })
67-
*/
61+
#[ApiResource(itemOperations: [ "get" => ["deprecation_reason" => "Retrieve a Book instead"])]
6862
class Parchment
6963
{
7064
// ...
@@ -84,16 +78,12 @@ namespace App\Entity;
8478
use ApiPlatform\Core\Annotation\ApiProperty;
8579
use ApiPlatform\Core\Annotation\ApiResource;
8680

87-
/**
88-
* @ApiResource
89-
*/
81+
#[ApiResource]
9082
class Review
9183
{
9284
// ...
9385

94-
/**
95-
* @ApiProperty(deprecationReason="Use the rating property instead")
96-
*/
86+
#[ApiProperty(deprecationReason: "Use the rating property instead")]
9787
public $letter;
9888

9989
// ...
@@ -133,12 +123,10 @@ namespace App\Entity;
133123
134124
use ApiPlatform\Core\Annotation\ApiResource;
135125
136-
/**
137-
* @ApiResource(
138-
* deprecationReason="Create a Book instead",
139-
* sunset="01/01/2020"
140-
* )
141-
*/
126+
#[ApiResource(
127+
deprecationReason: "Create a Book instead",
128+
sunset: "01/01/2020"
129+
)]
142130
class Parchment
143131
{
144132
// ...
@@ -158,14 +146,12 @@ namespace App\Entity;
158146
159147
use ApiPlatform\Core\Annotation\ApiResource;
160148
161-
/**
162-
* @ApiResource(itemOperations={
163-
* "get"={
164-
* "deprecation_reason"="Retrieve a Book instead",
165-
* "sunset"="01/01/2020"
166-
* }
167-
* })
168-
*/
149+
#[ApiResource(itemOperations: [
150+
"get" => [
151+
"deprecation_reason" => "Retrieve a Book instead",
152+
"sunset" => "01/01/2020"
153+
]
154+
])]
169155
class Parchment
170156
{
171157
// ...

core/dto.md

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Using Data Transfer Objects (DTOs)
22

3-
As stated in [the general design considerations](design.md), in most cases [the DTO pattern](https://en.wikipedia.org/wiki/Data_transfer_object) should be implemented using an API Resource class representing the public data model exposed through the API and [a custom data provider](data-providers.md). In such cases, the class marked with `@ApiResource` will act as a DTO.
3+
As stated in [the general design considerations](design.md), in most cases [the DTO pattern](https://en.wikipedia.org/wiki/Data_transfer_object) should be implemented using an API Resource class representing the public data model exposed through the API and [a custom data provider](data-providers.md). In such cases, the class marked with `#[ApiResource]` will act as a DTO.
44

55
However, it's sometimes useful to use a specific class to represent the input or output data structure related to an operation.
66

@@ -21,12 +21,7 @@ use ApiPlatform\Core\Annotation\ApiResource;
2121
use App\Dto\BookInput;
2222
use App\Dto\BookOutput;
2323

24-
/**
25-
* @ApiResource(
26-
* input=BookInput::class,
27-
* output=BookOutput::class
28-
* )
29-
*/
24+
#[ApiResource(input: BookInput::class, output: BookOutput::class)]
3025
final class Book
3126
{
3227
public $id;
@@ -375,24 +370,22 @@ use App\Dto\BookOutput;
375370
use App\Dto\CreateBook;
376371
use App\Dto\UpdateBook;
377372

378-
/**
379-
* @ApiResource(
380-
* collectionOperations={
381-
* "create"={
382-
* "method"="POST",
383-
* "input"=CreateBook::class,
384-
* "output"=BookOutput::class
385-
* }
386-
* },
387-
* itemOperations={
388-
* "update"={
389-
* "method"="PUT",
390-
* "input"=UpdateBook::class,
391-
* "output"=BookOutput::class
392-
* }
393-
* }
394-
* )
395-
*/
373+
#[ApiResource(
374+
collectionOperations: [
375+
"create" => [
376+
"method" => "POST",
377+
"input" => CreateBook::class,
378+
"output" => BookOutput::class
379+
],
380+
],
381+
itemOperations: [
382+
"update" => [
383+
"method" => "PUT",
384+
"input" => UpdateBook::class,
385+
"output" => BookOutput::class,
386+
],
387+
],
388+
)]
396389
final class Book
397390
{
398391
}
@@ -464,9 +457,7 @@ namespace App\Entity;
464457
use ApiPlatform\Core\Annotation\ApiResource;
465458
use App\Model\Attribute;
466459

467-
/**
468-
* @ApiResource
469-
*/
460+
#[ApiResource]
470461
final class Book
471462
{
472463
/**

core/extending-jsonld-context.md

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,24 @@ namespace App\Entity;
1717
use ApiPlatform\Core\Annotation\ApiProperty;
1818
use ApiPlatform\Core\Annotation\ApiResource;
1919

20-
/**
21-
* @ApiResource(iri="http://schema.org/Book")
22-
*/
20+
#[ApiResource(iri: "http://schema.org/Book")]
2321
class Book
2422
{
2523
// ...
2624

27-
/**
28-
* ...
29-
* @ApiProperty(
30-
* iri="http://schema.org/name",
31-
* attributes={
32-
* "jsonld_context"={
33-
* "@id"="http://yourcustomid.com",
34-
* "@type"="http://www.w3.org/2001/XMLSchema#string",
35-
* "someProperty"={
36-
* "a"="textA",
37-
* "b"="textB"
38-
* }
39-
* }
40-
* }
41-
* )
42-
*/
25+
#[ApiProperty(
26+
iri: "http://schema.org/name",
27+
attributes: [
28+
"jsonld_context" => [
29+
"@id" => "http://yourcustomid.com",
30+
"@type" => "http://www.w3.org/2001/XMLSchema#string",
31+
"someProperty" => [
32+
"a" => "textA",
33+
"b" => "textB"
34+
]
35+
]
36+
]
37+
)]
4338
public $name;
4439

4540
// ...
@@ -83,12 +78,9 @@ It's also possible to replace the Hydra context used by the documentation genera
8378

8479
use ApiPlatform\Core\Annotation\ApiResource;
8580

86-
/**
87-
* ...
88-
* @ApiResource(itemOperations={
89-
* "get"={"hydra_context"={"foo"="bar"}}
90-
* })
91-
*/
81+
#[ApiResource(itemOperations: [
82+
"get" => ["hydra_context" => ["foo" => "bar"]]
83+
])]
9284
class Book
9385
{
9486
//...

core/extensions.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ namespace App\Entity;
2828

2929
use ApiPlatform\Core\Annotation\ApiResource;
3030

31-
/**
32-
* @ApiResource
33-
*/
31+
#[ApiResource]
3432
class User
3533
{
3634
// ...
@@ -45,9 +43,7 @@ namespace App\Entity;
4543

4644
use ApiPlatform\Core\Annotation\ApiResource;
4745

48-
/**
49-
* @ApiResource
50-
*/
46+
#[ApiResource]
5147
class Offer
5248
{
5349
/**

core/external-vocabularies.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,12 @@ namespace App\Entity;
1414
use ApiPlatform\Core\Annotation\ApiProperty;
1515
use ApiPlatform\Core\Annotation\ApiResource;
1616

17-
/**
18-
* @ApiResource(iri="http://schema.org/Book")
19-
*/
17+
#[ApiResource(iri: "http://schema.org/Book")]
2018
class Book
2119
{
2220
// ...
2321

24-
/**
25-
* ...
26-
* @ApiProperty(iri="http://schema.org/name")
27-
*/
22+
#[ApiProperty(iri: "http://schema.org/name")]
2823
public $name;
2924

3025
// ...

0 commit comments

Comments
 (0)