Skip to content

Instantly share code, notes, and snippets.

@maciejmiara
Created February 25, 2019 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maciejmiara/30da3a238d3d4aeaea1f60c3a26a79d4 to your computer and use it in GitHub Desktop.
Save maciejmiara/30da3a238d3d4aeaea1f60c3a26a79d4 to your computer and use it in GitHub Desktop.
Run participation transformer
<?php
declare(strict_types = 1);
namespace Infrastructure\Eloquent\Transformer;
use Illuminate\Database\Eloquent\Collection;
use Infrastructure\Eloquent\Model\RunParticipation as Entity;
use Domain\Model\RunParticipation as Domain;
class RunParticipationTransformer
{
private $runTransformer;
public function __construct(RunTransformer $runTransformer)
{
$this->runTransformer = $runTransformer;
}
/**
* @throws \Common\Exception\InvalidIdException
* @throws \Domain\Exception\InvalidRunType
*/
public function entityToDomain(Entity $entity): Domain
{
$dbRun = $entity->run()->get()->pop();
$runnerId = \Common\Id::create($entity->runner_id);
$run = $this->runTransformer->entityToDomain($dbRun);
return new Domain($run, $runnerId);
}
/**
* @throws \Common\Exception\InvalidIdException
* @throws \Domain\Exception\InvalidRunType
*/
public function entityToDomainMany(Collection $entities): array
{
$domains = [];
foreach ($entities as $entity) {
$domains[$entity->run_id] = $this->entityToDomain($entity);
}
return $domains;
}
public function domainToEntity(Domain $domain): Entity
{
$run = $domain->getRun();
$entity = new Entity();
$entity->run_id = (string)$run->getId();
$entity->runner_id = (string)$domain->getRunnerId();
return $entity;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment