1
|
|
|
<?php |
2
|
|
|
namespace LunixREST\RequestFactory; |
3
|
|
|
|
4
|
|
|
use LunixREST\RequestFactory\Exceptions\UnableToCreateRequestException; |
5
|
|
|
use LunixREST\RequestFactory\HeaderParser\Exceptions\UnableToParseHeadersException; |
6
|
|
|
use LunixREST\RequestFactory\HeaderParser\HeaderParser; |
7
|
|
|
use LunixREST\RequestFactory\URLParser\Exceptions\UnableToParseURLException; |
8
|
|
|
use LunixREST\RequestFactory\URLParser\URLParser; |
9
|
|
|
use LunixREST\Server\APIRequest\APIRequest; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A generic Request Factory that derives it's behavior from a URLParser and a HeaderParser. Uses the requests Parsed Body. |
14
|
|
|
* Class GenericRequestFactory |
15
|
|
|
* @package LunixREST\RequestFactory |
16
|
|
|
*/ |
17
|
|
|
class GenericRequestFactory implements RequestFactory { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var URLParser |
21
|
|
|
*/ |
22
|
|
|
protected $URLParser; |
23
|
|
|
/** |
24
|
|
|
* @var HeaderParser |
25
|
|
|
*/ |
26
|
|
|
private $headerParser; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* BasicRequestFactory constructor. |
30
|
|
|
* @param URLParser $URLParser |
31
|
4 |
|
* @param HeaderParser $headerParser |
32
|
|
|
*/ |
33
|
4 |
|
public function __construct(URLParser $URLParser, HeaderParser $headerParser) |
34
|
4 |
|
{ |
35
|
4 |
|
$this->URLParser = $URLParser; |
36
|
|
|
$this->headerParser = $headerParser; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Creates a request from raw $data and a $url |
41
|
|
|
* @param ServerRequestInterface $serverRequest |
42
|
|
|
* @return APIRequest |
43
|
2 |
|
* @throws UnableToCreateRequestException |
44
|
|
|
* @throws UnableToParseHeadersException |
45
|
2 |
|
* @throws UnableToParseURLException |
46
|
|
|
*/ |
47
|
|
|
public function create(ServerRequestInterface $serverRequest): APIRequest |
48
|
2 |
|
{ |
49
|
|
|
$parsedURL = $this->URLParser->parse($serverRequest->getUri()); |
50
|
2 |
|
|
51
|
2 |
|
//TODO: Evaluate if this is still needed, as serverRequest allows getting of specific headers |
52
|
2 |
|
$parsedHeaders = $this->headerParser->parse($serverRequest->getHeaders()); |
53
|
|
|
|
54
|
|
|
$urlQueryData = []; |
55
|
2 |
|
if($urlQueryString = $parsedURL->getQueryString()) { |
56
|
2 |
|
parse_str($urlQueryString, $urlQueryData); |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
$apiKey = $parsedURL->getAPIKey(); |
60
|
2 |
|
if($apiKey === null) { |
61
|
2 |
|
$apiKey = $parsedHeaders->getAPIKey(); |
62
|
2 |
|
} |
63
|
|
|
|
64
|
|
|
$acceptableMIMETypes = array_unique(array_merge( |
65
|
2 |
|
$parsedURL->getAcceptableMIMETypes(), |
66
|
2 |
|
$parsedHeaders->getAcceptableMIMETypes() |
67
|
|
|
)); |
68
|
|
|
|
69
|
|
|
return new APIRequest($serverRequest->getMethod(), $parsedURL->getEndpoint(), $parsedURL->getElement(), |
70
|
|
|
$acceptableMIMETypes, $parsedURL->getVersion(), $apiKey, $urlQueryData, $serverRequest->getParsedBody()); |
|
|
|
|
71
|
|
|
} |
72
|
2 |
|
|
73
|
|
|
/** |
74
|
2 |
|
* @return URLParser |
75
|
|
|
*/ |
76
|
|
|
public function getURLParser(): URLParser |
77
|
|
|
{ |
78
|
|
|
return $this->URLParser; |
79
|
|
|
} |
80
|
2 |
|
|
81
|
|
|
/** |
82
|
2 |
|
* @return HeaderParser |
83
|
|
|
*/ |
84
|
|
|
public function getHeaderParser(): HeaderParser |
85
|
|
|
{ |
86
|
|
|
return $this->headerParser; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.