vklymniuk/dto-mapper-bundle

Symfony mapper bundle,轻松转换数据。

此包的官方仓库似乎已消失,因此该包已被冻结。

v0.5.1 2021-05-28 12:58 UTC

This package is auto-updated.

Last update: 2021-12-01 00:12:29 UTC


README

Build Status

为什么使用 VKMapperBundle?

该组件通过提供注解声明,使绑定所有数据映射规则的过程变得更容易。您可以从对象中提取内容,并将数据从原始数组填充到对象中。与慢速且低效的反射类不同,该组件使用快速且性能优化的 代码生成器。所有映射和 DI 配置都使用原生 symfony 缓存和延迟加载。

VKMapperBundle 使用定制的性能策略

  • Object 转换为 array
  • Object 提取到 array
  • 将数据从 array 放入 Object

安装

建议的安装方法是通过 composer

php composer.phar require symfony-ext/dto-mapper-bundle

设置

在 bundles.php 文件中注册组件。

<?php

return [
    VKMapperBundle\VKMapperBundle::class => ['all' => true],
];

使用您想添加到映射的类标记目录。

    Tests\DataFixtures\Dto\:
      resource: '../../DataFixtures/Dto/*'
      tags:
        - { name: dto_mapper.annotated }
    
    Tests\DataFixtures\Model\:
      resource: '../../DataFixtures/Model/*'
      tags:
        - { name: dto_mapper.annotated }

在您标记的目录中填写注解类。类示例

<?php

namespace Tests\DataFixtures\Dto;

use VKMapperBundle\Annotation\MappingMeta\SourceClass;
use VKMapperBundle\Annotation\MappingMeta\DestinationClass;
use VKMapperBundle\Annotation\MappingMeta\Strategy;

/**
 * @SourceClass
 */
class Source
{
    /**
     * @Strategy\XPathStrategy(xPath="nodeA.inner.optionA")
     */
    public $nodeA;
}

/**
 * @DestinationClass 
 */
class Destination
{
    /**
     * Contains value optionA.
     */
    public $nodeA;
}

使用

将 MapperInterface 注入到您的服务中。

<?php

use DataMapper\MapperInterface;

class DataTransformer
{
    public function __construct(MapperInterface $mapper);
}

将源转换为 dto 对象

<?php
use DataMapper\MapperInterface;

/** @var MapperInterface $mapper */
$mapper->convert($source, $destination);

从对象中提取内容到数组

<?php
use DataMapper\MapperInterface;

/** @var MapperInterface $mapper */
$mapper->extract($object);

文档

您可以在 文档 中了解有关组件的可能性和如何使用 VKMapperBundle 的更多信息。