- property_exists: documentation ( source)
- var_dump: documentation ( source)
- get_object_vars: documentation ( source)
<?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 $fetcher, $dto) {
foreach ($fetcher() as $property => $value) {
if (property_exists($dto, $property)) {
$dto->$property = $value;
}
}
return $dto;
};
return $transmitter($fetcher($domainObject), $dto);
};
var_dump($dtoFiller($domainObject, new Dto()));