Git Product home page Git Product logo

automapperbundle's Introduction

AutoMapper

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'

automapperbundle's People

Contributors

shatzibitten avatar benjaminpaap avatar bfeaver avatar jorgegarciafon avatar addfs avatar zerrvox avatar jaizquierdogalan avatar kifril avatar mikefox avatar

Stargazers

Kirill avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.