-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
improve dto documentation #2231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.2
Are you sure you want to change the base?
Conversation
JonathanBaudoin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really better, good job. 👍
| public string $email; | ||
| } | ||
| ``` | ||
| You can map a DTO Resource directly to a Doctrine Entity using `stateOptions`. This automatically configures the built-in State Providers and Processors to fetch/persist data using the Entity and map it to your Resource (DTO) using the Symfony Object Mapper. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only with Symfony >= 7.3, right? And what about Laravel?
| #[Map(transform: [self::class, 'formatPrice'])] | ||
| public string $price; | ||
|
|
||
| /** | ||
| * @implements ProcessorInterface<UserResetPasswordDto, User> | ||
| */ | ||
| final class UserResetPasswordProcessor implements ProcessorInterface | ||
| { | ||
| /** | ||
| * @param UserResetPasswordDto $data | ||
| * | ||
| * @throws NotFoundHttpException | ||
| */ | ||
| public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): User | ||
| public static function formatPrice(mixed $price, object $source, ?object $target): int|string | ||
| { | ||
| if ('[email protected]' === $data->email) { | ||
| return new User(email: $data->email, id: 1); | ||
| // Transform Entity (int) to DTO (string with $) | ||
| if ($target instanceof self) { | ||
| return number_format($price / 100, 2).'$'; | ||
| } | ||
|
|
||
| throw new NotFoundHttpException(); | ||
| // Transform DTO (string with $) to Entity (int) | ||
| if ($target instanceof BookEntity) { | ||
| return 100 * (int) str_replace('$', '', $price); | ||
| } | ||
|
|
||
| throw new \LogicException(\sprintf('Unexpected "%s" source.', $source::class)); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me, a DTO it's here to contain data, not to transform data. When I see the code, I'm sure it's possible to transform the data outside the DTO. IMO, the doc should at least mention (in a comment or below) that it's possible (recommended?) to transform the data outside the DTO.
| use App\Dto\DiscountBook; | ||
| use App\State\DiscountBookProcessor; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| use App\Dto\DiscountBook; | |
| use App\State\DiscountBookProcessor; | |
| use App\Dto\DiscountBook; | |
| use App\Entity\Book as BookEntity; | |
| use App\State\DiscountBookProcessor; |
| } | ||
|
|
||
| // 3. Execute Domain Logic | ||
| $bookEntity->discount($data->percentage); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the domain logic here. Maybe it's just an example, but if it is, the comment does not represent the code.
IMO, you should add a comment below (// Add your own domain logic) OR a real example like you did into the DTO.
No description provided.