Repository navigation
Expand file tree
/
Copy pathSwaggerRequest.php
More file actions
84 lines (72 loc) · 2.32 KB
/
Copy pathSwaggerRequest.php
File metadata and controls
84 lines (72 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Swagger\Request;
use Hyperf\Context\RequestContext;
use Hyperf\HttpServer\CoreMiddleware;
use Hyperf\HttpServer\Router\Dispatched;
use Hyperf\Swagger\Exception\RuntimeException;
use Hyperf\Validation\Request\FormRequest;
class SwaggerRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
$callback = $this->getCallbackByContext();
return ValidationCollector::get($callback[0], $callback[1], 'rules');
}
public function attributes(): array
{
$callback = $this->getCallbackByContext();
return ValidationCollector::get($callback[0], $callback[1], 'attribute');
}
protected function getCallbackByContext(): array
{
/** @var null|Dispatched $dispatched */
$dispatched = RequestContext::getOrNull()?->getAttribute(Dispatched::class);
if (! $dispatched) {
throw new RuntimeException('The request is invalid.');
}
$callback = $dispatched->handler?->callback;
if (! $callback) {
throw new RuntimeException('The SwaggerRequest is only used by swagger annotations.');
}
return $this->prepareHandler($callback);
}
/**
* @see CoreMiddleware::prepareHandler()
*/
protected function prepareHandler(array|string $handler): array
{
if (is_string($handler)) {
if (str_contains($handler, '@')) {
return explode('@', $handler);
}
$array = explode('::', $handler);
if (! isset($array[1]) && class_exists($handler) && method_exists($handler, '__invoke')) {
$array[1] = '__invoke';
}
return [$array[0], $array[1] ?? null];
}
if (is_array($handler) && isset($handler[0], $handler[1])) {
return $handler;
}
throw new RuntimeException('Handler not exist.');
}
}