1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace POData\OperationContext\Web\Illuminate; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use POData\OperationContext\HTTPRequestMethod; |
7
|
|
|
use POData\OperationContext\IHTTPRequest; |
8
|
|
|
|
9
|
|
|
class IncomingIlluminateRequest implements IHTTPRequest |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The Illuminate request. |
13
|
|
|
* |
14
|
|
|
* @var Request |
15
|
|
|
*/ |
16
|
|
|
private $request; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The request headers. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $headers = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The incoming url in raw format. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $rawUrl = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The request method (GET, POST, PUT, DELETE or MERGE). |
34
|
|
|
* |
35
|
|
|
* @var HTTPRequestMethod HttpVerb |
36
|
|
|
*/ |
37
|
|
|
private $method; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The query options as key value. |
41
|
|
|
* |
42
|
|
|
* @var array(string, string); |
43
|
|
|
*/ |
44
|
|
|
private $queryOptions = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* A collection that represents mapping between query |
48
|
|
|
* option and its count. |
49
|
|
|
* |
50
|
|
|
* @var array(string, int) |
51
|
|
|
*/ |
52
|
|
|
private $queryOptionsCount = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* IncomingIlluminateRequest constructor. |
56
|
|
|
* |
57
|
|
|
* @param Request $request |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function __construct(Request $request) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$this->request = $request; |
62
|
|
|
$this->headers = []; |
63
|
|
|
$this->queryOptions = []; |
64
|
|
|
$this->queryOptionsCount = []; |
65
|
|
|
$this->method = new HTTPRequestMethod($this->request->getMethod()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string RequestURI called by User with the value of QueryString |
70
|
|
|
*/ |
71
|
|
|
public function getRawUrl() |
72
|
|
|
{ |
73
|
|
|
$this->rawUrl = $this->request->fullUrl(); |
74
|
|
|
|
75
|
|
|
return $this->rawUrl; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $key The header name |
80
|
|
|
* |
81
|
|
|
* @return array|null|string |
82
|
|
|
*/ |
83
|
|
|
public function getRequestHeader($key) |
84
|
|
|
{ |
85
|
|
|
$result = $this->request->header($key); |
|
|
|
|
86
|
|
|
//Zend returns false for a missing header...POData needs a null |
87
|
|
|
if (false === $result || '' === $result) { |
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $result; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns the Query String Parameters (QSPs) as an array of KEY-VALUE pairs. If a QSP appears twice |
96
|
|
|
* it will have two entries in this array. |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public function getQueryParameters() |
101
|
|
|
{ |
102
|
|
|
//TODO: the contract is more specific than this, it requires the name and values to be decoded |
|
|
|
|
103
|
|
|
//not sure how to test that... |
104
|
|
|
//Have to convert to the stranger format known to POData that deals with multiple query strings. |
105
|
|
|
//this makes this request a bit non compliant as it doesn't expose duplicate keys, something POData will |
106
|
|
|
//check for instead whatever parameter was last in the query string is set. IE |
107
|
|
|
//odata.svc/?$format=xml&$format=json the format will be json |
108
|
|
|
$this->queryOptions = []; |
109
|
|
|
$this->queryOptionsCount = []; |
110
|
|
|
|
111
|
|
|
foreach ($this->request->all() as $key => $value) { |
112
|
|
|
$keyBitz = explode(';', $key); |
113
|
|
|
$newKey = strtolower($keyBitz[count($keyBitz) - 1]); |
114
|
|
|
$this->queryOptions[] = [$newKey => $value]; |
115
|
|
|
if (!array_key_exists($key, $this->queryOptionsCount)) { |
116
|
|
|
$this->queryOptionsCount[$newKey] = 0; |
117
|
|
|
} |
118
|
|
|
$this->queryOptionsCount[$newKey]++; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this->queryOptions; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return HTTPRequestMethod |
126
|
|
|
*/ |
127
|
|
|
public function getMethod() |
128
|
|
|
{ |
129
|
|
|
return $this->method; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return array|mixed |
|
|
|
|
134
|
|
|
*/ |
135
|
|
|
public function getAllInput() |
136
|
|
|
{ |
137
|
|
|
$content = $this->request->all(); |
138
|
|
|
return !empty($content) ? $content : $this->request->getContent(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|