This commit is contained in:
Bastian Allgeier
2020-07-07 12:40:13 +02:00
parent 5f025ac2c2
commit f79d2e960c
176 changed files with 10532 additions and 5343 deletions

View File

@@ -107,7 +107,7 @@ class Request
public function __construct(array $options = [])
{
$this->options = $options;
$this->method = $options['method'] ?? $_SERVER['REQUEST_METHOD'] ?? 'GET';
$this->method = $this->detectRequestMethod($options['method'] ?? null);
if (isset($options['body']) === true) {
$this->body = new Body($options['body']);
@@ -208,6 +208,39 @@ class Request
return array_merge($this->body()->toArray(), $this->query()->toArray());
}
/**
* Detect the request method from various
* options: given method, query string, server vars
*
* @param string $method
* @return string
*/
public function detectRequestMethod(string $method = null): string
{
// all possible methods
$methods = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH'];
// the request method can be overwritten with a header
$methodOverride = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] ?? null);
if ($method === null && in_array($methodOverride, $methods) === true) {
$method = $methodOverride;
}
// final chain of options to detect the method
$method = $method ?? $_SERVER['REQUEST_METHOD'] ?? 'GET';
// uppercase the shit out of it
$method = strtoupper($method);
// sanitize the method
if (in_array($method, $methods) === false) {
$method = 'GET';
}
return $method;
}
/**
* Returns the domain
*