From 2b9403db28a5260e590e2023172327c562f3a764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Thu, 25 Mar 2021 03:41:11 +0100 Subject: [PATCH] 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. --- src/Pecee/Http/Input/InputHandler.php | 31 ++++++++++++--- src/Pecee/Http/Request.php | 38 +++++++++++++------ src/Pecee/Http/Url.php | 2 +- .../SimpleRouter/ClassLoader/ClassLoader.php | 2 +- src/Pecee/SimpleRouter/Router.php | 16 ++++---- tests/Pecee/SimpleRouter/InputHandlerTest.php | 9 +++++ 6 files changed, 72 insertions(+), 26 deletions(-) diff --git a/src/Pecee/Http/Input/InputHandler.php b/src/Pecee/Http/Input/InputHandler.php index 1f2b696..a09618e 100644 --- a/src/Pecee/Http/Input/InputHandler.php +++ b/src/Pecee/Http/Input/InputHandler.php @@ -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; } diff --git a/src/Pecee/Http/Request.php b/src/Pecee/Http/Request.php index 8d6dab9..15f9cb4 100644 --- a/src/Pecee/Http/Request.php +++ b/src/Pecee/Http/Request.php @@ -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; } diff --git a/src/Pecee/Http/Url.php b/src/Pecee/Http/Url.php index 161dc37..256e4a5 100644 --- a/src/Pecee/Http/Url.php +++ b/src/Pecee/Http/Url.php @@ -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; } /** diff --git a/src/Pecee/SimpleRouter/ClassLoader/ClassLoader.php b/src/Pecee/SimpleRouter/ClassLoader/ClassLoader.php index f1e5729..b6359cf 100644 --- a/src/Pecee/SimpleRouter/ClassLoader/ClassLoader.php +++ b/src/Pecee/SimpleRouter/ClassLoader/ClassLoader.php @@ -11,7 +11,7 @@ class ClassLoader implements IClassLoader * * @param string $class * @return object - * @throws NotFoundHttpException + * @throws ClassNotFoundHttpException */ public function loadClass(string $class) { diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index 99ecb07..72fbc05 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -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 */ diff --git a/tests/Pecee/SimpleRouter/InputHandlerTest.php b/tests/Pecee/SimpleRouter/InputHandlerTest.php index 7fcb87b..0657a73 100644 --- a/tests/Pecee/SimpleRouter/InputHandlerTest.php +++ b/tests/Pecee/SimpleRouter/InputHandlerTest.php @@ -20,6 +20,13 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase 'Canon', ]; + protected $sodas = [ + 0 => 'Pepsi', + 1 => 'Coca Cola', + 2 => 'Harboe', + 3 => 'Mountain Dew', + ]; + protected $day = 'monday'; public function testPost() @@ -29,6 +36,7 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase $_POST = [ 'names' => $this->names, 'day' => $this->day, + 'sodas' => $this->sodas, ]; $router = TestRouter::router(); @@ -51,6 +59,7 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase $this->assertNull($handler->find('non-existing')); $this->assertNull($handler->value('names', null, 'get')); $this->assertNull($handler->find('names', 'get')); + $this->assertEquals($this->sodas, $handler->value('sodas')); $objects = $handler->find('names');