liuggio/filler-dto

Retrieve and set properties from and to a DTO

0.1.0 2016-04-20 09:51 UTC

This package is auto-updated.

Last update: 2024-10-18 23:08:29 UTC


README

It helps you to bridge DTOs and Model Entities.

Fill or retrieve properties from and to DTO objects!

filler-dto on packagist

Latest Stable Version Latest Unstable Version Total Downloads

Fast

It doesn't use the \Reflection.

Before the Cure

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);
    }
}

After the Cure

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;
}

From Request

Do you want to map an object from the 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)) ...
    }
}

Copy 2 objects

You can also use it for copy properties between 2 objects

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

Differences?

We needed it for an edge case, but then we have decided to release it because if you are used to develop with the Command pattern, this lib could help you to develop faster app.

More info at: verraes:decoupling-symfony2-forms-from-entities

Install

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)

Example

Please have a look to the tests and tests/Fixtures folders :)

Compatibility

  • = 5.4

  • hhvm