add persisted set ordering

This commit is contained in:
Yisroel Baum 2026-07-31 11:00:41 +03:00
parent fde12ad561
commit dc034bf393
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
9 changed files with 340 additions and 6 deletions

View file

@ -13,6 +13,11 @@ class FakeSetRepository implements SetRepository
*/
private array $setsById = [];
/**
* @var array<int, int>
*/
private array $sortOrdersById = [];
public function create(CreateSetDto $dto): DomainSet
{
$id = count($this->setsById) + 1;
@ -23,6 +28,7 @@ class FakeSetRepository implements SetRepository
iconImageUrl: $dto->iconImageUrl,
);
$this->setsById[$id] = $set;
$this->sortOrdersById[$id] = $this->nextSortOrder();
return $set;
}
@ -38,6 +44,7 @@ class FakeSetRepository implements SetRepository
public function delete(DomainSet $set): void
{
unset($this->setsById[$set->getId()]);
unset($this->sortOrdersById[$set->getId()]);
}
public function find(int $id): ?DomainSet
@ -58,10 +65,35 @@ class FakeSetRepository implements SetRepository
foreach ($this->setsById as $set) {
$sets[] = $this->cloneSet($set);
}
usort($sets, function (
DomainSet $firstSet,
DomainSet $secondSet,
): int {
$firstSortOrder = $this->sortOrdersById[$firstSet->getId()]
?? $firstSet->getId();
$secondSortOrder = $this->sortOrdersById[$secondSet->getId()]
?? $secondSet->getId();
if ($firstSortOrder === $secondSortOrder) {
return $firstSet->getId() <=> $secondSet->getId();
}
return $firstSortOrder <=> $secondSortOrder;
});
return $sets;
}
public function reorder(array $setIds): array
{
$sortOrder = 1;
foreach ($setIds as $setId) {
$this->sortOrdersById[$setId] = $sortOrder;
$sortOrder++;
}
return $this->getAll();
}
private function cloneSet(DomainSet $set): DomainSet
{
return new DomainSet(
@ -71,4 +103,13 @@ class FakeSetRepository implements SetRepository
iconImageUrl: $set->getIconImageUrl(),
);
}
private function nextSortOrder(): int
{
if ($this->sortOrdersById === []) {
return 1;
}
return max($this->sortOrdersById) + 1;
}
}