Skip to content

Conversation

@soyuka
Copy link
Member

@soyuka soyuka commented Nov 25, 2025

No description provided.

Copy link

@JonathanBaudoin JonathanBaudoin left a 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.

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?

Comment on lines +56 to 73
#[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 ('user@example.com' === $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));
}
}

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.

Comment on lines +244 to +245
use App\Dto\DiscountBook;
use App\State\DiscountBookProcessor;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants