Passed
Push — master ( f7f26a...2fd308 )
by Atanas
02:20
created

Request::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 2
c 0
b 0
f 0
cc 1
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace WPEmerge\Requests;
4
5
use WPEmerge\Helpers\Url;
6
use WPEmerge\Support\Arr;
7
8
/**
9
 * A server request representation
10
 */
11
class Request {
12
	/**
13
	 * GET parameters.
14
	 *
15
	 * @var array
16
	 */
17
	protected $get = [];
18
19
	/**
20
	 * POST parameters.
21
	 *
22
	 * @var array
23
	 */
24
	protected $post = [];
25
26
	/**
27
	 * COOKIE parameters.
28
	 *
29
	 * @var array
30
	 */
31
	protected $cookie = [];
32
33
	/**
34
	 * FILES parameters.
35
	 *
36
	 * @var array
37
	 */
38
	protected $files = [];
39
40
	/**
41
	 * SERVER parameters.
42
	 *
43
	 * @var array
44
	 */
45
	protected $server = [];
46
47
	/**
48
	 * Headers.
49
	 *
50
	 * @var array
51
	 */
52
	protected $headers = [];
53
54
	/**
55
	 * Create a new instance from php superglobals.
56
	 *
57
	 * @return Request
58
	 */
59 1
	public static function fromGlobals() {
60 1
		return new static( stripslashes_deep( $_GET ), stripslashes_deep( $_POST ), $_COOKIE, $_FILES, $_SERVER, getallheaders() );
61
	}
62
63
	/**
64
	 * Constructor.
65
	 *
66
	 * @param array $get
67
	 * @param array $post
68
	 * @param array $cookie
69
	 * @param array $files
70
	 * @param array $server
71
	 * @param array $headers
72
	 */
73 1
	public function __construct( $get, $post, $cookie, $files, $server, $headers ) {
74 1
		$this->get = $get;
75 1
		$this->post = $post;
76 1
		$this->cookie = $cookie;
77 1
		$this->files = $files;
78 1
		$this->server = $server;
79 1
		$this->headers = $headers;
80 1
	}
81
82
	/**
83
	 * Get the request method.
84
	 *
85
	 * @return string
86
	 */
87 1
	public function getMethod() {
88 1
		$method = (string) $this->server( 'REQUEST_METHOD', 'GET' );
89
90 1
		$override = (string) $this->headers( 'X-HTTP-METHOD-OVERRIDE' );
91 1
		if ( $method === 'POST' && $override ) {
92 1
			$method = $override;
93 1
		}
94
95 1
		return strtoupper( $method );
96
	}
97
98
	/**
99
	 * Check if the request method is GET.
100
	 *
101
	 * @return boolean
102
	 */
103 1
	public function isGet() {
104 1
		return $this->getMethod() === 'GET';
105
	}
106
107
	/**
108
	 * Check if the request method is HEAD.
109
	 *
110
	 * @return boolean
111
	 */
112 1
	public function isHead() {
113 1
		return $this->getMethod() === 'HEAD';
114
	}
115
116
	/**
117
	 * Check if the request method is POST.
118
	 *
119
	 * @return boolean
120
	 */
121 1
	public function isPost() {
122 1
		return $this->getMethod() === 'POST';
123
	}
124
125
	/**
126
	 * Check if the request method is PUT.
127
	 *
128
	 * @return boolean
129
	 */
130 1
	public function isPut() {
131 1
		return $this->getMethod() === 'PUT';
132
	}
133
134
	/**
135
	 * Check if the request method is PATCH.
136
	 *
137
	 * @return boolean
138
	 */
139 1
	public function isPatch() {
140 1
		return $this->getMethod() === 'PATCH';
141
	}
142
143
	/**
144
	 * Check if the request method is DELETE.
145
	 *
146
	 * @return boolean
147
	 */
148 1
	public function isDelete() {
149 1
		return $this->getMethod() === 'DELETE';
150
	}
151
152
	/**
153
	 * Check if the request method is OPTIONS.
154
	 *
155
	 * @return boolean
156
	 */
157 1
	public function isOptions() {
158 1
		return $this->getMethod() === 'OPTIONS';
159
	}
160
161
	/**
162
	 * Check if the request method is a "read" verb.
163
	 *
164
	 * @return boolean
165
	 */
166 1
	public function isReadVerb() {
167 1
		return in_array( $this->getMethod(), ['GET', 'HEAD', 'OPTIONS'] );
168
	}
169
170
	/**
171
	 * Get the request url.
172
	 *
173
	 * @return string
174
	 */
175 3
	public function getUrl() {
176 3
		$https = $this->server( 'HTTPS' );
177
178 3
		$protocol = $https ? 'https' : 'http';
179 3
		$host = (string) $this->server( 'HTTP_HOST', '' );
180 3
		$uri = (string) $this->server( 'REQUEST_URI', '' );
181 3
		$uri = Url::addLeadingSlash( $uri );
182
183 3
		return $protocol . '://' . $host . $uri;
184
	}
185
186
	/**
187
	 * Get a value from any of the request parameters.
188
	 *
189
	 * @see    \WPEmerge\Support\Arr
190
	 * @return mixed
191
	 */
192 4
	protected function input() {
193 4
		$args = func_get_args();
194 4
		$source = $this->{$args[0]};
195
196 4
		if ( count( $args ) === 1 ) {
197 1
			return $source;
198
		}
199
200 3
		$args[0] = $source;
201 3
		return call_user_func_array( [Arr::class, 'get'], $args );
202
	}
203
204
	/**
205
	 * Get a value from the GET parameters.
206
	 *
207
	 * @see    \WPEmerge\Support\Arr
208
	 * @return mixed
209
	 */
210 4
	public function get() {
211 4
		return call_user_func_array( [$this, 'input'], array_merge( ['get'], func_get_args() ) );
212
	}
213
214
	/**
215
	 * Get a value from the POST parameters.
216
	 *
217
	 * @see    \WPEmerge\Support\Arr
218
	 * @return mixed
219
	 */
220 4
	public function post() {
221 4
		return call_user_func_array( [$this, 'input'], array_merge( ['post'], func_get_args() ) );
222
	}
223
224
	/**
225
	 * Get a value from the COOKIE parameters.
226
	 *
227
	 * @see    \WPEmerge\Support\Arr
228
	 * @return mixed
229
	 */
230 4
	public function cookie() {
231 4
		return call_user_func_array( [$this, 'input'], array_merge( ['cookie'], func_get_args() ) );
232
	}
233
234
	/**
235
	 * Get a value from the FILES parameters.
236
	 *
237
	 * @see    \WPEmerge\Support\Arr
238
	 * @return mixed
239
	 */
240 4
	public function files() {
241 4
		return call_user_func_array( [$this, 'input'], array_merge( ['files'], func_get_args() ) );
242
	}
243
244
	/**
245
	 * Get a value from the SERVER parameters.
246
	 *
247
	 * @see    \WPEmerge\Support\Arr
248
	 * @return mixed
249
	 */
250 4
	public function server() {
251 4
		return call_user_func_array( [$this, 'input'], array_merge( ['server'], func_get_args() ) );
252
	}
253
254
	/**
255
	 * Get a value from the headers.
256
	 *
257
	 * @see    \WPEmerge\Support\Arr
258
	 * @return mixed
259
	 */
260 4
	public function headers() {
261 4
		return call_user_func_array( [$this, 'input'], array_merge( ['headers'], func_get_args() ) );
262
	}
263
}
264