-
-
Save gquemener/dad6142528ba5f9b554ddc4753d2f60e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare (strict_types = 1); | |
namespace App\Infrastructure; | |
use App\Domain; | |
trait HasDomainModel | |
{ | |
public function mapToDomainModel() | |
{ | |
$model = (new \ReflectionClass($this->getDomainModelClass()))->newInstanceWithoutConstructor(); | |
$hydrator = (function($entity) { | |
return function(string $propertyName) use ($entity) { | |
return function(callable $propertyValue) use ($entity, $propertyName) { | |
return function(Domain\Program $program) use ($entity, $propertyName, $propertyValue) { | |
$program->{$propertyName} = $propertyValue($entity); | |
}; | |
}; | |
}; | |
})($this); | |
$mapper = $this->getPropertyMapper(); | |
foreach ($this->getDomainMapping() as $entityPropertyName => $domainModelPropertyName) { | |
$hydrate = $hydrator($domainModelPropertyName)( | |
isset($mapper[$entityPropertyName]) ? $mapper[$entityPropertyName] : function($entity) use ($entityPropertyName) { | |
return $entity->{'get'.ucfirst($entityPropertyName)}(); | |
} | |
); | |
$hydrate = \Closure::bind($hydrate, null, $model); | |
$hydrate($model); | |
} | |
return $model; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare (strict_types = 1); | |
namespace App\Infrastructure\Entity; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Doctrine\ORM\Mapping as ORM; | |
use App\Infrastructure\HasDomainModel; | |
use App\Domain; | |
/** | |
* @ORM\Entity | |
*/ | |
final class Program | |
{ | |
use HasDomainModel; | |
/** | |
* @ORM\Id | |
* @ORM\Column(type="string", length=36) | |
*/ | |
private $id; | |
/** | |
* @ORM\Column(type="string") | |
*/ | |
private $description; | |
/** | |
* @ORM\Column(type="integer") | |
*/ | |
private $maxParticipants; | |
/** | |
* @ORM\ManyToMany(targetEntity="Participant", cascade={"persist"}) | |
*/ | |
private $participants; | |
public function __construct() | |
{ | |
$this->participants = new ArrayCollection(); | |
} | |
public function getId() | |
{ | |
return $this->id; | |
} | |
public function getDescription() | |
{ | |
return $this->description; | |
} | |
public function setDescription($description) | |
{ | |
$this->description = $description; | |
} | |
public function getMaxParticipants() | |
{ | |
return $this->maxParticipants; | |
} | |
public function setMaxParticipants($maxParticipants) | |
{ | |
$this->maxParticipants = $maxParticipants; | |
} | |
public function getParticipants() | |
{ | |
return $this->participants; | |
} | |
public function setParticipants($participants) | |
{ | |
$this->participants = $participants; | |
} | |
private function getDomainModelClass(): string | |
{ | |
return Domain\Program::class; | |
} | |
private function getDomainMapping(): array | |
{ | |
return [ | |
'id' => 'programId', | |
'description' => 'description', | |
'maxParticipants' => 'maxParticipants', | |
]; | |
} | |
private function getPropertyMapper(): array | |
{ | |
return [ | |
'id' => function(Program $entity) { | |
return Domain\ProgramId::fromString($entity->getId()); | |
}, | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment