Skip to content

DX initiative #20

Description

@Korbeil

Hey,

The objective is to make AutoMapper easier to work with for developpers.
In this issue I'll try to list all stuff developpers needs to fit their needs when using AutoMapper.

All the following stuff are examples and subject to change in their implementation.

  • Being able to give a custom property output for a given target
class Foo
{
  public function __construct(
    #[MapTo(('array', 'phrase')]
    #[MapTo(Bar::class, 'phrase')]
    public string $sentence,
  ) {
  }
}
  • Being able to give a custom property input for a given target
class Foo
{
  public function __construct(
    #[MapFrom(('array', 'phrase')]
    #[MapFrom(Bar::class, 'phrase')]
    public string $sentence,
  ) {
  }
}
  • Being able to map a field only if a certain condition is filled for a given target
class Foo
{
  public function __construct(
    #[MapIf(('array', fn() => true)]
    #[MapIf(Bar::class, fn() => false)]
    public string $sentence,
  ) {
  }
}
  • Having a way to override / custom the mapping of a property for a given transformation
final readonly class FromSourceCustomPropertyTransformer implements CustomPropertyTransformerInterface
{
    public function supports(string $source, string $target, string $propertyName): bool
    {
        return $source === UserDTO::class && $target === 'array' && $propertyName === 'name';
    }

    public function transform(mixed $input): mixed
    {
        return "{$input} set by custom property transformer";
    }
}
  • Having a way to override / custom the mapping for a given transformation as a whole
final readonly class FromTargetCustomModelTransformer implements CustomModelTransformerInterface
{
    public function supports(string $source, string $target): bool
    {
        return $source === 'array' && $target === AddressDTO::class;
    }

    public function transform(mixed $input): mixed
    {
        $addressDTO = new \AutoMapper\Tests\Fixtures\AddressDTO();
        $addressDTO->city = "{$input['city']} from custom model transformer";

        return $addressDTO;
    }
}
  • Having a way to add custom code during some steps of the transformation:
    • beforeInitialization
    • afterInitialization
    • afterHydratation
class Foo
{
  public function __construct(
    public string $sentence,
  ) {
  }

  #[MapEvent(Event::AFTER_INITIALIZATION)]
  public function doctrineHook(mixed $object): void
  {
    // link my entity to Doctrine UOW
  }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions