This version of the bundle requires Symfony 3.4+.
The dto-handler-bundle loads the content of a request into a Data Transfer Object (DTO), mapping its properties such as entities from the database.
It uses the ParamConverterInterface provided by the SensioLabs Framework Extra Bundle to automatically map the request content to the appropriate variables.
The dto-handler-bundle is simple to use as it requires almost no configuration. To use it in a controller, simply declare the variable in the controller's argument:
public function postAction(DummyDataTransferObject $dto): Response
{
// ...
}
And add the DTO annotation to your DTO:
/**
* @DTO
*/
final class DummyDataTransferObject
{
// ...
}
This bundle required at least Symfony 3.4.
You can use composer to install dto-handler-bundle:
composer require chaplean/dto-handler-bundle
Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php file of your project:
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = [
// ...
new Chaplean\Bundle\DtoHandlerBundle\ChapleanDtoHandlerBundle(),
];
// ...
}
// ...
}
In the following example, the data contained in the request will be loaded in the DTO.
The $property1 and the $property2 will be not changed, so their values will be the one set in the request.
The $property3 will be mapped to the appropriate entity using the field keyname, so the value of the property3 field in the request should be a keyname of the DummyEntity.
The $property4 will be an array of the entities mapped by id, so the property4 value in the request should be an array of id.
Controller:
/**
* ...
*
* @ParamConverter(
* name="dtoVariable",
* converter="data_transfer_object_converter",
* options={"validations": "violationsList"}
* )
*
* @param DummyDataTransferObject $dummyDataTransferObject
* @param ConstraintViolationListInterface $violationsList
*
* @return Response
*/
public function postAction(
DummyDataTransferObject $dummyDataTransferObject,
ConstraintViolationListInterface $violationsList
): Response {
// ...
}
Data Transfer Object (DummyDataTransferObject):
/**
* Class DummyDataTransferObject.
*
* @DTO
*/
final class DummyDataTransferObject
{
/**
* @var string
*/
public $property1;
/**
* @var integer
*
* @Assert\Type("integer")
*/
public $property2;
/**
* @var DummyEntity
*
* @Assert\Type("Chaplean\Bundle\DtoHandlerBundle\Tests\Resources\Entity\DummyEntity")
* @MapTo("keyname")
*/
public $property3;
/**
* @var DummyEntity
*
* @Assert\All(
* @Assert\Type("Chaplean\Bundle\DtoHandlerBundle\Tests\Resources\Entity\DummyEntity")
* )
*/
public $property4;
}
dto-handler-bundle follows semantic versioning. In short the scheme is MAJOR.MINOR.PATCH where
Versions bellow 1.0.0 are considered experimental and breaking changes may occur at any time.
Contributions are welcomed! There are many ways to contribute, and we appreciate all of them. Here are some of the major ones:
master branch.As a reminder, all contributors are expected to follow our Code of Conduct.
You might find the following commands usefull when hacking on this project:
# Install dependencies
composer install
# Run tests
bin/phpunit
dto-handler-bundle is distributed under the terms of the MIT license.
See LICENSE for details.
How can I help you explore Laravel packages today?