Skip to content

Commit

Permalink
Ignore null values in params
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Sep 25, 2018
1 parent 7363430 commit cf172a6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 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
20 changes: 20 additions & 0 deletions tests/Stripe/Util/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ 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 cf172a6

Please sign in to comment.