*/ public array $images = []; public bool $failOnSave = false; private int $nextId = 1; public function findForTask(int $taskId, int $imageId): ?TaskImage { $image = $this->images[$imageId] ?? null; return $image !== null && $image->taskId() === $taskId ? clone $image : null; } public function filePathsOfTask(int $taskId): array { $paths = []; foreach ($this->images as $image) { if ($image->taskId() === $taskId) { array_push($paths, ...$image->file()->paths()); } } return $paths; } public function save(TaskImage $image): TaskImage { if ($this->failOnSave) { throw new \RuntimeException('Saving failed.'); } $id = $image->id() ?? $this->nextId++; $this->images[$id] = TaskImage::reconstitute($id, $image->taskId(), $image->uploaderId(), $image->file(), $image->originalName(), $image->caption(), $image->uploadedAt()); return clone $this->images[$id]; } public function delete(TaskImage $image): void { unset($this->images[$image->id()]); } }