Skip to content

Instantly share code, notes, and snippets.

@cystbear
Last active April 23, 2020 08:26
Show Gist options
  • Save cystbear/88a1cdad05d45cb488b7 to your computer and use it in GitHub Desktop.
Save cystbear/88a1cdad05d45cb488b7 to your computer and use it in GitHub Desktop.
Shifter Domain -> DTO
<?php
class Domain
{
protected $foo;
protected $bar;
protected $baz;
public function setFoo($foo)
{
$this->foo = $foo;
return $this;
}
public function getFoo()
{
return $this->foo;
}
public function setBar($bar)
{
$this->bar = $bar;
return $this;
}
public function getBar()
{
return $this->bar;
}
public function setBaz($baz)
{
$this->baz = $baz;
return $this;
}
public function getBaz()
{
return $this->baz;
}
}
class Dto {
public $foo;
public $baz;
}
$domainObject = new Domain();
$domainObject
->setFoo('foo')
->setBar('bar')
->setBaz('baz')
;
$dtoFiller = function($domainObject, $dto) {
$fetcher = function($object) {
$getter = function() {
return get_object_vars($this);
};
return $getter->bindTo($object, $object);
};
$transmitter = function(\Closure $getter, $dto) {
foreach ($getter() as $property => $value) {
if (property_exists($dto, $property)) {
$dto->$property = $value;
}
}
return $dto;
};
return $transmitter($fetcher($domainObject), $dto);
};
var_dump($dtoFiller($domainObject, new Dto()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment