1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-07-06 17:01:11 +08:00
2019-12-13 00:10:10 +08:00

88 lines
1.9 KiB
PHP

<?php
namespace App\Validators;
use App\Exceptions\BadRequest as BadRequestException;
use App\Exceptions\NotFound as NotFoundException;
use App\Repos\Package as PackageRepo;
class Package extends Validator
{
/**
* @param integer $id
* @return \App\Models\Package
* @throws NotFoundException
*/
public function checkPackage($id)
{
$packageRepo = new PackageRepo();
$package = $packageRepo->findById($id);
if (!$package) {
throw new NotFoundException('package.not_found');
}
return $package;
}
public function checkTitle($title)
{
$value = $this->filter->sanitize($title, ['trim', 'string']);
$length = kg_strlen($value);
if ($length < 2) {
throw new BadRequestException('package.title_too_short');
}
if ($length > 50) {
throw new BadRequestException('package.title_too_long');
}
return $value;
}
public function checkSummary($summary)
{
$value = $this->filter->sanitize($summary, ['trim', 'string']);
return $value;
}
public function checkMarketPrice($price)
{
$value = $this->filter->sanitize($price, ['trim', 'float']);
if ($value < 0.01 || $value > 10000) {
throw new BadRequestException('package.invalid_market_price');
}
return $value;
}
public function checkVipPrice($price)
{
$value = $this->filter->sanitize($price, ['trim', 'float']);
if ($value < 0.01 || $value > 10000) {
throw new BadRequestException('package.invalid_vip_price');
}
return $value;
}
public function checkPublishStatus($status)
{
$value = $this->filter->sanitize($status, ['trim', 'int']);
if (!in_array($value, [0, 1])) {
throw new BadRequestException('package.invalid_publish_status');
}
return $value;
}
}