Download the PHP package artyuum/request-dto-mapper-bundle without Composer
On this page you can find all versions of the php package artyuum/request-dto-mapper-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package request-dto-mapper-bundle
Request DTO Mapper Bundle
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).
Requirements
- PHP ^8.0
- Symfony ^5.0 or ^6.0
Installation
Configuration
Usage
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.
- Inject the DTO into your controller & configure it using the Dto attribute.
Alternatively, you can set the attribute directly on the argument:
If you have set some default options in the configuration file (the default extractor to use, whether to enable the validation), you can even make it shorter:
- That's it!
Attribute
The Dto attribute has the following seven properties:
1. Extractor
The FQCN (Fully-Qualified Class Name) of a class that implements the ExtractorInterface
. It basically contains the extraction logic and it's called by the mapper in order to extract the data from the request.
The bundle already comes with 3 built-in extractors that should meet most of your use-cases:
- BodyParameterExtractor (extracts the data from
$request->request->all()
) - JsonExtractor (extracts the data from
$request->toArray()
) - QueryStringExtractor (extracts the data from
$request->query->all()
)
If an error occurs when the extract()
method is called from the extractor class, the ExtractionFailedException will be thrown.
If these built-in extractor classes don't meet your needs, you can implement your own extractor like this:
Then pass it to the Dto
attribute like this:
If you don't set any value, the default value (defined in the bundle's configuration file) will be used.
Note: All classes implementing ExtractorInterface
are automatically tagged as "artyum_request_dto_mapper.extractor",
and this is needed by the mapper in order to retrieve the needed extractor class instance from the container.
2. Subject
The FQCN (Fully-Qualified Class Name) of the DTO you want to map (it must be present as your controller argument).
The "subject" property is required only if you're setting the attribute directly on the method. Example:
If you're setting the attribute on the method argument instead, the "subject" value can be omitted and won't be read by the mapper. Example:
3. Methods
It can contain a single or an array of HTTP methods that will "enable" the mapping/validation depending on the current HTTP method. In the following example, the DTO will be mapped & validated only if the request method is "GET".
or
If the array is empty (this is the default value), the mapper will always map the DTO and validate it.
4. Denormalization Options
The options that will be passed to the Denormalizer before mapping the DTO.
Example:
If an error occurs when the denormalize()
method is called from the Denormalizer, the DtoMappingFailedException will be thrown.
5. Validate
Whether to validate the DTO (once the mapping is done). Internally, the validator component will be used, and if you do not have it installed a LogicException
will be thrown.
Example:
If the validation failed (due to the constraint violations), the constraint violations will be available as request attribute:
If you don't set any value, the configured value (defined in the bundle's configuration file) will be used.
6. Validation Groups
The validation groups to pass to the validator.
Example:
If you don't set any value, the configured value (defined in the bundle's configuration file) will be used.
7. Throw on violation
When the validation failed, the DtoValidationFailedException will be thrown, and you will be able to get a list of these violations by calling the getViolations()
method.
Setting the value to false
will prevent the exception from being thrown, and your controller will still be executed.
Example:
If you don't set any value, the configured value (defined in the bundle's configuration file) will be used.
Events
- PreDtoMappingEvent - dispatched before the mapping is made.
- PostDtoMappingEvent - dispatched once the mapping is made.
- PreDtoValidationEvent - dispatched before the validation is made (if the validation is enabled).
- PostDtoValidationEvent - dispatched once the validation is made (if the validation is enabled).
All versions of request-dto-mapper-bundle with dependencies
phpdocumentor/reflection-docblock Version ^5.3
symfony/config Version ^5.0 || ^6.0
symfony/event-dispatcher Version ^5.0 || ^6.0
symfony/framework-bundle Version ^5.0 || ^6.0
symfony/http-kernel Version ^5.0 || ^6.0
symfony/property-access Version ^5.0 || ^6.0
symfony/property-info Version ^5.0 || ^6.0
symfony/serializer Version ^5.0 || ^6.0