1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-24 04:01:31 +08:00
koogua 0336a54911 1.源文件增加版权信息
2.群组状态和课程协同
2021-06-13 15:49:47 +08:00

122 lines
3.0 KiB
PHP

<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Validators;
use App\Exceptions\BadRequest as BadRequestException;
use App\Models\Comment as CommentModel;
use App\Models\Reason as ReasonModel;
use App\Repos\Comment as CommentRepo;
use App\Repos\User as UserRepo;
class Comment extends Validator
{
public function checkComment($id)
{
$commentRepo = new CommentRepo();
$comment = $commentRepo->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;
}
}