bigyohann / symfony-dto-bundle
Symfony bundle to add automatic Dto converter
Installs: 126
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=8.1
- ext-ctype: *
- ext-iconv: *
- symfony/framework-bundle: >=6.2
- symfony/runtime: 6.*
- symfony/serializer: 6.*
- symfony/serializer-pack: ^1.1
- symfony/validator: 6.*
- symfony/yaml: 6.*
README
Motivation
- Create and deploy a lib to packagist
- Create a bundle for Symfony
- Facilitate the way of handle serializing
Usage
Extends your Dto class from Bigyohann\DtoBundle\Dto\Dto
,
make all your properties private and add getter.
By default, I add a convert function to automatically set Dto properties to object passed as parameter.
You can annotate your property with attribute
Bigyohann\DtoBundle\Attributes\ConvertProperty
and if parameter
shouldConvertAutomatically
is set at false, property will not be
mapped to object passed as parameter, but you can still access it in Dto if you want to
do a specific action with this value.
if you don't want to use convert function, you can use
Bigyohann\DtoBundle\Dto\DtoInterface
.
Exemple
use Bigyohann\DtoBundle\Attributes\ConvertProperty; use Bigyohann\DtoBundle\Dto\Dto; use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Constraints\Type; class UserDto extends Dto { #[ConvertProperty] #[Type(type: 'string')] #[Length(min: 2, max: 20)] private ?string $name; #[ConvertProperty(shouldConvertAutomatically: false)] #[Type(type: 'string')] private ?string $password; public function getPassword(): ?string { return $this->password; } public function getName(): ?string { return $this->name; } }
Inject Dto directly in Controller functions
public function create(UserDto $dto){ $user = new User(); $dto->transformToObject($user); // password is not automatically convert in $user, do your custom logic with $dto->getPassword() // Add your custom logic here }