This bundle provides an easy way to automatically map the incoming request data to a DTO and optionally validate it. It's using the powerful Serializer component under the hood along with the Validator component (optional).
- PHP ^8.0
- Symfony ^5.0 or ^6.0
composer require artyuum/request-dto-mapper-bundle
# config/packages/artyum_request_dto_mapper.yaml
artyum_request_dto_mapper:
# Used if the attribute does not specify any (must be a FQCN implementing "\Artyum\RequestDtoMapperBundle\Extractor\ExtractorInterface").
default_extractor: null # Example: Artyum\RequestDtoMapperBundle\Extractor\JsonExtractor
# The configuration related to the denormalizer (https://symfony.com/doc/current/components/serializer.html).
denormalizer:
# Used when mapping the request data to the DTO if the attribute does not set any.
default_options: []
# Used when mapping the request data to the DTO (merged with the values passed by the attribute or "default_options").
additional_options: []
# The configuration related to the validator (https://symfony.com/doc/current/validation.html).
validation:
# Whether to validate the DTO after mapping it.
enabled: false
# Used when validating the DTO if the attribute does not set any.
default_groups: []
# Used when validating the DTO (merged with the values passed by the attribute or "default_groups").
additional_groups: []
# Whether to throw an exception if the DTO validation failed (constraint violations).
throw_on_violation: true
This is a simple step-by-step example of how to make a DTO that will be used by the bundle.
- Create the DTO that represents the structure of the content the user will send to your controller.
class PostPayload {
/**
* @Assert\Sequentially({
* @Assert\NotBlank,
* @Assert\Type("string")
* })
*
* @var string|null
*/
public $content;
}
- Inject the DTO into your controller & configure it using the Dto attribute.