From db0ddc4b36f2bb0c997c64498f27d0d9968e7947 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Wed, 11 Dec 2013 21:18:57 -0800 Subject: [PATCH 1/2] Add is_array_assoc() and array indexed and associative tests --- Curl.class.php | 4 ++++ tests/run.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/Curl.class.php b/Curl.class.php index 3508454..1e2c0fe 100644 --- a/Curl.class.php +++ b/Curl.class.php @@ -168,6 +168,10 @@ class Curl { public $response = NULL; } +function is_array_assoc($array) { + return (bool)count(array_filter(array_keys($array), 'is_string')); +} + function is_array_multidim($array) { if (!is_array($array)) { return FALSE; diff --git a/tests/run.php b/tests/run.php index 448cc49..d7288fb 100644 --- a/tests/run.php +++ b/tests/run.php @@ -10,6 +10,22 @@ class CurlTest extends PHPUnit_Framework_TestCase { $this->assertTrue(extension_loaded('curl')); } + public function testArrayAssociative() { + $this->assertTrue(is_array_assoc(array( + 'foo' => 'wibble', + 'bar' => 'wubble', + 'baz' => 'wobble', + ))); + } + + public function testArrayIndexed() { + $this->assertFalse(is_array_assoc(array( + 'wibble', + 'wubble', + 'wobble', + ))); + } + public function testUserAgent() { $test = new Test(); $test->curl->setUserAgent(Curl::USER_AGENT); From 5ead0ee6dcf23dc79b33d39e2175699a51309ffb Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Wed, 11 Dec 2013 22:05:54 -0800 Subject: [PATCH 2/2] Fix #4: Arrays inside the data array loses its indexes --- Curl.class.php | 7 +++++-- tests/run.php | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Curl.class.php b/Curl.class.php index 1e2c0fe..2088253 100644 --- a/Curl.class.php +++ b/Curl.class.php @@ -84,12 +84,15 @@ class Curl { function http_build_multi_query($data, $key=NULL) { $query = array(); + $is_array_assoc = is_array_assoc($data); + foreach ($data as $k => $value) { if (is_string($value)) { - $query[] = urlencode(is_null($key) ? $k : $key) . '=' . rawurlencode($value); + $brackets = $is_array_assoc ? '[' . $k . ']' : '[]'; + $query[] = urlencode(is_null($key) ? $k : $key . $brackets) . '=' . rawurlencode($value); } else if (is_array($value)) { - $query[] = $this->http_build_multi_query($value, $k . '[]'); + $query[] = $this->http_build_multi_query($value, $k); } } diff --git a/tests/run.php b/tests/run.php index d7288fb..320d395 100644 --- a/tests/run.php +++ b/tests/run.php @@ -59,6 +59,20 @@ class CurlTest extends PHPUnit_Framework_TestCase { )) === 'post'); } + public function testPostAssociativeArrayData() { + $test = new Test(); + $this->assertTrue($test->server('POST', array( + 'test' => 'post_multidimensional', + 'username' => 'myusername', + 'password' => 'mypassword', + 'more_data' => array( + 'param1' => 'something', + 'param2' => 'other thing', + 'param3' => '123', + ), + )) === 'test=post_multidimensional&username=myusername&password=mypassword&more_data%5Bparam1%5D=something&more_data%5Bparam2%5D=other%20thing&more_data%5Bparam3%5D=123'); + } + public function testPostMultidimensionalData() { $test = new Test(); $this->assertTrue($test->server('POST', array(