-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
ActionRequestFactory.php
94 lines (82 loc) · 3.2 KB
/
ActionRequestFactory.php
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
85
86
87
88
89
90
91
92
93
94
<?php
namespace Neos\Flow\Mvc;
use Neos\Flow\Http\Helper\UploadedFilesHelper;
use Neos\Utility\Arrays;
use Psr\Http\Message\ServerRequestInterface;
/**
* Creates an ActionRequest from a PSR-7 http request and sets appropriate defaults.
*/
class ActionRequestFactory
{
/**
* @param ServerRequestInterface $httpRequest
* @param array $additionalArguments
* @return ActionRequest
* @throws Exception\InvalidActionNameException
* @throws Exception\InvalidArgumentNameException
* @throws Exception\InvalidArgumentTypeException
* @throws Exception\InvalidControllerNameException
*/
public function createActionRequest(ServerRequestInterface $httpRequest, array $additionalArguments = []): ActionRequest
{
$arguments = $this->mergeHttpRequestArguments($httpRequest);
$arguments = $this->mergeHttpRequestArgumentsWithAdditionalArguments($arguments, $additionalArguments);
$actionRequest = $this->prepareActionRequest($httpRequest);
$actionRequest->setArguments($arguments);
$actionRequest = $this->setDefaultControllerAndActionNameIfNoneSpecified($actionRequest);
return $actionRequest;
}
/**
* @param ServerRequestInterface $httpRequest
* @return array
*/
protected function mergeHttpRequestArguments(ServerRequestInterface $httpRequest): array
{
$arguments = $httpRequest->getQueryParams();
$parsedBody = $httpRequest->getParsedBody();
if (is_array($parsedBody) && $parsedBody !== []) {
$arguments = Arrays::arrayMergeRecursiveOverrule($arguments, $parsedBody);
}
$uploadedFiles = UploadedFilesHelper::upcastUploadedFiles($httpRequest->getUploadedFiles(), $arguments);
$arguments = Arrays::arrayMergeRecursiveOverrule($arguments, $uploadedFiles);
return $arguments;
}
/**
* @param array $arguments
* @param array $additionalArguments
* @return array
*/
protected function mergeHttpRequestArgumentsWithAdditionalArguments(array $arguments, array $additionalArguments): array
{
if (!empty($additionalArguments)) {
$arguments = Arrays::arrayMergeRecursiveOverrule($arguments, $additionalArguments);
}
return $arguments;
}
/**
* @param ServerRequestInterface $httpRequest
* @return ActionRequest
*/
protected function prepareActionRequest(ServerRequestInterface $httpRequest): ActionRequest
{
return ActionRequest::fromHttpRequest($httpRequest);
}
/**
* Set the default controller and action names if none has been specified.
*
* @param ActionRequest $actionRequest
* @return ActionRequest
* @throws Exception\InvalidActionNameException
* @throws Exception\InvalidControllerNameException
*/
protected function setDefaultControllerAndActionNameIfNoneSpecified(ActionRequest $actionRequest): ActionRequest
{
if ($actionRequest->getControllerName() === '') {
$actionRequest->setControllerName('Standard');
}
if ($actionRequest->getControllerActionName() === '') {
$actionRequest->setControllerActionName('index');
}
return $actionRequest;
}
}