Skip to content

Commit cd8dabb

Browse files
authored
Update operations.md - PHP 8.0 (#1245)
1 parent 733c8fb commit cd8dabb

File tree

1 file changed

+103
-129
lines changed

1 file changed

+103
-129
lines changed

core/operations.md

Lines changed: 103 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,10 @@ namespace App\Entity;
7070

7171
use ApiPlatform\Core\Annotation\ApiResource;
7272

73-
/**
74-
* ...
75-
* @ApiResource(
76-
* collectionOperations={"get"},
77-
* itemOperations={"get"}
78-
* )
79-
*/
73+
#[ApiResource(
74+
collectionOperations: ['get'],
75+
itemOperations: ['get'],
76+
)]
8077
class Book
8178
{
8279
// ...
@@ -122,13 +119,14 @@ namespace App\Entity;
122119

123120
use ApiPlatform\Core\Annotation\ApiResource;
124121

125-
/**
126-
* ...
127-
* @ApiResource(
128-
* collectionOperations={"get"={"method"="GET"}},
129-
* itemOperations={"get"={"method"="GET"}}
130-
* )
131-
*/
122+
#[ApiResource(
123+
collectionOperations: [
124+
'get' => ['method' => 'get'],
125+
],
126+
itemOperations: [
127+
'get' => ['method' => 'get'],
128+
],
129+
)]
132130
class Book
133131
{
134132
// ...
@@ -149,23 +147,21 @@ namespace App\Entity;
149147
use ApiPlatform\Core\Action\NotFoundAction;
150148
use ApiPlatform\Core\Annotation\ApiResource;
151149

152-
/**
153-
* @ApiResource(
154-
* collectionOperations={
155-
* "get",
156-
* },
157-
* itemOperations={
158-
* "get"={
159-
* "controller"=NotFoundAction::class,
160-
* "read"=false,
161-
* "output"=false,
162-
* },
163-
* },
164-
* )
165-
*/
166-
class Book
167-
{
168-
}
150+
#[ApiResource(
151+
collectionOperations: [
152+
'get' => ['method' => 'get'],
153+
],
154+
itemOperations: [
155+
'get' => [
156+
'controller' => NotFoundAction::class,
157+
'read' => false,
158+
'output' => false,
159+
],
160+
],
161+
)]
162+
class Book
163+
{
164+
}
169165
```
170166

171167
## Configuring Operations
@@ -184,24 +180,24 @@ namespace App\Entity;
184180

185181
use ApiPlatform\Core\Annotation\ApiResource;
186182

187-
/**
188-
* ...
189-
* @ApiResource(
190-
* collectionOperations={
191-
* "post"={"path"="/grimoire", "status"=301}
192-
* },
193-
* itemOperations={
194-
* "get"={
195-
* "path"="/grimoire/{id}",
196-
* "requirements"={"id"="\d+"},
197-
* "defaults"={"color"="brown"},
198-
* "options"={"my_option"="my_option_value"},
199-
* "schemes"={"https"},
200-
* "host"="{subdomain}.api-platform.com"
201-
* }
202-
* }
203-
* )
204-
*/
183+
#[ApiResource(
184+
collectionOperations: [
185+
'post' => [
186+
'path' => '/grimoire',
187+
'status' => 301,
188+
],
189+
],
190+
itemOperations: [
191+
'get' => [
192+
'path' => '/grimoire/{id}',
193+
'requirements' => ['id' => '\d+'],
194+
'defaults' => ['color' => 'brown'],
195+
'options' => ['my_option' => 'my_option_value'],
196+
'schemes' => ['https'],
197+
'host' => '{subdomain}.api-platform.com',
198+
],
199+
],
200+
)]
205201
class Book
206202
{
207203
//...
@@ -284,9 +280,7 @@ namespace App\Entity;
284280

285281
use ApiPlatform\Core\Annotation\ApiResource;
286282

287-
/**
288-
* @ApiResource(routePrefix="/library")
289-
*/
283+
#[ApiResource(routePrefix: '/library')]
290284
class Book
291285
{
292286
//...
@@ -340,24 +334,19 @@ use Symfony\Component\Routing\Annotation\Route;
340334

341335
class CreateBookPublication
342336
{
343-
private $bookPublishingHandler;
344-
345-
public function __construct(BookPublishingHandler $bookPublishingHandler)
346-
{
347-
$this->bookPublishingHandler = $bookPublishingHandler;
348-
}
349-
350-
/**
351-
* @Route(
352-
* name="book_post_publication",
353-
* path="/books/{id}/publication",
354-
* methods={"POST"},
355-
* defaults={
356-
* "_api_resource_class"=Book::class,
357-
* "_api_item_operation_name"="post_publication"
358-
* }
359-
* )
360-
*/
337+
public function __construct(
338+
private BookPublishingHandler $bookPublishingHandler
339+
) {}
340+
341+
#[Route(
342+
path: '/books/{id}/publication',
343+
name: 'book_post_publication',
344+
defaults: [
345+
'_api_resource_class' => Book::class,
346+
'_api_item_operation_name' => 'post_publication',
347+
],
348+
methods: ['POST'],
349+
)]
361350
public function __invoke(Book $data): Book
362351
{
363352
$this->bookPublishingHandler->handle($data);
@@ -424,34 +413,26 @@ use Doctrine\ORM\Mapping as ORM;
424413
class Place
425414
{
426415
/**
427-
* @var int
428-
*
429-
* @ORM\Id
430-
* @ORM\GeneratedValue
431-
* @ORM\Column(type="integer")
432-
*/
433-
private $id;
416+
* @ORM\Id
417+
* @ORM\GeneratedValue
418+
* @ORM\Column(type="integer")
419+
*/
420+
private ?int $id = null;
434421

435422
/**
436-
* @var string
437-
*
438-
* @ORM\Column
439-
*/
440-
private $name;
423+
* @ORM\Column
424+
*/
425+
private string $name = '';
441426

442427
/**
443-
* @var float
444-
*
445-
* @ORM\Column(type="float")
446-
*/
447-
private $latitude;
428+
* @ORM\Column(type="float")
429+
*/
430+
private float $latitude = 0;
448431

449432
/**
450-
* @var float
451-
*
452-
* @ORM\Column(type="float")
453-
*/
454-
private $longitude;
433+
* @ORM\Column(type="float")
434+
*/
435+
private float $longitude = 0;
455436

456437
// ...
457438
}
@@ -465,15 +446,9 @@ namespace App\Entity;
465446

466447
class Weather
467448
{
468-
/**
469-
* @var float
470-
*/
471-
private $temperature;
449+
private float $temperature;
472450

473-
/**
474-
* @var float
475-
*/
476-
private $pressure;
451+
private float $pressure;
477452

478453
// ...
479454
}
@@ -495,19 +470,23 @@ use Doctrine\ORM\Mapping as ORM;
495470

496471
/**
497472
* @ORM\Entity
498-
*
499-
* @ApiResource(
500-
* itemOperations={
501-
* "get",
502-
* "put",
503-
* "delete",
504-
* "get_weather": {
505-
* "method": "GET",
506-
* "path": "/places/{id}/weather",
507-
* "controller": GetWeather::class
508-
* }
509-
* }, collectionOperations={"get", "post"})
510473
*/
474+
#[ApiResource(
475+
collectionOperations: [
476+
'get',
477+
'post',
478+
],
479+
itemOperations: [
480+
'get',
481+
'put',
482+
'delete',
483+
'get_weather' => [
484+
'method' => 'GET',
485+
'path' => '/places/{id}/weather',
486+
'controller' => GetWeather::class,
487+
],
488+
],
489+
)]
511490
class Place
512491
{
513492
// ...
@@ -525,9 +504,7 @@ namespace App\Entity;
525504

526505
use ApiPlatform\Core\Annotation\ApiResource;
527506

528-
/**
529-
* @ApiResource
530-
*/
507+
#[ApiResource]
531508
class Weather
532509
{
533510
// ...
@@ -545,14 +522,14 @@ namespace App\Entity;
545522

546523
use ApiPlatform\Core\Annotation\ApiResource;
547524

548-
/**
549-
* @ApiResource(itemOperations={
550-
* "get": {
551-
* "method": "GET",
552-
* "controller": SomeRandomController::class
553-
* }
554-
* })
555-
*/
525+
#[ApiResource(
526+
itemOperations: [
527+
'get' => [
528+
'method' => 'GET',
529+
'controller' => SomeRandomController::class,
530+
],
531+
],
532+
)]
556533
class Weather
557534
{
558535
// ...
@@ -574,12 +551,9 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
574551

575552
final class SwaggerDecorator implements NormalizerInterface
576553
{
577-
private $decorated;
578-
579-
public function __construct(NormalizerInterface $decorated)
580-
{
581-
$this->decorated = $decorated;
582-
}
554+
public function __construct(
555+
private NormalizerInterface $decorated
556+
) {}
583557

584558
public function normalize($object, $format = null, array $context = [])
585559
{

0 commit comments

Comments
 (0)