Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix encoder, add a couple tests.

  • Loading branch information...
commit 3ae9e6d4db21e52be30708e9ba0d9307fd3e95fd 1 parent dcbe572
@boucher boucher authored
Showing with 20 additions and 9 deletions.
  1. +12 −9 lib/Stripe/ApiRequestor.php
  2. +8 −0 test/Stripe/ApiRequestorTest.php
View
21 lib/Stripe/ApiRequestor.php
@@ -41,23 +41,26 @@ private static function _encodeObjects($d)
}
}
- public static function encode($arr, $prefix=null, $nested=false)
+ public static function encode($arr, $prefix=null)
{
if (!is_array($arr))
- return false;
+ return $arr;
+ $r = array();
foreach ($arr as $k => $v) {
- if ($nested)
+ if (is_null($v))
+ continue;
+
+ if ($prefix && $k && !is_int($k))
+ $k = $prefix."[".$k."]";
+ else if ($prefix)
$k = $prefix."[]";
- elseif (is_int($k))
- $k = $prefix.$k;
- if (is_array($v) || is_object($v)) {
+ if (is_array($v)) {
$r[] = self::encode($v, $k, true);
- continue;
+ } else {
+ $r[] = urlencode($k)."=".urlencode($v);
}
-
- $r[]=urlencode($k)."=" .urlencode($v);
}
return implode("&", $r);
View
8 test/Stripe/ApiRequestorTest.php
@@ -11,6 +11,14 @@ public function testEncode()
$a = array('that' => array('your' => 'example', 'foo' => null));
$enc = Stripe_APIRequestor::encode($a);
$this->assertEqual($enc, 'that%5Byour%5D=example');
+
+ $a = array('that' => 'example', 'foo' => array('bar', 'baz'));
+ $enc = Stripe_APIRequestor::encode($a);
+ $this->assertEqual($enc, 'that=example&foo%5B%5D=bar&foo%5B%5D=baz');
+
+ $a = array('my' => 'value', 'that' => array('your' => array('cheese', 'whiz', null)), 'bar' => 1, 'baz' => null);
+ $enc = Stripe_APIRequestor::encode($a);
+ $this->assertEqual($enc, 'my=value&that%5Byour%5D%5B%5D=cheese&that%5Byour%5D%5B%5D=whiz&bar=1');
}
public function testEncodeObjects()
Please sign in to comment.
Something went wrong with that request. Please try again.