diff --git a/classes/query.php b/classes/query.php index 6c54a1b..66fd039 100644 --- a/classes/query.php +++ b/classes/query.php @@ -58,6 +58,18 @@ public static function rebuild($uri) */ protected static function _make_query($query_arr) { + // init check function + $check_hash_array = function($arr) + { + foreach(array_keys($arr) as $key) + { + if(is_string($key)) + { + return true; + } + } + return false; + }; // sort array by name asc ksort($query_arr); $query = array(); @@ -74,7 +86,28 @@ protected static function _make_query($query_arr) // re-insert only value exists foreach($query_arr as $key => $value) { - if(strlen($value) > 0){ + if(is_array($value)) + { + // check has string key + $is_hash_array = $check_hash_array($value); + if($is_hash_array) + { + ksort($value, SORT_STRING); + } + else + { + sort($value, SORT_STRING); + } + foreach($value as $k => $v) + { + if(strlen($v) > 0) + { + $query[$key][$k] = $v; + } + } + } + elseif(strlen($value) > 0) + { $query[$key] = $value; } } @@ -82,6 +115,29 @@ protected static function _make_query($query_arr) { return ''; } - return '?' . http_build_query($query); + $return_string = '?'; + foreach($query as $key => $value) + { + if(is_array($value)) + { + $is_hash_array = $check_hash_array($value); + foreach($value as $k => $v) + { + if($is_hash_array) + { + $return_string .= $key.'['.$k.']='.rawurlencode($v).'&'; + } + else + { + $return_string .= $key.'[]='.rawurlencode($v).'&'; + } + } + } + else + { + $return_string .= $key.'='.rawurlencode($value).'&'; + } + } + return substr($return_string, 0, -1); } } diff --git a/classes/seo.php b/classes/seo.php index 0aa39e6..2d6daf8 100644 --- a/classes/seo.php +++ b/classes/seo.php @@ -54,7 +54,7 @@ public function __construct($request) $this->_current_uri = preg_replace('/^\//', '', urldecode($_SERVER['REQUEST_URI'])); $this->_current_get = ''; $get_pos = strpos($this->_current_uri, '?'); - if($get_pos > 0) + if($get_pos !== false and $get_pos >= 0) { $this->_current_get = substr($this->_current_uri, $get_pos, strlen($this->_current_uri) - $get_pos); $this->_current_uri = substr($this->_current_uri, 0, $get_pos); @@ -257,7 +257,7 @@ public function noindex() public function canonical() { // not use when use noindex for seo - if($this->_noindex_flg === false && $this->_canonical_flg === true) + if($this->_noindex_flg === false and $this->_canonical_flg === true) { return Seo_Html::canonical($this->_canonical_uri); } diff --git a/tests/query.php b/tests/query.php index af6cd1c..e095ae6 100644 --- a/tests/query.php +++ b/tests/query.php @@ -39,6 +39,39 @@ public function test_build() 'c' => '' ); $this->assertEquals('fuga/hoge?a=bar&z=foo', \Seo\Query::build('fuga/hoge')); + + $_GET = array( + 'a' => array( + 'foo', + ), + ); + $this->assertEquals('fuga/hoge?a[]=foo', \Seo\Query::build('fuga/hoge')); + + $_GET = array( + 'a' => array( + 'foo', + 'bar', + ), + ); + $this->assertEquals('fuga/hoge?a[]=bar&a[]=foo', \Seo\Query::build('fuga/hoge')); + + $_GET = array( + 'a' => array( + 'aa' => 'foo', + 'bb' => 'bar', + ), + ); + $this->assertEquals('fuga/hoge?a[aa]=foo&a[bb]=bar', \Seo\Query::build('fuga/hoge')); + + $_GET = array( + 'a' => array( + 'aa' => 'foo', + 'bb' => 'bar', + 'baz', + ), + ); + $this->assertEquals('fuga/hoge?a[0]=baz&a[aa]=foo&a[bb]=bar', \Seo\Query::build('fuga/hoge')); + } public function test_rebuild() @@ -68,6 +101,31 @@ public function test_rebuild() '?a=2&b=1', \Seo\Query::rebuild('?b=1&a=2') ); + $this->assertEquals( + 'hoge/fuga?a[]=1&a[]=2', + \Seo\Query::rebuild('hoge/fuga?a[]=1&a[]=2') + ); + $this->assertEquals( + 'hoge/fuga?a[]=1&a[]=2', + \Seo\Query::rebuild('hoge/fuga?a[]=2&a[]=1') + ); + $this->assertEquals( + 'hoge/fuga?a[]=1&a[]=2&b[]=1', + \Seo\Query::rebuild('hoge/fuga?a[]=1&a[]=2&b[]=1') + ); + // ignore only numeric key + $this->assertEquals( + 'hoge/fuga?a[]=1&a[]=2', + \Seo\Query::rebuild('hoge/fuga?a[0]=1&a[1]=2') + ); + $this->assertEquals( + 'hoge/fuga?a[aa]=2&a[bb]=1', + \Seo\Query::rebuild('hoge/fuga?a[bb]=1&a[aa]=2') + ); + $this->assertEquals( + 'hoge/fuga?a[0]=baz&a[aa]=2&a[bb]=1', + \Seo\Query::rebuild('hoge/fuga?a[bb]=1&a[aa]=2&a[]=baz') + ); // bug. encoded ampersand decode and resort // @see https://github.com/fuel/core/issues/1608 $this->assertEquals(