Skip to content

Commit

Permalink
Merge pull request stripe#32 from stripe/expand
Browse files Browse the repository at this point in the history
Add support for expand.
  • Loading branch information
boucher committed Feb 5, 2013
2 parents e7d436d + 3ae9e6d commit dc22127
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
=== 1.7.13 2013-02-01

* Add support for passing options when retrieving Stripe objects
e.g., Stripe_Charge::retrieve(array("id"=>"foo", "expand" => array("customer")))
Stripe_Charge::retrieve("foo") will continue to work

=== 1.7.12 2013-01-15

* Add support for setting a Stripe API version override
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.7.12
1.7.13
26 changes: 23 additions & 3 deletions lib/Stripe/ApiRequestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,36 @@ private static function _encodeObjects($d)
} else if (is_array($d)) {
$res = array();
foreach ($d as $k => $v)
$res[$k] = self::_encodeObjects($v);
$res[$k] = self::_encodeObjects($v);
return $res;
} else {
return $d;
}
}

public static function encode($d)
public static function encode($arr, $prefix=null)
{
return http_build_query($d, null, '&');
if (!is_array($arr))
return $arr;

$r = array();
foreach ($arr as $k => $v) {
if (is_null($v))
continue;

if ($prefix && $k && !is_int($k))
$k = $prefix."[".$k."]";
else if ($prefix)
$k = $prefix."[]";

if (is_array($v)) {
$r[] = self::encode($v, $k, true);
} else {
$r[] = urlencode($k)."=".urlencode($v);
}
}

return implode("&", $r);
}

public function request($meth, $url, $params=null)
Expand Down
2 changes: 1 addition & 1 deletion lib/Stripe/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function refresh()
$requestor = new Stripe_ApiRequestor($this->_apiKey);
$url = $this->instanceUrl();

list($response, $apiKey) = $requestor->request('get', $url);
list($response, $apiKey) = $requestor->request('get', $url, $this->_retrieveOptions);
$this->refreshFrom($response, $apiKey);
return $this;
}
Expand Down
13 changes: 12 additions & 1 deletion lib/Stripe/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@ public static function init()
protected $_values;
protected $_unsavedValues;
protected $_transientValues;
protected $_retrieveOptions;

public function __construct($id=null, $apiKey=null)
{
$this->_apiKey = $apiKey;
$this->_values = array();
$this->_unsavedValues = new Stripe_Util_Set();
$this->_transientValues = new Stripe_Util_Set();

$this->_retrieveOptions = array();
if (is_array($id)) {
foreach($id as $key => $value) {
if ($key != 'id')
$this->_retrieveOptions[$key] = $value;
}
$id = $id['id'];
}

if ($id)
$this->id = $id;
}
Expand Down Expand Up @@ -63,7 +74,7 @@ public function offsetSet($k, $v)
{
$this->$k = $v;
}

public function offsetExists($k)
{
return array_key_exists($k, $this->_values);
Expand Down
2 changes: 1 addition & 1 deletion lib/Stripe/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ abstract class Stripe
public static $apiBase = 'https://api.stripe.com';
public static $apiVersion = null;
public static $verifySslCerts = true;
const VERSION = '1.7.12';
const VERSION = '1.7.13';

public static function getApiKey()
{
Expand Down
8 changes: 8 additions & 0 deletions test/Stripe/ApiRequestorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit dc22127

Please sign in to comment.