Failed Conditions
Push — master ( 63b660...353e51 )
by Atanas
02:58
created

src/Requests/Request.php (9 issues)

1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2018 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)
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
37
			->withQueryParams($_GET)
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
38
			->withParsedBody($_POST)
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
39
			->withUploadedFiles(self::normalizeFiles($_FILES));
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
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 = '';
56
57 1
		$header_override = (string) $this->getHeaderLine( 'X-HTTP-METHOD-OVERRIDE' );
58 1
		if ( is_string( $header_override ) && ! empty( $header_override ) ) {
59 1
			$override = strtoupper( $header_override );
60
		}
61
62 1
		$body_override = (string) $this->body( '_method', '' );
63 1
		if ( is_string( $body_override ) && ! empty( $body_override ) ) {
64 1
			$override = strtoupper( $body_override );
65
		}
66
67 1
		if ( ! empty( $override ) && 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  string $source
154
	 * @param  string $key
155
	 * @param  mixed  $default
156
	 * @return mixed
157
	 */
158 4
	protected function get( $source, $key = '', $default = null ) {
159 4
		$source = is_array( $source ) ? $source : [];
0 ignored issues
show
The condition is_array($source) is always false.
Loading history...
160
161 4
		if ( empty( $key ) ) {
162 1
			return $source;
163
		}
164
165 3
		return Arr::get( $source, $key, $default );
166
	}
167
168
	/**
169
	 * {@inheritDoc}
170
	 * @see ::get()
171
	 */
172
	public function attributes( $key = '', $default = null ) {
173
		return call_user_func( [$this, 'get'], $this->getAttributes(), $key, $default );
174
	}
175
176
	/**
177
	 * {@inheritDoc}
178
	 * @see ::get()
179
	 */
180 4
	public function query( $key = '', $default = null ) {
181 4
		return call_user_func( [$this, 'get'], $this->getQueryParams(), $key, $default );
182
	}
183
184
	/**
185
	 * {@inheritDoc}
186
	 * @see ::get()
187
	 */
188 4
	public function body( $key = '', $default = null ) {
189 4
		return call_user_func( [$this, 'get'], $this->getParsedBody(), $key, $default );
190
	}
191
192
	/**
193
	 * {@inheritDoc}
194
	 * @see ::get()
195
	 */
196 4
	public function cookies( $key = '', $default = null ) {
197 4
		return call_user_func( [$this, 'get'], $this->getCookieParams(), $key, $default );
198
	}
199
200
	/**
201
	 * {@inheritDoc}
202
	 * @see ::get()
203
	 */
204 4
	public function files( $key = '', $default = null ) {
205 4
		return call_user_func( [$this, 'get'], $this->getUploadedFiles(), $key, $default );
206
	}
207
208
	/**
209
	 * {@inheritDoc}
210
	 * @see ::get()
211
	 */
212 4
	public function server( $key = '', $default = null ) {
213 4
		return call_user_func( [$this, 'get'], $this->getServerParams(), $key, $default );
214
	}
215
216
	/**
217
	 * {@inheritDoc}
218
	 * @see ::get()
219
	 */
220 4
	public function headers( $key = '', $default = null ) {
221 4
		return call_user_func( [$this, 'get'], $this->getHeaders(), $key, $default );
222
	}
223
}
224