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 clear(): void { $this->mangas = []; } }