Developed with love by KnpLabs Hire us for your project!
55

automapper-plus-bundle

by mark-gerarts

A symfony bundle for AutoMapper+

AutoMapperPlusBundle

A Symfony bundle for AutoMapper+.
To see it in action, check out the demo app.

Table of Contents

Installation

The bundle is available on packagist:

$ composer require mark-gerarts/automapper-plus-bundle

Don't forget to register the bundle:

$bundles = [
    new AutoMapperPlus\AutoMapperPlusBundle\AutoMapperPlusBundle(),
    // ...
];

Usage

The automapper is available as a service: automapper_plus.mapper (or just type hint
the AutoMapperPlus\AutoMapperInterface). You can register mapping configurations by
creating a class that implements the AutoMapperConfiguratorInterface. This configurator
class will have to define a configure method, that gets passed the configuration object:

<?php

namespace Demo;

use AutoMapperPlus\AutoMapperPlusBundle\AutoMapperConfiguratorInterface;
use AutoMapperPlus\Configuration\AutoMapperConfigInterface;
use Demo\Model\Employee\Employee;
use Demo\Model\Employee\EmployeeDto;

class AutoMapperConfig implements AutoMapperConfiguratorInterface
{
    public function configure(AutoMapperConfigInterface $config): void
    {
        $config->registerMapping(Employee::class, EmployeeDto::class)
            ->forMember('fullName', function (Employee $source) {
                return $source->getFirstName() . ' ' . $source->getLastName();
            });

        // And so on..
    }
}

If you use autowiring, the configurators will be picked up automatically.
Alternatively, you'll have to register the class as a service and tag it
with automapper_plus.configurator. You can optionally add a priority parameter
to the tag.

demo.automapper_configurator:
    class: Demo\AutoMapperConfig
    tags: ['automapper_plus.configurator']

You can register all your mappings in a single configurator class, or spread it
across multiple classes. The choice is yours!

Configuration

The options for the mapper can be configured. Create a config/packages/auto_mapper_plus.yaml
file (or add to your config.yaml for older Symfony versions) with the following contents:

auto_mapper_plus:
    options:
        create_unregistered_mappings: true

These options correspond with the ones of the Options object.

Full reference:

auto_mapper_plus:
    options:
        # Only one option for now, more coming soon!
        create_unregistered_mappings: true

Using the configuration is completely optional, you can just set the options directly
on the Options object in one of your configurators using $config->getOptions().

Further reading

For more info regarding the automapper itself, check out the
project page.

MIT License

Copyright (c) 2017 Mark Gerarts

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
auto_mapper_plus:
options:
create_unregistered_mappings: ~
  • Merge pull request #14 from ChristianRiesen/symfony5
    By web-flow, 1 month ago
  • Support Symfony 5 version
    By ChristianRiesen, 1 month ago
  • Merge pull request #11 from gianiaz/root-node-fix
    By web-flow, 3 months ago
  • Fixes deprecations for symfony 4.*
    By Giovanni Lenoci, 3 months ago
  • Changes required php-version
    By Giovanni Lenoci, 3 months ago
  • Setup travis and adds a simple test to fire up deprecations through symfony/phpunit-bridge
    By Giovanni Lenoci, 3 months ago
  • Merge branch 'feature-configuration-support'
    By mark-gerarts, 6 months ago
  • Update the README
    By mark-gerarts, 6 months ago
  • Merge branch 'master' into feature-configuration-support
    By mark-gerarts, 6 months ago
  • Revert @dev version constraint
    By mark-gerarts, 6 months ago
  • Set up support for defining options in (yaml) configuration
    By mark-gerarts, 6 months ago
  • Remove obsolete todo
    By web-flow, 1 year ago
  • Mention autowiring in the readme
    By web-flow, 1 year ago
  • Add priority support to `automapper_plus.configurator` tag (#7)
    By mark-gerarts, 1 year ago
  • Add support of symfony/flex (#6)
    By mark-gerarts, 1 year ago
  • Added more DI aliases (#5)
    By mark-gerarts, 1 year ago
  • Make bundle autowiring-friendly (#4)
    By mark-gerarts, 1 year ago
  • Symfony 4 support
    By mark-gerarts, 2 years ago
  • Use the stable branch of automapper+
    By mark-gerarts, 2 years ago
  • composer install -> composer require
    By web-flow, 2 years ago
  • Add demo link & TOC
    By mark-gerarts, 2 years ago
  • Add some more info to the README
    By mark-gerarts, 2 years ago
  • Add some missing composer config
    By mark-gerarts, 2 years ago
  • Fix typo in readme
    By mark-gerarts, 2 years ago
  • Create LICENSE (#1)
    By web-flow, 2 years ago
  • Initial commit
    By mark-gerarts, 2 years ago