Browse Source

add http_build_query

main 1.2.0
Aaron Parecki 6 years ago
parent
commit
ecd0ba1afc
No known key found for this signature in database GPG Key ID: 276C2817346D6056
2 changed files with 27 additions and 6 deletions
  1. +13
    -6
      src/utils.php
  2. +14
    -0
      tests/UtilsTest.php

+ 13
- 6
src/utils.php View File

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

+ 14
- 0
tests/UtilsTest.php View File

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

Loading…
Cancel
Save