add persisted set ordering
This commit is contained in:
parent
fde12ad561
commit
dc034bf393
9 changed files with 340 additions and 6 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue