Features & bugfixes

- Feature: added new getFirstHeader to Request object that will return the first header found from array list- used to simplify the code.
- Feature: added new InputHandler::getValueFromArray method that loops through input-items to ensure that value is always returned.
- Fixed calling getUrl with array as parameters option throws error.
- Fixed `SimpleRouter::getUrl` having wrong nullable return type.
This commit is contained in:
Simon Sessingø
2021-03-25 03:41:11 +01:00
parent 0ec7c0d960
commit 2b9403db28
6 changed files with 72 additions and 26 deletions
+27 -11
View File
@@ -117,7 +117,9 @@ class Request
$this->setHost($this->getHeader('http-host'));
// Check if special IIS header exist, otherwise use default.
$this->setUrl(new Url($this->getHeader('unencoded-url', $this->getHeader('request-uri'))));
$this->setUrl(new Url($this->getFirstHeader(['unencoded-url', 'request-uri',])));
$this->method = strtolower($this->getHeader('request-method'));
$this->inputHandler = new InputHandler($this);
@@ -205,13 +207,11 @@ class Request
*/
public function getIp(): ?string
{
return $this->getHeader(
return $this->getFirstHeader([
'http-cf-connecting-ip',
$this->getHeader(
'http-x-forwarded-for',
$this->getHeader('remote-addr')
)
);
'http-x-forwarded-for',
'remote-addr',
]);
}
/**
@@ -270,6 +270,25 @@ class Request
return $header ?? $defaultValue;
}
/**
* Will try to find first header from list of headers.
*
* @param array $headers
* @param null $defaultValue
* @return mixed|null
*/
public function getFirstHeader(array $headers, $defaultValue = null)
{
foreach($headers as $header) {
$header = $this->getHeader($header);
if($header !== null) {
return $header;
}
}
return $defaultValue;
}
/**
* Get input class
* @return InputHandler
@@ -286,7 +305,7 @@ class Request
*
* @return bool
*/
public function isFormatAccepted($format): bool
public function isFormatAccepted(string $format): bool
{
return ($this->getHeader('http-accept') !== null && stripos($this->getHeader('http-accept'), $format) !== false);
}
@@ -426,7 +445,6 @@ class Request
public function setLoadedRoutes(array $routes): self
{
$this->loadedRoutes = $routes;
return $this;
}
@@ -439,7 +457,6 @@ class Request
public function addLoadedRoute(ILoadableRoute $route): self
{
$this->loadedRoutes[] = $route;
return $this;
}
@@ -462,7 +479,6 @@ class Request
public function setHasPendingRewrite(bool $boolean): self
{
$this->hasPendingRewrite = $boolean;
return $this;
}