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