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; 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 * Get input element value matching index
* *
@@ -249,18 +265,18 @@ class InputHandler
{ {
$input = $this->find($index, ...$methods); $input = $this->find($index, ...$methods);
if ($input instanceof IInputItem) {
$input = $input->getValue();
}
/* Handle collection */ /* Handle collection */
if (\is_array($input) === true) { if (\is_array($input) === true) {
$output = []; $output = $this->getValueFromArray($input);
/* @var $item InputItem */
foreach ($input as $item) {
$output[] = \is_array($item) ? $item : $item->getValue();
}
return (\count($output) === 0) ? $defaultValue : $output; 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 public function setOriginalPost(array $post): self
{ {
$this->originalPost = $post; $this->originalPost = $post;
return $this; return $this;
} }
@@ -400,6 +417,7 @@ class InputHandler
public function setOriginalParams(array $params): self public function setOriginalParams(array $params): self
{ {
$this->originalParams = $params; $this->originalParams = $params;
return $this; return $this;
} }
@@ -420,6 +438,7 @@ class InputHandler
public function setOriginalFile(array $file): self public function setOriginalFile(array $file): self
{ {
$this->originalFile = $file; $this->originalFile = $file;
return $this; return $this;
} }
+27 -11
View File
@@ -117,7 +117,9 @@ class Request
$this->setHost($this->getHeader('http-host')); $this->setHost($this->getHeader('http-host'));
// Check if special IIS header exist, otherwise use default. // 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->method = strtolower($this->getHeader('request-method'));
$this->inputHandler = new InputHandler($this); $this->inputHandler = new InputHandler($this);
@@ -205,13 +207,11 @@ class Request
*/ */
public function getIp(): ?string public function getIp(): ?string
{ {
return $this->getHeader( return $this->getFirstHeader([
'http-cf-connecting-ip', 'http-cf-connecting-ip',
$this->getHeader( 'http-x-forwarded-for',
'http-x-forwarded-for', 'remote-addr',
$this->getHeader('remote-addr') ]);
)
);
} }
/** /**
@@ -270,6 +270,25 @@ class Request
return $header ?? $defaultValue; 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 * Get input class
* @return InputHandler * @return InputHandler
@@ -286,7 +305,7 @@ class Request
* *
* @return bool * @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); 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 public function setLoadedRoutes(array $routes): self
{ {
$this->loadedRoutes = $routes; $this->loadedRoutes = $routes;
return $this; return $this;
} }
@@ -439,7 +457,6 @@ class Request
public function addLoadedRoute(ILoadableRoute $route): self public function addLoadedRoute(ILoadableRoute $route): self
{ {
$this->loadedRoutes[] = $route; $this->loadedRoutes[] = $route;
return $this; return $this;
} }
@@ -462,7 +479,6 @@ class Request
public function setHasPendingRewrite(bool $boolean): self public function setHasPendingRewrite(bool $boolean): self
{ {
$this->hasPendingRewrite = $boolean; $this->hasPendingRewrite = $boolean;
return $this; return $this;
} }
+1 -1
View File
@@ -371,7 +371,7 @@ class Url implements \JsonSerializable
*/ */
public function getParam(string $name, ?string $defaultValue = null): ?string 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 * @param string $class
* @return object * @return object
* @throws NotFoundHttpException * @throws ClassNotFoundHttpException
*/ */
public function loadClass(string $class) public function loadClass(string $class)
{ {
+9 -7
View File
@@ -683,14 +683,16 @@ class Router
->setParams($getParams); ->setParams($getParams);
} }
/* We try to find a match on the given name */ if($name !== null) {
$route = $this->findRoute($name); /* We try to find a match on the given name */
$route = $this->findRoute($name);
if ($route !== null) { if ($route !== null) {
return $this->request return $this->request
->getUrlCopy() ->getUrlCopy()
->setPath($route->findUrl($route->getMethod(), $parameters, $name)) ->setPath($route->findUrl($route->getMethod(), $parameters, $name))
->setParams($getParams); ->setParams($getParams);
}
} }
/* Using @ is most definitely a controller@method or alias@method */ /* Using @ is most definitely a controller@method or alias@method */
@@ -20,6 +20,13 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase
'Canon', 'Canon',
]; ];
protected $sodas = [
0 => 'Pepsi',
1 => 'Coca Cola',
2 => 'Harboe',
3 => 'Mountain Dew',
];
protected $day = 'monday'; protected $day = 'monday';
public function testPost() public function testPost()
@@ -29,6 +36,7 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase
$_POST = [ $_POST = [
'names' => $this->names, 'names' => $this->names,
'day' => $this->day, 'day' => $this->day,
'sodas' => $this->sodas,
]; ];
$router = TestRouter::router(); $router = TestRouter::router();
@@ -51,6 +59,7 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase
$this->assertNull($handler->find('non-existing')); $this->assertNull($handler->find('non-existing'));
$this->assertNull($handler->value('names', null, 'get')); $this->assertNull($handler->value('names', null, 'get'));
$this->assertNull($handler->find('names', 'get')); $this->assertNull($handler->find('names', 'get'));
$this->assertEquals($this->sodas, $handler->value('sodas'));
$objects = $handler->find('names'); $objects = $handler->find('names');