Skip to content

Commit ad5a148

Browse files
authored
Merge pull request #646 from soyuka/doc-io
Document input/output resource attribute
2 parents 22bd0ad + eec7207 commit ad5a148

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

core/dto.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## How to Use a DTO for Writing
44

5+
!> The following isn't recommended anymore, please use [Input/Output](specify-an-input-and-or-output-class) instead
6+
57
Sometimes it's easier to use a DTO than an Entity when performing simple
68
operation. For example, the application should be able to send an email when
79
someone has lost its password.
@@ -126,6 +128,8 @@ services:
126128

127129
## How to Use a DTO for Reading
128130

131+
!> The following isn't recommended anymore, please use [Input/Output](specify-an-input-and-or-output-class) instead
132+
129133
Sometimes, you need to retrieve data not related to an entity.
130134
For example, the application can send the
131135
[list of supported locales](https://github.com/symfony/demo/blob/master/config/services.yaml#L6)
@@ -350,3 +354,68 @@ use Swagger\Annotations as SWG;
350354
*/
351355
public function __invoke()
352356
```
357+
358+
### Specify an Input and/or Output Class
359+
360+
For a given resource class, you may want to have a different representation of this class as input (write) or output (read).
361+
To do so, a resource can take an input and/or an output class:
362+
363+
```php
364+
<?php
365+
// api/src/Entity/Data.php
366+
367+
namespace App\Entity;
368+
369+
use ApiPlatform\Core\Annotation\ApiResource;
370+
use App\Dto\Input;
371+
use App\Dto\Output;
372+
373+
/**
374+
* @ApiResource(
375+
* inputClass=Input::class,
376+
* outputClass=Output::class,
377+
* )
378+
*/
379+
final class Data
380+
{
381+
}
382+
```
383+
384+
The `input_class` attribute will be used in the denormalization phase, when transforming the user data to a resource instance.
385+
The `output_class` attribute will be used in the normalization phase, this class represents how the `Data` resource will be given in the `Response`.
386+
387+
To create a `Data`, we `POST` an `Input` and get back an `Output` in the response.
388+
For this example to work, one could use the following `DataPersister`:
389+
390+
```php
391+
<?php
392+
393+
namespace App\DataPersister;
394+
395+
use ApiPlatform\Core\DataPersister\DataPersisterInterface;
396+
use App\Dto\Input;
397+
use App\Dto\Output;
398+
399+
final class InputDataPersister implements DataPersisterInterface
400+
{
401+
public function supports($data): bool
402+
{
403+
return $data instanceof Input;
404+
}
405+
406+
public function persist($data)
407+
{
408+
$output = new Output();
409+
$output->name = $data->name;
410+
$output->id = 1;
411+
412+
return $output;
413+
}
414+
415+
public function remove($data)
416+
{
417+
// TODO: implement removal
418+
return null;
419+
}
420+
}
421+
```

0 commit comments

Comments
 (0)