mark-gerarts / automapper-plus

An AutoMapper for PHP
MIT License
551 stars 30 forks source link

Allow guessing of property names #1

Open mark-gerarts opened 7 years ago

mark-gerarts commented 7 years ago

As it is now, the classes need to follow strict naming conventions in order to be mapped. A nice feature would be to be able to change this behaviour via the Options, making the mapper instead guess the source property name.

For example, when looking for the property firstName, the mapper would match the first occurrence of either [firstName, first_name, FirstName, ...].

Credit

samrap commented 7 years ago

I am currently reading through the source to wrap my head around the architecture of this package. I'd really like to be a major contributor to this down the line.

There are clearly some arguments for and against this feature so I definitely think it should be something that must be explicitly set in the Options. Maybe something like:

$config
    ->registerMapping(CrazySource::class, SomeOtherCrazyDestination::class)
    ->withoutNamingConventions();

The method withoutNamingConventions() could simply set the naming conventions to a NoNamingConventions implementation. The NoNamingConventions would then be responsible for converting the property name, perhaps stripping it of underscores and converting it to lowercase?

mark-gerarts commented 7 years ago

That's definitely a step in the right direction. Using a NoNamingConventions is an option, although it might be more fitting to put this logic in a NameResolver, either adding logic to the default one or creating a completely new one. The advantage here being that the NameResolver can already be overwritten in the Options.

That being said, I'm open for other ideas. I think the hardest part will be the actual guessing of source properties. If you wanna take a shot at this, then go ahead. I'll be more than happy to merge a good solution for this.

Thanks for your interest!