1. Go to this page and download the library: Download bcc/auto-mapper-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bcc / auto-mapper-bundle example snippets
php
// app/autoload.php
$loader->registerNamespaces(array(
'BCC' => __DIR__.'/../vendor/bundles',
// your other namespaces
));
php
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new BCC\AutoMapperBundle\BCCAutoMapperBundle(),
// ...
);
}
php
// get mapper
$mapper = $container->get('bcc_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'
php
use BCC\AutoMapperBundle\Mapper\FieldAccessor\Closure;
// get mapper
$mapper = $container->get('bcc_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'
php
// get mapper
$mapper = $container->get('bcc_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'
php
// get mapper
$mapper = $container->get('bcc_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'
php
use BCC\AutoMapperBundle\Mapper\FieldAccessor\Constant;
// get mapper
$mapper = $container->get('bcc_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'
php
use BCC\AutoMapperBundle\Mapper\FieldAccessor\Constant;
// get mapper
$mapper = $container->get('bcc_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'
php
use BCC\AutoMapperBundle\Mapper\FieldAccessor\Constant;
// get mapper
$mapper = $container->get('bcc_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'
php
use BCC\AutoMapperBundle\Mapper\FieldAccessor\IfNull;
// get mapper
$mapper = $container->get('bcc_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'
php
namespace My;
use BCC\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';
}
}