|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\RequestModel; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use ReflectionClass; |
|
9
|
|
|
use ReflectionException; |
|
10
|
|
|
use ReflectionNamedType; |
|
11
|
|
|
use ReflectionParameter; |
|
12
|
|
|
use Yiisoft\Injector\Injector; |
|
13
|
|
|
use Yiisoft\Router\CurrentRoute; |
|
14
|
|
|
use Yiisoft\Validator\RulesProviderInterface; |
|
15
|
|
|
use Yiisoft\Validator\ValidatorInterface; |
|
16
|
|
|
|
|
17
|
|
|
final class RequestModelFactory |
|
18
|
|
|
{ |
|
19
|
|
|
private Injector $injector; |
|
20
|
|
|
private ValidatorInterface $validator; |
|
21
|
|
|
private CurrentRoute $currentRoute; |
|
22
|
|
|
|
|
23
|
16 |
|
public function __construct(ValidatorInterface $validator, Injector $injector, CurrentRoute $currentRoute) |
|
24
|
|
|
{ |
|
25
|
16 |
|
$this->validator = $validator; |
|
26
|
16 |
|
$this->injector = $injector; |
|
27
|
16 |
|
$this->currentRoute = $currentRoute; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param ServerRequestInterface $request |
|
32
|
|
|
* @param array|ReflectionParameter[] $handlerParams |
|
33
|
|
|
* |
|
34
|
|
|
* @throws ReflectionException |
|
35
|
|
|
* |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
10 |
|
public function createInstances(ServerRequestInterface $request, array $handlerParams): array |
|
39
|
|
|
{ |
|
40
|
10 |
|
$requestModelInstances = []; |
|
41
|
10 |
|
foreach ($this->getModelRequestClasses($handlerParams) as $modelClass) { |
|
42
|
9 |
|
$requestModelInstances[] = $this->processModel($request, $this->injector->make($modelClass)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
9 |
|
return $requestModelInstances; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
9 |
|
private function processModel(ServerRequestInterface $request, RequestModelInterface $model): RequestModelInterface |
|
49
|
|
|
{ |
|
50
|
9 |
|
$requestData = $this->getRequestData($request); |
|
51
|
9 |
|
$model->setRequestData($requestData); |
|
52
|
9 |
|
if ($model instanceof RulesProviderInterface) { |
|
53
|
2 |
|
$result = $this->validator->validate($model, $model->getRules()); |
|
54
|
2 |
|
if (!$result->isValid()) { |
|
55
|
1 |
|
throw new RequestValidationException($result->getErrorMessagesIndexedByAttribute()); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
8 |
|
return $model; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param array|ReflectionParameter[] $handlerParams |
|
64
|
|
|
* |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
10 |
|
private function getModelRequestClasses(array $handlerParams): array |
|
68
|
|
|
{ |
|
69
|
10 |
|
$modelClasses = []; |
|
70
|
10 |
|
foreach ($handlerParams as $param) { |
|
71
|
10 |
|
if (!$this->paramsIsRequestModel($param)) { |
|
72
|
1 |
|
continue; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** @var ReflectionNamedType $type */ |
|
76
|
9 |
|
$type = $param->getType(); |
|
77
|
9 |
|
$modelClasses[] = $type->getName(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
10 |
|
return $modelClasses; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
10 |
|
private function paramsIsRequestModel(ReflectionParameter $param): bool |
|
84
|
|
|
{ |
|
85
|
10 |
|
if (!$param->hasType()) { |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** @var ReflectionNamedType $type */ |
|
90
|
10 |
|
$type = $param->getType(); |
|
91
|
10 |
|
if ($type->isBuiltin()) { |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
10 |
|
return (new ReflectionClass($type->getName()))->implementsInterface(RequestModelInterface::class); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
9 |
|
private function getRequestData(ServerRequestInterface $request): array |
|
99
|
|
|
{ |
|
100
|
|
|
return [ |
|
101
|
9 |
|
'query' => $request->getQueryParams(), |
|
102
|
9 |
|
'body' => $request->getParsedBody(), |
|
103
|
9 |
|
'attributes' => $request->getAttributes(), |
|
104
|
9 |
|
'headers' => $request->getHeaders(), |
|
105
|
9 |
|
'files' => $request->getUploadedFiles(), |
|
106
|
9 |
|
'cookie' => $request->getCookieParams(), |
|
107
|
9 |
|
'router' => $this->currentRoute->getArguments(), |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|