[FEATURE] Added Request::getContentType for content-type header-parsing

- Added unit-tests for Request::getContentType parsing.
This commit is contained in:
Simon Sessingø
2021-03-25 13:09:23 +01:00
parent 86bb88a41f
commit 8835aca02e
2 changed files with 76 additions and 3 deletions
+38 -3
View File
@@ -19,6 +19,10 @@ class Request
public const REQUEST_TYPE_DELETE = 'delete';
public const REQUEST_TYPE_HEAD = 'head';
public const CONTENT_TYPE_JSON = 'application/json';
public const CONTENT_TYPE_FORM_DATA = 'multipart/form-data';
public const CONTENT_TYPE_X_FORM_ENCODED = 'application/x-www-form-urlencoded';
/**
* All request-types
* @var string[]
@@ -57,6 +61,12 @@ class Request
*/
protected $headers = [];
/**
* Request ContentType
* @var string
*/
protected $contentType;
/**
* Request host
* @var string
@@ -117,10 +127,10 @@ class Request
$this->setHost($this->getHeader('http-host'));
// Check if special IIS header exist, otherwise use default.
$this->setUrl(new Url($this->getFirstHeader(['unencoded-url', 'request-uri',])));
$this->setContentType(strtolower($this->getHeader('content-type')));
$this->method = strtolower($this->getHeader('request-method'));
$this->inputHandler = new InputHandler($this);
$this->method = strtolower($this->inputHandler->value('_method', $this->getHeader('request-method')));
@@ -289,6 +299,31 @@ class Request
return $defaultValue;
}
/**
* Get request content-type
* @return string|null
*/
public function getContentType(): ?string
{
return $this->contentType;
}
/**
* Set request content-type
* @param string $contentType
* @return $this
*/
protected function setContentType(string $contentType): self
{
if(strpos($contentType, ';') > 0) {
$this->contentType = substr($contentType, 0, strpos($contentType, ';'));
} else {
$this->contentType = $contentType;
}
return $this;
}
/**
* Get input class
* @return InputHandler
@@ -497,4 +532,4 @@ class Request
return $this->data[$name] ?? null;
}
}
}