liuggio/filler-dto

从和到DTO检索和设置属性

0.1.0 2016-04-20 09:51 UTC

This package is auto-updated.

Last update: 2024-09-18 22:57:26 UTC


README

它帮助您连接DTO和模型实体。

从和到DTO对象中填充或检索属性!

filler-dto在packagist上

Latest Stable Version Latest Unstable Version Total Downloads

快速

它不使用\Reflection。

在治疗之前

class Cart
{
    private $a;
    private $b;
    private $c;

    private function __construct($a, $b, $c)
    {
        $this->a = $a;
        $this->b = $b;
        $this->c = $c;
    }

    public static function startShipping(StartShippingData $data)
    {
        return new self($data->a, $data->b, null);
    }

    public static function addProduct(AddProductData $data)
    {
        return new self(null, $data->b, $data->cs);
    }
}

在治疗之后

class Cart
{
    use PropertyTrait;

    private $a;
    private $b;
    private $c;

    private function __construct($dto)
    {
        $this->fillProperties($dto);
    }

    public static function startShipping(StartShippingData $data)
    {
        return new self($data);
    }

    public static function addProduct(AddProductData $data)
    {
        return new self($data);
    }
}

class StartShippingData
{
    public $a;
    public $b;
}

class AddProductData
{
    public $b;
    public $c;
}

从请求

您想从Request中映射一个对象吗?

use Liuggio\Filler\HTTPPropertyTrait;

class StartShippingDTO
{
    use HTTPPropertyTrait;

    private $developer;

    public function __construct(Request $request)
    {
        $this->fillPropertiesFromRequest($request);
    }
...
}

Class Controller
{
    public function startShippingAction(Request $request)
    {
        $startShipping = new StartShippingDTO($request);

        if ($this->isValid($startShipping)) ...
    }
}

复制2个对象

您也可以用它来在2个对象之间复制属性

use HTTPPropertyTrait;
$to = new DTOFromRequest();
$this->fillPropertiesFromRequest($request, $to);
// the $to object has all the var from the Request

区别?

我们为了一个边缘情况需要它,但后来我们决定发布它,因为如果您习惯于使用Command模式进行开发,这个库可以帮助您更快地开发应用程序。

更多信息请访问:verraes:decoupling-symfony2-forms-from-entities

安装

composer require liuggio/filler-dto dev-master

API

trait PropertyTrait
   fillProperties($dto)
   getAllProperties($filter = null)

trait HTTPPropertyTrait
    fillPropertiesFromRequest(Request $request, $name = '')
    copyPropertiesFromRequest(Request $request, $to)

示例

请查看测试和tests/Fixtures文件夹:)

兼容性

  • = 5.4

  • hhvm