1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package WPEmerge |
4
|
|
|
* @author Atanas Angelov <[email protected]> |
5
|
|
|
* @copyright 2017-2019 Atanas Angelov |
6
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
7
|
|
|
* @link https://wpemerge.com/ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace WPEmerge\Requests; |
11
|
|
|
|
12
|
|
|
use GuzzleHttp\Psr7\ServerRequest; |
13
|
|
|
use WPEmerge\Support\Arr; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A representation of a request to the server. |
17
|
|
|
*/ |
18
|
|
|
class Request extends ServerRequest implements RequestInterface { |
19
|
|
|
/** |
20
|
|
|
* @codeCoverageIgnore |
21
|
|
|
* {@inheritDoc} |
22
|
|
|
* @return static |
23
|
|
|
*/ |
24
|
|
|
public static function fromGlobals() { |
25
|
|
|
$request = parent::fromGlobals(); |
26
|
|
|
$new = new self( |
27
|
|
|
$request->getMethod(), |
28
|
|
|
$request->getUri(), |
29
|
|
|
$request->getHeaders(), |
30
|
|
|
$request->getBody(), |
31
|
|
|
$request->getProtocolVersion(), |
32
|
|
|
$request->getServerParams() |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
return $new |
36
|
|
|
->withCookieParams( $_COOKIE ) |
37
|
|
|
->withQueryParams( $_GET ) |
38
|
|
|
->withParsedBody( $_POST ) |
39
|
|
|
->withUploadedFiles( static::normalizeFiles( $_FILES ) ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @codeCoverageIgnore |
44
|
|
|
* {@inheritDoc} |
45
|
|
|
*/ |
46
|
|
|
public function getUrl() { |
47
|
|
|
return $this->getUri(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritDoc} |
52
|
|
|
*/ |
53
|
1 |
|
protected function getMethodOverride( $default ) { |
54
|
1 |
|
$valid_overrides = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']; |
55
|
1 |
|
$override = $default; |
56
|
|
|
|
57
|
1 |
|
$header_override = (string) $this->getHeaderLine( 'X-HTTP-METHOD-OVERRIDE' ); |
58
|
1 |
|
if ( ! empty( $header_override ) ) { |
59
|
1 |
|
$override = strtoupper( $header_override ); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
$body_override = (string) $this->body( '_method', '' ); |
63
|
1 |
|
if ( ! empty( $body_override ) ) { |
64
|
1 |
|
$override = strtoupper( $body_override ); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
if ( in_array( $override, $valid_overrides, true ) ) { |
68
|
1 |
|
return $override; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return $default; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritDoc} |
76
|
|
|
*/ |
77
|
1 |
|
public function getMethod() { |
78
|
1 |
|
$method = parent::getMethod(); |
79
|
|
|
|
80
|
1 |
|
if ( $method === 'POST' ) { |
81
|
1 |
|
$method = $this->getMethodOverride( $method ); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
return $method; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritDoc} |
89
|
|
|
*/ |
90
|
1 |
|
public function isGet() { |
91
|
1 |
|
return $this->getMethod() === 'GET'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
*/ |
97
|
1 |
|
public function isHead() { |
98
|
1 |
|
return $this->getMethod() === 'HEAD'; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritDoc} |
103
|
|
|
*/ |
104
|
1 |
|
public function isPost() { |
105
|
1 |
|
return $this->getMethod() === 'POST'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritDoc} |
110
|
|
|
*/ |
111
|
1 |
|
public function isPut() { |
112
|
1 |
|
return $this->getMethod() === 'PUT'; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritDoc} |
117
|
|
|
*/ |
118
|
1 |
|
public function isPatch() { |
119
|
1 |
|
return $this->getMethod() === 'PATCH'; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritDoc} |
124
|
|
|
*/ |
125
|
1 |
|
public function isDelete() { |
126
|
1 |
|
return $this->getMethod() === 'DELETE'; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritDoc} |
131
|
|
|
*/ |
132
|
1 |
|
public function isOptions() { |
133
|
1 |
|
return $this->getMethod() === 'OPTIONS'; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritDoc} |
138
|
|
|
*/ |
139
|
1 |
|
public function isReadVerb() { |
140
|
1 |
|
return in_array( $this->getMethod(), ['GET', 'HEAD', 'OPTIONS'] ); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritDoc} |
145
|
|
|
*/ |
146
|
1 |
|
public function isAjax() { |
147
|
1 |
|
return strtolower( $this->getHeaderLine( 'X-Requested-With' ) ) === 'xmlhttprequest'; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get all values or a single one from an input type. |
152
|
|
|
* |
153
|
|
|
* @param array $source |
154
|
|
|
* @param string $key |
155
|
|
|
* @param mixed $default |
156
|
|
|
* @return mixed |
157
|
|
|
*/ |
158
|
4 |
|
protected function get( $source, $key = '', $default = null ) { |
159
|
4 |
|
if ( empty( $key ) ) { |
160
|
1 |
|
return $source; |
161
|
|
|
} |
162
|
|
|
|
163
|
3 |
|
return Arr::get( $source, $key, $default ); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritDoc} |
168
|
|
|
* @see ::get() |
169
|
|
|
*/ |
170
|
4 |
|
public function attributes( $key = '', $default = null ) { |
171
|
4 |
|
return call_user_func( [$this, 'get'], $this->getAttributes(), $key, $default ); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritDoc} |
176
|
|
|
* @see ::get() |
177
|
|
|
*/ |
178
|
4 |
|
public function query( $key = '', $default = null ) { |
179
|
4 |
|
return call_user_func( [$this, 'get'], $this->getQueryParams(), $key, $default ); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritDoc} |
184
|
|
|
* @see ::get() |
185
|
|
|
*/ |
186
|
4 |
|
public function body( $key = '', $default = null ) { |
187
|
4 |
|
return call_user_func( [$this, 'get'], $this->getParsedBody(), $key, $default ); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritDoc} |
192
|
|
|
* @see ::get() |
193
|
|
|
*/ |
194
|
4 |
|
public function cookies( $key = '', $default = null ) { |
195
|
4 |
|
return call_user_func( [$this, 'get'], $this->getCookieParams(), $key, $default ); |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* {@inheritDoc} |
200
|
|
|
* @see ::get() |
201
|
|
|
*/ |
202
|
4 |
|
public function files( $key = '', $default = null ) { |
203
|
4 |
|
return call_user_func( [$this, 'get'], $this->getUploadedFiles(), $key, $default ); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* {@inheritDoc} |
208
|
|
|
* @see ::get() |
209
|
|
|
*/ |
210
|
4 |
|
public function server( $key = '', $default = null ) { |
211
|
4 |
|
return call_user_func( [$this, 'get'], $this->getServerParams(), $key, $default ); |
|
|
|
|
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* {@inheritDoc} |
216
|
|
|
* @see ::get() |
217
|
|
|
*/ |
218
|
4 |
|
public function headers( $key = '', $default = null ) { |
219
|
4 |
|
return call_user_func( [$this, 'get'], $this->getHeaders(), $key, $default ); |
|
|
|
|
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|