The component ships a Bundle to allow a quick integration with Symfony.
To use it, you just have to add the main bundle class to your config/bundles.php
file.
return [
// ...
AutoMapper\Bundle\AutoMapperBundle::class => ['all' => true],
];
Then configure the bundle to your needs, for example:
automapper:
autoregister: true
mappings:
- source: AutoMapper\Bundle\Tests\Fixtures\User
target: AutoMapper\Bundle\Tests\Fixtures\UserDTO
pass: DummyApp\UserConfigurationPass
Possible properties:
normalizer
(default:false
): A boolean which indicate if we inject the AutoMapperNormalizer;cache_dir
(default:%kernel.cache_dir%/automapper
): This settings allows you to customize the output directory for generated mappers;mappings
: This option allows you to customize Mapper metadata, you have to specifysource
&target
data types and related configuration usingpass
field. This configuration should implementsAutoMapper\Bundle\Configuration\ConfigurationPassInterface
.allow_readonly_target_to_populate
(default:false
): Will throw an exception if you use a readonly class as target to populate if set tofalse
.
A Normalizer Bridge is available, aiming to be 100% feature compatible with the ObjectNormalizer of the symfony/serializer
component. The goal of this bridge is not to replace the ObjectNormalizer but rather providing a very fast alternative.
As shown in the benchmark above, using this bridge leads up to more than 8x speed increase in normalization.
Things that could be done later:
- symfony/form bridge for mapping request data to object
- symfony/validator integration:
PHP 7.4 may give a problem to the symfony/validator component where typed properties can be problem, given a class like this:
class Foo {
/** @Assert\NotNull() */
public int $foo;
}
An user may send a null value (in a form by example or JSON), and PHP will raise an error before the validation, since the validation occurs on the mapped object.
This component can help resolving this case with the actual behavior:
- Create a dummy class with the same properties as Foo but without type checking
- Mapping user data to this dummy class (using the automapper component)
- Validating this dummy class with the metadata from the Foo class
- Mapping the dummy object to the foo class (using the automapper component)
Feel free to challenge as much as possible.