From cf172a6b3fc41fe6b0d0061ceee71a07b93ea727 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Tue, 25 Sep 2018 13:56:53 +0200 Subject: [PATCH] Ignore null values in params --- lib/HttpClient/CurlClient.php | 2 ++ lib/Util/Util.php | 32 ++++++++++++++++++++++++++++++++ tests/Stripe/Util/UtilTest.php | 20 ++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/lib/HttpClient/CurlClient.php b/lib/HttpClient/CurlClient.php index 1234ef5abd..b25c1c14f3 100644 --- a/lib/HttpClient/CurlClient.php +++ b/lib/HttpClient/CurlClient.php @@ -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( diff --git a/lib/Util/Util.php b/lib/Util/Util.php index 271dec1230..7e64536bf6 100644 --- a/lib/Util/Util.php +++ b/lib/Util/Util.php @@ -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 * diff --git a/tests/Stripe/Util/UtilTest.php b/tests/Stripe/Util/UtilTest.php index 2992ff1ef3..25d8492e12 100644 --- a/tests/Stripe/Util/UtilTest.php +++ b/tests/Stripe/Util/UtilTest.php @@ -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 = [