Skip to content

Commit

Permalink
Merge pull request #4 from hinashiki/development [skip ci]
Browse files Browse the repository at this point in the history
Fix #2 and #3
  • Loading branch information
hinashiki committed May 10, 2016
2 parents e10671c + e6001b9 commit f48d30c
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 4 deletions.
60 changes: 58 additions & 2 deletions classes/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -74,14 +86,58 @@ 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;
}
}
if(empty($query))
{
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);
}
}
4 changes: 2 additions & 2 deletions classes/seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
58 changes: 58 additions & 0 deletions tests/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit f48d30c

Please sign in to comment.