retailcrm / auto-mapper-bundle
An object to object mapper bundle for Symfony
Installs: 80 577
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 21
Type:symfony-bundle
Requires
- php: >=8.1
- doctrine/common: ^2.0||^3.0
- doctrine/orm: ^2.0||^3.0
- symfony/expression-language: ^5.4||^6.0||^7.0
- symfony/framework-bundle: ^5.4||^6.0||^7.0
- symfony/property-access: ^5.4||^6.0||^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.56
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^10.0
- retailcrm/php-code-style: ^1.0
This package is auto-updated.
Last update: 2024-11-11 14:44:08 UTC
README
github.com/retailcrm/AutoMapperBundle
is a fork of github.com/michelsalib/BCCAutoMapperBundle
Installation and configuration:
Get the bundle
composer install retailcrm/auto-mapper-bundle
Add AutoMapperBundle to your application kernel
<?php // app/AppKernel.php public function registerBundles() { return array( // ... new Retailcrm\AutoMapperBundle\AutoMapperBundle(), // ... ); }
Usage examples:
Giving a source and a destination object:
<?php namespace My; class SourcePost { public $name; public $description; /** * @var SourceAuthor */ public $author; } class SourceAuthor { public $name; } class DestinationPost { public $title; public $description; public $author; }
Use default map :
THe default map will automatically associate members that have the same name. It will automatically use public properties or look for getters.
You can create a default map and map object this way:
<?php // get mapper $mapper = $container->get('auto_mapper.mapper'); // create default map $mapper->createMap('My\SourcePost', 'My\DestinationPost'); // create objects $source = new SourcePost(); $source->description = 'Symfony2 developer'; $destination = new DestinationPost(); // map $mapper->map($source, $destination); echo $destination->description; // outputs 'Symfony2 developer'
Route members
On the previous example the fields name
and title
did not match. You can route members this way:
<?php // get mapper $mapper = $container->get('auto_mapper.mapper'); // create default map and route members $mapper->createMap('My\SourcePost', 'My\DestinationPost') ->route('title', 'name'); // create objects $source = new SourcePost(); $source->name = 'AutoMapper Bundle'; $destination = new DestinationPost(); // map $mapper->map($source, $destination); echo $destination->title; // outputs 'AutoMapper Bundle'
Note that if title
or name
is private, it will try to use getter and setter to route the member.
Map member with a closure
If you need some extra computation when mapping a member, you can provide a closure that will handle a specific member:
<?php use Retailcrm\AutoMapperBundle\Mapper\FieldAccessor\Closure; // get mapper $mapper = $container->get('auto_mapper.mapper'); // create default map and override members $mapper->createMap('My\SourcePost', 'My\DestinationPost') ->forMember('title', new Closure(function(SourcePost $source){ return \strtoupper($source->name); })); // create objects $source = new SourcePost(); $source->name = 'AutoMapper Bundle'; $destination = new DestinationPost(); // map $mapper->map($source, $destination); echo $destination->title; // outputs 'AUTOMAPPER BUNDLE'
Map a graph
You can map the author->name member this way:
<?php // get mapper $mapper = $container->get('auto_mapper.mapper'); // create default map and override members $mapper->createMap('My\SourcePost', 'My\DestinationPost') ->route('author', 'author.name'); // create objects $source = new SourcePost(); $source->author = new SourceAuthor(); $source->author->name = 'Michel'; $destination = new DestinationPost(); $mapper = new Mapper(); // map $mapper->map($source, $destination); echo $destination->author; // outputs 'Michel'
Note that if there are private members, it will try to use getter and setter to route the member.
Use Symfony Expression Language
If you want to define how properties are accessed, use Expression field accessor: You can read all documentation about ExpressionLanguage.
<?php // get mapper $mapper = $container->get('auto_mapper.mapper'); // create default map and override members $mapper->createMap('My\SourcePost', 'My\DestinationPost') ->forMember('author', new Expression('author.getFullName()')); // create objects $source = new SourcePost(); $source->author = new SourceAuthor(); $source->author->name = 'Michel'; $destination = new DestinationPost(); $mapper = new Mapper(); // map $mapper->map($source, $destination); echo destination->author; // outputs 'Michel'
Map to a constant
You can map a specific member to a constant:
<?php use Retailcrm\AutoMapperBundle\Mapper\FieldAccessor\Constant; // get mapper $mapper = $container->get('auto_mapper.mapper'); // create default map and override members $mapper->createMap('My\SourcePost', 'My\DestinationPost') ->forMember('title', new Constant('Constant title')); // create objects $source = new SourcePost(); $source->name = 'AutoMapper Bundle'; $destination = new DestinationPost(); // map $mapper->map($source, $destination); echo $destination->title; // outputs 'Constant title'
Deep object mapping
You can map a specific member to a constant:
<?php use Retailcrm\AutoMapperBundle\Mapper\FieldAccessor\Constant; // get mapper $mapper = $container->get('auto_mapper.mapper'); // create default map $mapper->createMap('My\SourcePost', 'My\DestinationPost'); // Set property deep mapping $mapper->filter('author', new ObjectMappingFilter('My\DestinationAuthor')); // create objects $source = new SourcePost(); $source->author = new SourceAuthor(); $destination = new DestinationPost(); // map $mapper->map($source, $destination); echo get_class(destination->author); // outputs 'My\DestinationAuthor'
Deep array object mapping
You can map a specific member to a constant:
<?php use Retailcrm\AutoMapperBundle\Mapper\FieldAccessor\Constant; // get mapper $mapper = $container->get('auto_mapper.mapper'); // create default map $mapper->createMap('My\SourcePost', 'My\DestinationPost'); // Set property deep mapping $mapper->filter('comments', new ArrayObjectMappingFilter('My\DestinationComment')); // create objects $source = new SourcePost(); $source->comments = array( new SourceComment(), new SourceComment(), ); $destination = new DestinationPost(); // map $mapper->map($source, $destination); echo get_class(destination->comments[0]); // outputs 'My\DestinationComment'
Apply a filter
You can apply a filter to a mapped member. Right now there is just a IfNull
filter that applies a default value if the field could not be mapped or is mapped on a null value:
<?php use Retailcrm\AutoMapperBundle\Mapper\FieldAccessor\IfNull; // get mapper $mapper = $container->get('auto_mapper.mapper'); // create default map and override members $mapper->createMap('My\SourcePost', 'My\DestinationPost') ->filter('title', new IfNull('Default title')); // create objects $source = new SourcePost(); $source->name = 'AutoMapper Bundle'; $destination = new DestinationPost(); // map $mapper->map($source, $destination); echo $destination->title; // outputs 'Default title'
Register a map
You can define map and add them to the Mapper at the container level.
Extend the Retailcrm\AutoMapperBundle\Mapper\AbstractMap
class:
<?php namespace My; use Retailcrm\AutoMapperBundle\Mapper\AbstractMap; class PostMap extends AbstractMap { function __construct() { $this->buildDefaultMap(); // generate the default map $this->route('title', 'name'); // override the title member } public function getDestinationType() { return 'My\DestinationPost'; } public function getSourceType() { return 'My\SourcePost'; } }
You can register all maps that implements MapInterface
with the auto_mapper.map
tag:
// services.yml _instanceof: Retailcrm\AutoMapperBundle\Mapper\MapInterface: tags: ['auto_mapper.map']
You can now use the mapper directly:
<?php // get mapper $mapper = $container->get('auto_mapper.mapper'); // create objects $source = new SourcePost(); $source->name = 'AutoMapper Bundle'; $destination = new DestinationPost(); // map $mapper->map($source, $destination); echo $destination->title; // outputs 'AutoMapper Bundle'
Ignore a field
You can ignore a destination field.
<?php // get mapper $mapper = $container->get('auto_mapper.mapper'); // create default map $mapper->createMap('My\SourcePost', 'My\DestinationPost') ->ignoreMember('description'); // create objects $source = new SourcePost(); $source->description = 'Symfony2 developer'; $destination = new DestinationPost(); // map $mapper->map($source, $destination); var_dump($destination->description); // ignored, will be null
Do not overwrite already set field
You can have the mapper not overwrite a field that is set on the destination.
<?php // get mapper $mapper = $container->get('auto_mapper.mapper'); // create default map $mapper->createMap('My\SourcePost', 'My\DestinationPost') ->setOverwriteIfSet(false); // create objects $source = new SourcePost(); $source->description = 'Symfony2 developer'; $destination = new DestinationPost(); $destination->description = 'Foo bar'; // map $mapper->map($source, $destination); var_dump($destination->description); // will be 'Foo bar'
Skip null
You can skip a field that is null.
<?php // get mapper $mapper = $container->get('auto_mapper.mapper'); // create default map $mapper->createMap('My\SourcePost', 'My\DestinationPost') ->setSkipNull(true); // create objects $source = new SourcePost(); $source->description = null; $destination = new DestinationPost(); $destination->description = 'Foo bar'; // map $mapper->map($source, $destination); var_dump($destination->description); // will be 'Foo bar'