Phase 1, item 3: the hierarchy customer jobs, tasks and time entries will refer to. - New Catalog module: service unit > optional service > work type. Names are unique per level (case-insensitive); items are never deleted, only deactivated, and foreign keys restrict deletes. - Pohjatiedot page (/catalog, admins and management): add, rename, reorder with move up/down buttons (keyboard-friendly, focus kept and announced) and an "in use" checkbox per row. Work types can move to another service of the same unit. - A migration inserts the starting catalog from the specification when the tables are empty, so every installation has it. - CatalogQueries gives other modules the active service units and the work types of chosen units plus the shared ones. - Spec and CLAUDE.md updated. Co-Authored-By: Claude Opus 5.5 <[email protected]>
94 lines
2.9 KiB
PHP
94 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Fakes;
|
|
|
|
use App\Modules\Catalog\Domain\Contracts\ServiceRepository;
|
|
use App\Modules\Catalog\Domain\Entities\CatalogItem;
|
|
use App\Modules\Catalog\Domain\Entities\Service;
|
|
use App\Modules\Catalog\Domain\Enums\MoveDirection;
|
|
|
|
class InMemoryServiceRepository implements ServiceRepository
|
|
{
|
|
/** @var array<int, Service> */
|
|
public array $items = [];
|
|
|
|
private int $nextId = 1;
|
|
|
|
public function findById(int $id): ?Service
|
|
{
|
|
return isset($this->items[$id]) ? clone $this->items[$id] : null;
|
|
}
|
|
|
|
public function save(Service $service): Service
|
|
{
|
|
$id = $service->id() ?? $this->nextId++;
|
|
$this->items[$id] = Service::reconstitute($id, $service->serviceUnitId(), $service->name(), $service->isActive(), $service->sortOrder());
|
|
|
|
return clone $this->items[$id];
|
|
}
|
|
|
|
public function isNameTaken(int $serviceUnitId, string $name, ?int $exceptId = null): bool
|
|
{
|
|
return self::nameTakenIn($this->siblings($serviceUnitId), $name, $exceptId);
|
|
}
|
|
|
|
public function nextSortOrder(int $serviceUnitId): int
|
|
{
|
|
return self::nextIn($this->siblings($serviceUnitId));
|
|
}
|
|
|
|
public function neighbour(Service $service, MoveDirection $direction): ?Service
|
|
{
|
|
return self::neighbourIn($this->siblings($service->serviceUnitId()), $service, $direction);
|
|
}
|
|
|
|
/**
|
|
* @return array<int, Service>
|
|
*/
|
|
private function siblings(int $serviceUnitId): array
|
|
{
|
|
return array_filter($this->items, fn (Service $service): bool => $service->serviceUnitId() === $serviceUnitId);
|
|
}
|
|
|
|
/**
|
|
* @template T of CatalogItem
|
|
*
|
|
* @param array<int, T> $siblings
|
|
* @return T|null
|
|
*/
|
|
private static function neighbourIn(array $siblings, CatalogItem $item, MoveDirection $direction): ?CatalogItem
|
|
{
|
|
$candidates = array_filter($siblings, fn (CatalogItem $sibling): bool => $direction === MoveDirection::Up
|
|
? $sibling->sortOrder() < $item->sortOrder()
|
|
: $sibling->sortOrder() > $item->sortOrder());
|
|
|
|
usort($candidates, fn (CatalogItem $a, CatalogItem $b): int => $direction === MoveDirection::Up
|
|
? $b->sortOrder() <=> $a->sortOrder()
|
|
: $a->sortOrder() <=> $b->sortOrder());
|
|
|
|
return isset($candidates[0]) ? clone $candidates[0] : null;
|
|
}
|
|
|
|
/**
|
|
* @param array<int, CatalogItem> $siblings
|
|
*/
|
|
private static function nameTakenIn(array $siblings, string $name, ?int $exceptId): bool
|
|
{
|
|
foreach ($siblings as $id => $sibling) {
|
|
if ($id !== $exceptId && mb_strtolower($sibling->name()->value) === mb_strtolower($name)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @param array<int, CatalogItem> $siblings
|
|
*/
|
|
private static function nextIn(array $siblings): int
|
|
{
|
|
return array_reduce($siblings, fn (int $max, CatalogItem $item): int => max($max, $item->sortOrder()), 0) + 1;
|
|
}
|
|
}
|