mark-gerarts / automapper-plus-bundle
AutoMapper+ 的 Symfony 扩展包
Requires
- php: >=7.1.0
- mark-gerarts/auto-mapper-plus: ^1.3
- symfony/framework-bundle: ^3.4|^4.0|^5.0|^6.0|^7.0
Requires (Dev)
- matthiasnoback/symfony-dependency-injection-test: ^3.1|^4.1
- phpunit/phpunit: ^7.5|^8.0|^9.0|^10.0
- symfony/phpunit-bridge: ^5.0|^6.0|^7.0
- symfony/property-access: ^3.4|^4.4|^5.0|^6.0|^7.0
Suggests
- symfony/property-access: ^3.4|^4.4|^5.4|^6.0|^7.0
This package is auto-updated.
Last update: 2024-09-13 13:56:16 UTC
README
一个用于 AutoMapper+ 的 Symfony 扩展包。要查看其运行效果,请查看 示例应用。
目录
安装
该扩展包可在 Packagist 上找到
$ composer require mark-gerarts/automapper-plus-bundle
不要忘记注册扩展包
$bundles = [ new AutoMapperPlus\AutoMapperPlusBundle\AutoMapperPlusBundle(), // ... ];
使用
AutoMapper 可作为服务使用: automapper_plus.mapper
(或者直接使用 AutoMapperPlus\AutoMapperInterface
类型提示)。
您可以通过创建一个实现 AutoMapperConfiguratorInterface
接口的类来注册映射配置。这个配置器类必须定义一个 configure
方法,该方法会传入配置对象
<?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.. } }
如果您使用自动注入,配置器将被自动拾取。或者,您必须将类注册为服务并标记为 automapper_plus.configurator
。您可以选择性地为标记添加优先级参数。
demo.automapper_configurator: class: Demo\AutoMapperConfig tags: ['automapper_plus.configurator']
您可以在单个配置器类中注册所有映射,或者分散到多个类中。选择权在您。
配置
可以配置映射器的选项。创建一个 config/packages/automapper_plus.yaml
文件(或者为旧版本的 Symfony 添加到 config.yaml
)并包含以下内容
auto_mapper_plus: options: create_unregistered_mappings: true
这些选项与 选项对象 中的选项相对应。
完整参考(目前不是所有选项都受支持,即将推出更多!)
auto_mapper_plus: options: # These options are example values, and not necessarily the default # ones. If an option is not provided, the base library's default will # be used. create_unregistered_mappings: true skip_constructor: true use_substitution: true ignore_null_properties: false # Note that this should be the service name, and not necessarily the # FQCN. property_accessor: AutoMapperPlus\AutoMapperPlusBundle\PropertyAccessor\SymfonyPropertyAccessorBridge
使用配置是完全可选的,您可以直接在配置器中的一个 Options
对象上设置选项,使用 $config->getOptions()
。
Symfony 属性访问器
该扩展包包含一个用于 Symfony PropertyAccessor 的桥梁。它提供了两种变体
SymfonyPropertyAccessorBridge
实际上只提供了 Symfony 组件的功能,这意味着您不能直接设置私有属性(如果您想要更严格的话,这是一个好事)。DecoratedPropertyAccessor
使用 Symfony 属性访问,但在失败时回退到默认值。这意味着即使没有 getter/setter,也会处理私有属性。
两种选项都允许使用完整的属性路径与 fromProperty
一起使用,例如 forMember('aProperty', Operation::fromProperty('some.nested[child]'));
。请注意,其他属性路径的使用尚未测试,并且不能保证其工作。这将在 2.x 版本中调查(相关问题)。
示例服务定义
services: AutoMapperPlus\AutoMapperPlusBundle\PropertyAccessor\SymfonyPropertyAccessorBridge: arguments: $propertyAccessor: '@property_accessor'
在您的 automapper_plus.yaml
配置中
auto_mapper_plus: options: property_accessor: 'AutoMapperPlus\AutoMapperPlusBundle\PropertyAccessor\SymfonyPropertyAccessorBridge'
进一步阅读
有关 AutoMapper 本身的更多信息,请查看 项目页面。