Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for expand. #32

Merged
merged 2 commits into from
Feb 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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