hyperf-chat/app/Controller/AbstractController.php

63 lines
1.3 KiB
PHP
Raw Normal View History

2020-11-02 22:45:37 +08:00
<?php
declare(strict_types=1);
2020-11-04 11:57:16 +08:00
2020-11-02 22:45:37 +08:00
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
2020-11-04 16:47:17 +08:00
2020-11-02 22:45:37 +08:00
namespace App\Controller;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Psr\Container\ContainerInterface;
2020-11-04 11:57:16 +08:00
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use App\Exception\ValidateException;
use App\Constants\ResponseCode;
2020-11-02 22:45:37 +08:00
abstract class AbstractController
{
/**
* @Inject
* @var ContainerInterface
*/
protected $container;
/**
* @Inject
* @var RequestInterface
*/
protected $request;
/**
* @Inject
* @var ResponseInterface
*/
protected $response;
2020-11-04 11:57:16 +08:00
/**
* @Inject
* @var ValidatorFactoryInterface
*/
protected $validationFactory;
/**
* 自定义验证器
*
* @param mixed ...$arg
*/
2020-11-04 16:47:17 +08:00
protected function validate(...$arg)
{
2020-11-04 11:57:16 +08:00
$validator = $this->validationFactory->make(...$arg);
if ($validator->fails()) {
2020-11-04 16:47:17 +08:00
throw new ValidateException($validator->errors()->first(), ResponseCode::VALIDATION_ERROR);
2020-11-04 11:57:16 +08:00
}
}
2020-11-02 22:45:37 +08:00
}