|
1
|
|
|
<?php |
|
2
|
|
|
namespace LunixREST\RequestFactory; |
|
3
|
|
|
|
|
4
|
|
|
use LunixREST\RequestFactory\HeaderParser\HeaderParser; |
|
5
|
|
|
use LunixREST\RequestFactory\URLParser\Exceptions\InvalidRequestURLException; |
|
6
|
|
|
use LunixREST\RequestFactory\URLParser\URLParser; |
|
7
|
|
|
use LunixREST\Server\APIRequest\APIRequest; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* A generic Request Factory that derives it's behavior from a URLParser and a HeaderParser. Uses the requests Parsed Body. |
|
12
|
|
|
* Class GenericRequestFactory |
|
13
|
|
|
* @package LunixREST\RequestFactory |
|
14
|
|
|
*/ |
|
15
|
|
|
class GenericRequestFactory implements RequestFactory { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var URLParser |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $URLParser; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var HeaderParser |
|
23
|
|
|
*/ |
|
24
|
|
|
private $headerParser; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* BasicRequestFactory constructor. |
|
28
|
|
|
* @param URLParser $URLParser |
|
29
|
|
|
* @param HeaderParser $headerParser |
|
30
|
|
|
*/ |
|
31
|
4 |
|
public function __construct(URLParser $URLParser, HeaderParser $headerParser) |
|
32
|
|
|
{ |
|
33
|
4 |
|
$this->URLParser = $URLParser; |
|
34
|
4 |
|
$this->headerParser = $headerParser; |
|
35
|
4 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Creates a request from raw $data and a $url |
|
39
|
|
|
* @param ServerRequestInterface $serverRequest |
|
40
|
|
|
* @return APIRequest |
|
41
|
|
|
* @throws InvalidRequestURLException |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public function create(ServerRequestInterface $serverRequest): APIRequest |
|
44
|
|
|
{ |
|
45
|
2 |
|
$parsedURL = $this->URLParser->parse($serverRequest->getUri()); |
|
46
|
|
|
|
|
47
|
|
|
//TODO: Evaluate if this is still needed, as serverRequest allows getting of specific headers |
|
48
|
2 |
|
$parsedHeaders = $this->headerParser->parse($serverRequest->getHeaders()); |
|
49
|
|
|
|
|
50
|
2 |
|
$urlQueryData = []; |
|
51
|
2 |
|
if($urlQueryString = $parsedURL->getQueryString()) { |
|
52
|
2 |
|
parse_str($urlQueryString, $urlQueryData); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
$apiKey = $parsedURL->getAPIKey(); |
|
56
|
2 |
|
if($apiKey === null) { |
|
57
|
1 |
|
$apiKey = $parsedHeaders->getAPIKey(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
$acceptableMIMETypes = array_unique(array_merge( |
|
61
|
2 |
|
$parsedURL->getAcceptableMIMETypes(), |
|
62
|
2 |
|
$parsedHeaders->getAcceptableMIMETypes() |
|
63
|
|
|
)); |
|
64
|
|
|
|
|
65
|
2 |
|
return new APIRequest($serverRequest->getMethod(), $parsedURL->getEndpoint(), $parsedURL->getElement(), |
|
66
|
2 |
|
$acceptableMIMETypes, $parsedURL->getVersion(), $apiKey, $urlQueryData, $serverRequest->getParsedBody()); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return URLParser |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function getURLParser(): URLParser |
|
73
|
|
|
{ |
|
74
|
2 |
|
return $this->URLParser; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return HeaderParser |
|
79
|
|
|
*/ |
|
80
|
2 |
|
public function getHeaderParser(): HeaderParser |
|
81
|
|
|
{ |
|
82
|
2 |
|
return $this->headerParser; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
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.