56 lines
1.2 KiB
PHP
56 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\Fakes;
|
|
|
|
use App\Set\CreateSetDto;
|
|
use App\Set\Set;
|
|
use App\Set\SetRepository;
|
|
|
|
class FakeSetRepository implements SetRepository
|
|
{
|
|
/**
|
|
* @var array<int, Set>
|
|
*/
|
|
private array $sets = [];
|
|
|
|
public function create(CreateSetDto $dto): Set
|
|
{
|
|
$id = count($this->sets) + 1;
|
|
$set = new Set(
|
|
id: $id,
|
|
name: $dto->name,
|
|
creator: $dto->creator,
|
|
);
|
|
$this->sets[$id] = $set;
|
|
|
|
return $this->copy($set);
|
|
}
|
|
|
|
public function find(int $id): ?Set
|
|
{
|
|
$set = $this->sets[$id] ?? null;
|
|
|
|
return $set === null ? null : $this->copy($set);
|
|
}
|
|
|
|
public function all(): array
|
|
{
|
|
$sets = array_values($this->sets);
|
|
usort($sets, function (Set $first, Set $second): int {
|
|
return $first->getName() <=> $second->getName();
|
|
});
|
|
|
|
return array_map(function (Set $set): Set {
|
|
return $this->copy($set);
|
|
}, $sets);
|
|
}
|
|
|
|
private function copy(Set $set): Set
|
|
{
|
|
return new Set(
|
|
id: $set->getId(),
|
|
name: $set->getName(),
|
|
creator: $set->getCreator(),
|
|
);
|
|
}
|
|
}
|