- 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.
This commit is contained in:
Simon Sessingø
2016-04-22 12:55:24 +02:00
parent 1420203149
commit 67a00c6800
3 changed files with 28 additions and 21 deletions

View File

@@ -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;
}
}
}

View File

@@ -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);

View File

@@ -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;
}
}