It helps you to bridge DTOs and Model Entities.
Fill or retrieve properties from and to DTO objects!
filler-dto
on packagist
It doesn't use the \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;
}
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)) ...
}
}
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
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
composer require liuggio/filler-dto dev-master
trait PropertyTrait
fillProperties($dto)
getAllProperties($filter = null)
trait HTTPPropertyTrait
fillPropertiesFromRequest(Request $request, $name = '')
copyPropertiesFromRequest(Request $request, $to)
Please have a look to the tests and tests/Fixtures folders :)
-
= 5.4
- hhvm