hyperf-chat/app/Controller/AbstractController.php

63 lines
1.4 KiB
PHP
Raw Normal View History

2020-11-02 22:45:37 +08:00
<?php
declare(strict_types=1);
/**
*
2020-12-26 21:33:40 +08:00
* This is my open source code, please do not use it for commercial applications.
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code
*
* @author Yuandong<837215079@qq.com>
* @link https://github.com/gzydong/hyperf-chat
2020-11-02 22:45:37 +08:00
*/
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;
/**
2020-11-29 14:44:11 +08:00
* 自定义控制器验证器
2020-11-04 11:57:16 +08:00
*
* @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
}