Skip to content

Commit

Permalink
Merge pull request #28 from Trunkrs/fix/25/nullability
Browse files Browse the repository at this point in the history
Fix: Strip unnecessary null values from request body
  • Loading branch information
hiddestokvis authored Feb 9, 2021
2 parents 82bc8ce + c765507 commit 18d60f1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.1
2.0.2
5 changes: 3 additions & 2 deletions lib/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Trunkrs\SDK\Exception\GeneralApiException;
use Trunkrs\SDK\Exception\NotAuthorizedException;
use Trunkrs\SDK\Exception\ServerValidationException;
use Trunkrs\SDK\Util\NullStripper;

class RequestHandler {
private static $_httpClient;
Expand Down Expand Up @@ -103,7 +104,7 @@ public static function post(string $resource, array $body) {
"POST",
self::createUrl($resource),
self::createHeaders(!empty($body)),
$body
NullStripper::strip($body)
);

if (!self::isSuccessful($response)) {
Expand All @@ -127,7 +128,7 @@ public static function put(string $resource, array $body = []) {
"PUT",
self::createUrl($resource),
self::createHeaders(!empty($body)),
$body
NullStripper::strip($body)
);

if (!self::isSuccessful($response)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Settings {
/**
* @var string The current version of the SDK.
*/
public static $sdkVersion = '2.0.1';
public static $sdkVersion = '2.0.2';

/**
* Sets the client credentials that will be used in subsequent requests.
Expand Down
20 changes: 20 additions & 0 deletions lib/Util/NullStripper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php


namespace Trunkrs\SDK\Util;


class NullStripper
{
public static function strip(array $array): array {
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key] = NullStripper::strip($value);
} else if (is_null($value)) {
unset($key);
}
}

return $array;
}
}

0 comments on commit 18d60f1

Please sign in to comment.