| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package WPEmerge |
| 4 |
* @author Atanas Angelov <hi@atanas.dev> |
| 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 |
protected function getMethodOverride( $default ) { |
| 54 |
$valid_overrides = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']; |
| 55 |
$override = $default; |
| 56 |
|
| 57 |
$header_override = (string) $this->getHeaderLine( 'X-HTTP-METHOD-OVERRIDE' ); |
| 58 |
if ( ! empty( $header_override ) ) { |
| 59 |
$override = strtoupper( $header_override ); |
| 60 |
} |
| 61 |
|
| 62 |
$body_override = (string) $this->body( '_method', '' ); |
| 63 |
if ( ! empty( $body_override ) ) { |
| 64 |
$override = strtoupper( $body_override ); |
| 65 |
} |
| 66 |
|
| 67 |
if ( in_array( $override, $valid_overrides, true ) ) { |
| 68 |
return $override; |
| 69 |
} |
| 70 |
|
| 71 |
return $default; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* {@inheritDoc} |
| 76 |
*/ |
| 77 |
public function getMethod() { |
| 78 |
$method = parent::getMethod(); |
| 79 |
|
| 80 |
if ( $method === 'POST' ) { |
| 81 |
$method = $this->getMethodOverride( $method ); |
| 82 |
} |
| 83 |
|
| 84 |
return $method; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* {@inheritDoc} |
| 89 |
*/ |
| 90 |
public function isGet() { |
| 91 |
return $this->getMethod() === 'GET'; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* {@inheritDoc} |
| 96 |
*/ |
| 97 |
public function isHead() { |
| 98 |
return $this->getMethod() === 'HEAD'; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* {@inheritDoc} |
| 103 |
*/ |
| 104 |
public function isPost() { |
| 105 |
return $this->getMethod() === 'POST'; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* {@inheritDoc} |
| 110 |
*/ |
| 111 |
public function isPut() { |
| 112 |
return $this->getMethod() === 'PUT'; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* {@inheritDoc} |
| 117 |
*/ |
| 118 |
public function isPatch() { |
| 119 |
return $this->getMethod() === 'PATCH'; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* {@inheritDoc} |
| 124 |
*/ |
| 125 |
public function isDelete() { |
| 126 |
return $this->getMethod() === 'DELETE'; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* {@inheritDoc} |
| 131 |
*/ |
| 132 |
public function isOptions() { |
| 133 |
return $this->getMethod() === 'OPTIONS'; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* {@inheritDoc} |
| 138 |
*/ |
| 139 |
public function isReadVerb() { |
| 140 |
return in_array( $this->getMethod(), ['GET', 'HEAD', 'OPTIONS'] ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* {@inheritDoc} |
| 145 |
*/ |
| 146 |
public function isAjax() { |
| 147 |
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 |
protected function get( $source, $key = '', $default = null ) { |
| 159 |
if ( empty( $key ) ) { |
| 160 |
return $source; |
| 161 |
} |
| 162 |
|
| 163 |
return Arr::get( $source, $key, $default ); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* {@inheritDoc} |
| 168 |
* @see ::get() |
| 169 |
*/ |
| 170 |
public function attributes( $key = '', $default = null ) { |
| 171 |
return call_user_func( [$this, 'get'], $this->getAttributes(), $key, $default ); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* {@inheritDoc} |
| 176 |
* @see ::get() |
| 177 |
*/ |
| 178 |
public function query( $key = '', $default = null ) { |
| 179 |
return call_user_func( [$this, 'get'], $this->getQueryParams(), $key, $default ); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* {@inheritDoc} |
| 184 |
* @see ::get() |
| 185 |
*/ |
| 186 |
public function body( $key = '', $default = null ) { |
| 187 |
return call_user_func( [$this, 'get'], $this->getParsedBody(), $key, $default ); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* {@inheritDoc} |
| 192 |
* @see ::get() |
| 193 |
*/ |
| 194 |
public function cookies( $key = '', $default = null ) { |
| 195 |
return call_user_func( [$this, 'get'], $this->getCookieParams(), $key, $default ); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* {@inheritDoc} |
| 200 |
* @see ::get() |
| 201 |
*/ |
| 202 |
public function files( $key = '', $default = null ) { |
| 203 |
return call_user_func( [$this, 'get'], $this->getUploadedFiles(), $key, $default ); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* {@inheritDoc} |
| 208 |
* @see ::get() |
| 209 |
*/ |
| 210 |
public function server( $key = '', $default = null ) { |
| 211 |
return call_user_func( [$this, 'get'], $this->getServerParams(), $key, $default ); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* {@inheritDoc} |
| 216 |
* @see ::get() |
| 217 |
*/ |
| 218 |
public function headers( $key = '', $default = null ) { |
| 219 |
return call_user_func( [$this, 'get'], $this->getHeaders(), $key, $default ); |
| 220 |
} |
| 221 |
} |
| 222 |
|