From ecd0ba1afca8e586f09792f7be5052e142d3cbde Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Wed, 28 Mar 2018 06:44:56 -0700 Subject: [PATCH] add http_build_query --- src/utils.php | 19 +++++++++++++------ tests/UtilsTest.php | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/utils.php b/src/utils.php index ce4c525..5d30756 100644 --- a/src/utils.php +++ b/src/utils.php @@ -98,6 +98,13 @@ function http_header_case($str) { return $str; } +function http_build_query($params) { + // PHP's built-in http_build_query function encodes arrays with numeric indexes, + // like foo[0]=bar&foo[0]=baz + // This function removes the numeric indexes so that it's conformant with Micropub + return preg_replace('/%5B[0-9]+%5D/', '%5B%5D', \http_build_query($params)); +} + function html_to_dom_document($html) { // Parse the source body as HTML $doc = new DOMDocument(); @@ -144,19 +151,19 @@ function correct_photo_rotation($filename) { } /** - * Converts base 10 to base 60. + * Converts base 10 to base 60. * http://tantek.pbworks.com/NewBase60 * @param int $n * @return string - */ + */ function b10to60($n) { $s = ""; $m = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz"; - if ($n==0) - return 0; + if ($n==0) + return 0; - while ($n>0) + while ($n>0) { $d = $n % 60; $s = $m[$d] . $s; @@ -176,7 +183,7 @@ function b60to10($s) $n = 0; for($i = 0; $i < strlen($s); $i++) // iterate from first to last char of $s { - $c = ord($s[$i]); // put current ASCII of char into $c + $c = ord($s[$i]); // put current ASCII of char into $c if ($c>=48 && $c<=57) { $c=$c-48; } else if ($c>=65 && $c<=72) { $c-=55; } else if ($c==73 || $c==108) { $c=1; } // typo capital I, lowercase l to 1 diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index 236d600..d6ff1ce 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -122,4 +122,18 @@ class UtilsTest extends PHPUnit_Framework_TestCase { $this->assertEquals('Predis\Client', get_class(p3k\redis())); } + public function testBuildQuery() { + $params = ['foo'=>['bar','baz']]; + $body = p3k\http_build_query($params); + $this->assertEquals('foo%5B%5D=bar&foo%5B%5D=baz', $body); + + $params = ['a','b','c']; + $body = p3k\http_build_query($params); + $this->assertEquals('0=a&1=b&2=c', $body); + + $params = ['a'=>'A','b'=>'B']; + $body = p3k\http_build_query($params); + $this->assertEquals('a=A&b=B', $body); + } + }