bigyohann/symfony-dto-bundle

This package is abandoned and no longer maintained. No replacement package was suggested.

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

2.0.1 2023-02-20 22:34 UTC

This package is auto-updated.

Last update: 2023-09-20 23:52:19 UTC


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
    }