getNextId(); $comment = new Comment( id: $id, postId: $dto->postId, userId: $dto->userId, body: $dto->body, createdAt: $dto->createdAt, ); $this->existingComments[$id] = $comment; return $this->copy($comment); } public function find(int $id): ?Comment { $comment = $this->existingComments[$id] ?? null; if ($comment === null) { return null; } return $this->copy($comment); } /** * @return Comment[] */ public function findByPostId(int $postId): array { $matching = []; foreach ($this->existingComments as $comment) { if ($comment->getPostId() === $postId) { $matching[] = $this->copy($comment); } } usort( $matching, function (Comment $left, Comment $right) { return $left->getCreatedAt() <=> $right->getCreatedAt(); }, ); return $matching; } public function delete(int $id): void { unset($this->existingComments[$id]); } private function copy(Comment $comment): Comment { return new Comment( id: $comment->getId(), postId: $comment->getPostId(), userId: $comment->getUserId(), body: $comment->getBody(), createdAt: $comment->getCreatedAt(), ); } private function getNextId(): int { return count($this->existingComments) + 1; } }