From 67a00c68008571ea1af468fcf5efbbce3b77576a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Fri, 22 Apr 2016 12:55:24 +0200 Subject: [PATCH] - Optimized Input class. - Input class now returns array instead of InputItem instance when object is of type array. - Optimized key behavior in Input class. - Optimized arrayToParams method in RouterBase class. - Added getMergableSettings method to RouterGroup to avoid middleware merge; as they will already be loaded. --- src/Pecee/Http/Input/Input.php | 20 +++++++------------- src/Pecee/SimpleRouter/RouterBase.php | 19 +++++++++++-------- src/Pecee/SimpleRouter/RouterGroup.php | 10 ++++++++++ 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/Pecee/Http/Input/Input.php b/src/Pecee/Http/Input/Input.php index acf8d92..689b9d0 100644 --- a/src/Pecee/Http/Input/Input.php +++ b/src/Pecee/Http/Input/Input.php @@ -49,12 +49,13 @@ class Input { } public function getObject($index, $default = null) { + $key = (strpos($index, '[') > -1) ? substr($index, strpos($index, '[')+1, strpos($index, ']') - strlen($index)) : null; $index = (strpos($index, '[') > -1) ? substr($index, 0, strpos($index, '[')) : $index; $element = $this->get->findFirst($index); if($element !== null) { - return $element; + return ($key !== null) ? $element[$key] : $element; } if(Request::getInstance()->getMethod() !== 'get') { @@ -62,12 +63,12 @@ class Input { $element = $this->post->findFirst($index); if ($element !== null) { - return $element; + return ($key !== null) ? $element[$key] : $element; } $element = $this->file->findFirst($index); if ($element !== null) { - return $element; + return ($key !== null) ? $element[$key] : $element; } } @@ -82,9 +83,6 @@ class Input { */ public function get($index, $default = null) { - $key = (strpos($index, '[') > -1) ? substr($index, strpos($index, '[')+1, strpos($index, ']') - strlen($index)) : null; - $index = (strpos($index, '[') > -1) ? substr($index, 0, strpos($index, '[')) : $index; - $item = $this->getObject($index); if($item !== null) { @@ -93,10 +91,6 @@ class Input { return $item; } - if (is_array($item->getValue())) { - return ($key !== null && isset($item->getValue()[$key])) ? $item->getValue()[$key] : $item->getValue(); - } - return (trim($item->getValue()) === '') ? $default : $item->getValue(); } @@ -119,7 +113,7 @@ class Input { $output[$k] = new InputItem($k, $g); } - $this->get->{$key} = new InputItem($key, $output); + $this->get->{$key} = $output; } } } @@ -149,7 +143,7 @@ class Input { $output[$k] = new InputItem($k, $p); } - $this->post->{strtolower($key)} = new InputItem($key, $output); + $this->post->{strtolower($key)} = $output; } } } @@ -189,7 +183,7 @@ class Input { } } - $this->file->{strtolower($key)} = new InputItem($key, $output); + $this->file->{strtolower($key)} = $output; } } } diff --git a/src/Pecee/SimpleRouter/RouterBase.php b/src/Pecee/SimpleRouter/RouterBase.php index b4c9471..cb7d358 100644 --- a/src/Pecee/SimpleRouter/RouterBase.php +++ b/src/Pecee/SimpleRouter/RouterBase.php @@ -303,13 +303,17 @@ class RouterBase { } public function arrayToParams(array $getParams = null, $includeEmpty = true) { - if (is_array($getParams) && count($getParams) > 0) { - foreach ($getParams as $key => $val) { - if (!empty($val) || $includeEmpty) { - $getParams[$key] = (is_array($val) ? $this->arrayToParams($val, $includeEmpty) : $key . '=' . $val); - } + + if(is_array($getParams)) { + if ($includeEmpty === false) { + $getParams = array_filter($getParams, function ($item) { + if (!empty($item)) { + return $item; + } + }); } - return join('&', $getParams); + + return http_build_query($getParams); } return ''; } @@ -386,8 +390,7 @@ class RouterBase { if($controller === null && $parameters === null) { $getParams = (is_array($getParams)) ? array_merge($_GET, $getParams) : $_GET; - $url = parse_url(Request::getInstance()->getUri()); - $url = $url['path']; + $url = parse_url($this->request->getUri(), PHP_URL_PATH); if(count($getParams)) { $url .= '?' . $this->arrayToParams($getParams); diff --git a/src/Pecee/SimpleRouter/RouterGroup.php b/src/Pecee/SimpleRouter/RouterGroup.php index 231bb52..ebf972a 100644 --- a/src/Pecee/SimpleRouter/RouterGroup.php +++ b/src/Pecee/SimpleRouter/RouterGroup.php @@ -98,4 +98,14 @@ class RouterGroup extends RouterEntry { return $this; } + public function getMergeableSettings() { + $settings = parent::getMergeableSettings(); + + if(isset($settings['middleware'])) { + unset($settings['middleware']); + } + + return $settings; + } + } \ No newline at end of file