Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tsh-code
Forked from maciejmiara/RunnerTransformer.php
Created March 11, 2019 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsh-code/d78287898fdc919f7246129a9a829d4d to your computer and use it in GitHub Desktop.
Save tsh-code/d78287898fdc919f7246129a9a829d4d to your computer and use it in GitHub Desktop.
Runner model transformer
<?php
declare(strict_types = 1);
namespace Infrastructure\Eloquent\Transformer;
use Infrastructure\Eloquent\Model\Runner as Entity;
use Domain\Model\Runner as Domain;
class RunnerTransformer
{
private $runParticipationTransformer;
private $runResultTransformer;
public function __construct(
RunParticipationTransformer $runParticipationTransformer,
RunResultTransformer $runResultTransformer
) {
$this->runParticipationTransformer = $runParticipationTransformer;
$this->runResultTransformer = $runResultTransformer;
}
/**
* @throws \Common\Exception\InvalidIdException
* @throws \Domain\Exception\InvalidRunType
*/
public function entityToDomain(Entity $entity): Domain
{
$dbParticipations = $entity->participations()->get();
$dbResults = $entity->results()->get();
$runnerId = \Common\Id::create($entity->id);
$participations = $this->runParticipationTransformer->entityToDomainMany($dbParticipations);
$results = $this->runResultTransformer->entityToDomainMany($dbResults);
return new Domain($runnerId, $entity->email, $entity->password, $participations, $results);
}
public function domainToEntity(Domain $domain): Entity
{
$entity = new Entity();
$entity->id = (string)$domain->getId();
$entity->email = $domain->getEmail();
$entity->password = $domain->getPassword();
return $entity;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment