retailcrm/auto-mapper-bundle
Composer 安装命令:
composer require retailcrm/auto-mapper-bundle
包简介
An object to object mapper bundle for Symfony
关键字:
README 文档
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'
retailcrm/auto-mapper-bundle 适用场景与选型建议
retailcrm/auto-mapper-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 459.92k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 06 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mapper automapper」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 retailcrm/auto-mapper-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 retailcrm/auto-mapper-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 retailcrm/auto-mapper-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
An object to object mapper bundle for Symfony2
data-mapper php library
PHP port of the popular automapper.org library
Papper is PHP convention-based object to object mapper
Convention-based Alternative ORM
Laravel wrapper for automapper-plus by Mark Gerarts
统计信息
- 总下载量: 459.92k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-06-26