findById($id); if (!$comment) { throw new BadRequestException('comment.not_found'); } return $comment; } public function checkParent($id) { $commentRepo = new CommentRepo(); $comment = $commentRepo->findById($id); if (!$comment) { throw new BadRequestException('comment.parent_not_found'); } return $comment; } public function checkToUser($userId) { $userRepo = new UserRepo(); $user = $userRepo->findById($userId); if (!$user) { throw new BadRequestException('comment.to_user_not_found'); } return $user; } public function checkItem($itemId, $itemType) { if (!array_key_exists($itemType, CommentModel::itemTypes())) { throw new BadRequestException('comment.invalid_item_type'); } $result = null; switch ($itemType) { case CommentModel::ITEM_CHAPTER: $validator = new Chapter(); $result = $validator->checkChapter($itemId); break; case CommentModel::ITEM_ARTICLE: $validator = new Article(); $result = $validator->checkArticle($itemId); break; case CommentModel::ITEM_QUESTION: $validator = new Question(); $result = $validator->checkQuestion($itemId); break; case CommentModel::ITEM_ANSWER: $validator = new Answer(); $result = $validator->checkAnswer($itemId); break; } return $result; } public function checkContent($content) { $value = $this->filter->sanitize($content, ['trim', 'string']); $length = kg_strlen($value); if ($length < 1) { throw new BadRequestException('comment.content_too_short'); } if ($length > 1000) { throw new BadRequestException('comment.content_too_long'); } return $value; } public function checkRejectReason($reason) { if (!array_key_exists($reason, ReasonModel::commentRejectOptions())) { throw new BadRequestException('comment.invalid_reject_reason'); } } public function checkPublishStatus($status) { if (!array_key_exists($status, CommentModel::publishTypes())) { throw new BadRequestException('comment.invalid_publish_status'); } return $status; } }