*/ private array $chapters = []; public function findAll(int $page = 1, int $limit = 20, string $sortBy = 'title', string $sortOrder = 'asc'): array { $sortedMangas = $this->mangas; usort($sortedMangas, function (Manga $a, Manga $b) use ($sortBy, $sortOrder) { $valueA = $this->getPropertyValue($a, $sortBy); $valueB = $this->getPropertyValue($b, $sortBy); return $sortOrder === 'asc' ? $valueA <=> $valueB : $valueB <=> $valueA; }); $offset = ($page - 1) * $limit; return array_slice($sortedMangas, $offset, $limit); } public function count(): int { return count($this->mangas); } public function findById(string $id): ?Manga { foreach ($this->mangas as $manga) { if ($manga->getId()->getValue() === $id) { return $manga; } } return null; } public function save(Manga $manga): void { $this->mangas[] = $manga; } public function delete(Manga $manga): void { $this->mangas = array_filter( $this->mangas, fn(Manga $existingManga) => !$existingManga->getId()->equals($manga->getId()) ); } private function getPropertyValue(Manga $manga, string $property): mixed { return match($property) { 'title' => $manga->getTitle()->getValue(), 'publicationYear' => $manga->getPublicationYear(), default => throw new \InvalidArgumentException("Unknown sort property: $property") }; } public function findChapters(string $mangaId, int $page = 1, int $limit = 20, string $sortOrder = 'desc'): array { if (!isset($this->chapters[$mangaId])) { return []; } $chapters = $this->chapters[$mangaId]; usort($chapters, function (Chapter $a, Chapter $b) use ($sortOrder) { return $sortOrder === 'desc' ? $b->getNumber() <=> $a->getNumber() : $a->getNumber() <=> $b->getNumber(); }); $offset = ($page - 1) * $limit; return array_slice($chapters, $offset, $limit); } public function countChapters(string $mangaId): int { return isset($this->chapters[$mangaId]) ? count($this->chapters[$mangaId]) : 0; } public function addChaptersToManga(string $mangaId, int $count): void { $this->chapters[$mangaId] = []; for ($i = 1; $i <= $count; $i++) { $this->chapters[$mangaId][] = new Chapter( id: new ChapterId((string)$i), mangaId: $mangaId, number: (float)$i, title: "Chapter $i", volume: (int)ceil($i / 10), isVisible: true, createdAt: new \DateTimeImmutable() ); } } public function clear(): void { $this->mangas = []; $this->chapters = []; } }