This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
14 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,25 @@ | ||
# AutoMapper Bundle | ||
|
||
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. | ||
The AutoMapper Bundle is the Symfony bridge for [AutoMapper component](https://github.com/jolicode/automapper). | ||
|
||
## Quick Start 🚀 | ||
|
||
```shell | ||
composer require jolicode/automapper-bundle | ||
``` | ||
|
||
Then add the bundle to your bundles list in `config/bundles.php` | ||
```php | ||
<?php | ||
|
||
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 specify `source` & `target` data types and related configuration using `pass` field. | ||
This configuration should implements `AutoMapper\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 to `false`. | ||
|
||
## Normalizer Bridge | ||
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. | ||
|
||
## Future consideration | ||
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: | ||
You can read more about this library and how to use it on the [documentation](https://jolicode.github.io/automapper/#/symfony). | ||
|
||
* 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) | ||
## Support | ||
|
||
Feel free to challenge as much as possible. | ||
For support, please create an issue on [GitHub tracker](https://github.com/jolicode/automapper-bundle/issues) |