hyperf-chat/app/Controller/AbstractController.php

56 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2020-11-02 22:45:37 +08:00
<?php
declare(strict_types=1);
2021-09-11 12:41:28 +08:00
2020-11-02 22:45:37 +08:00
/**
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;
2022-01-22 20:08:19 +08:00
use App\Constant\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
/**
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)
{
2021-09-11 12:41:28 +08:00
$validator = di()->get(ValidatorFactoryInterface::class)->make(...$arg);
2020-11-04 11:57:16 +08:00
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
}