Skip to content

Commit

Permalink
Merge pull request #526 from stripe/ob-fix-525
Browse files Browse the repository at this point in the history
Ignore null values in params
  • Loading branch information
ob-stripe authored Sep 25, 2018
2 parents 7363430 + 275ce65 commit 9a61fcb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/HttpClient/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public function request($method, $absUrl, $headers, $params, $hasFile)
$opts = $this->defaultOptions;
}

$params = Util\Util::objectsToIds($params);

if ($method == 'get') {
if ($hasFile) {
throw new Error\Api(
Expand Down
32 changes: 32 additions & 0 deletions lib/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,38 @@ public static function secureCompare($a, $b)
}
}

/**
* Recursively goes through an array of parameters. If a parameter is an instance of
* ApiResource, then it is replaced by the resource's ID.
* Also clears out null values.
*
* @param mixed $h
* @return mixed
*/
public static function objectsToIds($h)
{
if ($h instanceof \Stripe\ApiResource) {
return $h->id;
} elseif (static::isList($h)) {
$results = [];
foreach ($h as $v) {
array_push($results, static::objectsToIds($v));
}
return $results;
} elseif (is_array($h)) {
$results = [];
foreach ($h as $k => $v) {
if (is_null($v)) {
continue;
}
$results[$k] = static::objectsToIds($v);
}
return $results;
} else {
return $h;
}
}

/**
* @param array $params
*
Expand Down
34 changes: 30 additions & 4 deletions tests/Stripe/Util/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ public function testThatPHPHasValueSemanticsForArrays()

public function testConvertStripeObjectToArrayIncludesId()
{
$customer = Util\Util::convertToStripeObject([
'id' => 'cus_123',
'object' => 'customer',
], null);
$customer = Util\Util::convertToStripeObject(
[
'id' => 'cus_123',
'object' => 'customer',
],
null
);
$this->assertTrue(array_key_exists("id", $customer->__toArray(true)));
}

Expand All @@ -46,6 +49,29 @@ public function testUtf8()
$this->assertSame(Util\Util::utf8($x), $x);
}

public function testObjectsToIds()
{
$params = [
'foo' => 'bar',
'customer' => Util\Util::convertToStripeObject(
[
'id' => 'cus_123',
'object' => 'customer',
],
null
),
'null_value' => null,
];

$this->assertSame(
[
'foo' => 'bar',
'customer' => 'cus_123',
],
Util\Util::objectsToIds($params)
);
}

public function testEncodeParameters()
{
$params = [
Expand Down

0 comments on commit 9a61fcb

Please sign in to comment.