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
+25 -6
View File
@@ -237,6 +237,22 @@ class InputHandler
return $element;
}
protected function getValueFromArray(array $array): array
{
$output = [];
/* @var $item InputItem */
foreach ($array as $key => $item) {
if ($item instanceof IInputItem) {
$item = $item->getValue();
}
$output[$key] = \is_array($item) ? $this->getValueFromArray($item) : $item;
}
return $output;
}
/**
* Get input element value matching index
*
@@ -249,18 +265,18 @@ class InputHandler
{
$input = $this->find($index, ...$methods);
if ($input instanceof IInputItem) {
$input = $input->getValue();
}
/* Handle collection */
if (\is_array($input) === true) {
$output = [];
/* @var $item InputItem */
foreach ($input as $item) {
$output[] = \is_array($item) ? $item : $item->getValue();
}
$output = $this->getValueFromArray($input);
return (\count($output) === 0) ? $defaultValue : $output;
}
return ($input === null || (\is_string($input->getValue()) && trim($input->getValue()) === '')) ? $defaultValue : $input->getValue();
return ($input === null || (\is_string($input) && trim($input) === '')) ? $defaultValue : $input;
}
/**
@@ -380,6 +396,7 @@ class InputHandler
public function setOriginalPost(array $post): self
{
$this->originalPost = $post;
return $this;
}
@@ -400,6 +417,7 @@ class InputHandler
public function setOriginalParams(array $params): self
{
$this->originalParams = $params;
return $this;
}
@@ -420,6 +438,7 @@ class InputHandler
public function setOriginalFile(array $file): self
{
$this->originalFile = $file;
return $this;
}
+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;
}
+1 -1
View File
@@ -371,7 +371,7 @@ class Url implements \JsonSerializable
*/
public function getParam(string $name, ?string $defaultValue = null): ?string
{
return isset($this->getParams()[$name]) ?? $defaultValue;
return (isset($this->getParams()[$name]) === true) ? $this->getParams()[$name] : $defaultValue;
}
/**
@@ -11,7 +11,7 @@ class ClassLoader implements IClassLoader
*
* @param string $class
* @return object
* @throws NotFoundHttpException
* @throws ClassNotFoundHttpException
*/
public function loadClass(string $class)
{
+9 -7
View File
@@ -683,14 +683,16 @@ class Router
->setParams($getParams);
}
/* We try to find a match on the given name */
$route = $this->findRoute($name);
if($name !== null) {
/* We try to find a match on the given name */
$route = $this->findRoute($name);
if ($route !== null) {
return $this->request
->getUrlCopy()
->setPath($route->findUrl($route->getMethod(), $parameters, $name))
->setParams($getParams);
if ($route !== null) {
return $this->request
->getUrlCopy()
->setPath($route->findUrl($route->getMethod(), $parameters, $name))
->setParams($getParams);
}
}
/* Using @ is most definitely a controller@method or alias@method */