Skip to content

Commit

Permalink
Merge pull request #19 from piggyllc/fix-guzzle-6-compatibility-issue
Browse files Browse the repository at this point in the history
Fix guzzle 6 compatibility issue
  • Loading branch information
hywak committed Jun 30, 2021
2 parents dc37aaa + e96b074 commit d31554c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/Api/AppleApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Azimo\Apple\Api\Exception\PublicKeyFetchingFailedException;
use Azimo\Apple\Api\Factory\ResponseFactory;
use GuzzleHttp;
use GuzzleHttp\Utils;
use InvalidArgumentException;

final class AppleApiClient implements AppleApiClientInterface
Expand Down
33 changes: 33 additions & 0 deletions src/Api/Utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

namespace Azimo\Apple\Api;

use InvalidArgumentException;

final class Utils
{
/**
* Wrapper for json_decode that throws when an error occurs.
*
* @param string $json JSON data to parse
* @param bool $assoc When true, returned objects will be converted
* into associative arrays.
* @param int $depth User specified recursion depth.
* @param int $options Bitmask of JSON decode options.
*
* @return object|array|string|int|float|bool|null
*
* @throws InvalidArgumentException if the JSON cannot be decoded.
*
* @link https://www.php.net/manual/en/function.json-decode.php
*/
public static function jsonDecode(string $json, bool $assoc = false, int $depth = 512, int $options = 0)
{
$data = \json_decode($json, $assoc, $depth, $options);
if (\JSON_ERROR_NONE !== \json_last_error()) {
throw new InvalidArgumentException('json_decode error: ' . \json_last_error_msg());
}

return $data;
}
}
26 changes: 26 additions & 0 deletions tests/Unit/Api/UtilsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace Azimo\Apple\Tests\Unit\Api;

use Azimo\Apple\Api\Utils;
use PHPUnit\Framework\TestCase;
use InvalidArgumentException;

final class UtilsTest extends TestCase
{
public function testDecodesJson()
{
$this->assertTrue(Utils::jsonDecode('true'));
$this->assertEquals(['a' => 1, 'b' => 2], Utils::jsonDecode('{"a":1,"b":2}', true));
$this->assertEquals((object) ['a' => 1, 'b' => 2], Utils::jsonDecode('{"a":1,"b":2}'));
$this->assertEquals([5, 10], Utils::jsonDecode('[5, 10]', true));
$this->assertEquals([5, 10], Utils::jsonDecode('[5, 10]'));
}

public function testDecodesJsonAndThrowsOnError()
{
$this->expectException(InvalidArgumentException::class);

Utils::jsonDecode('{{]]');
}
}

0 comments on commit d31554c

Please sign in to comment.