mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 00:37:52 +00:00
[FEATURE] Added Request::getContentType for content-type header-parsing
- Added unit-tests for Request::getContentType parsing.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user