-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('{{]]'); | ||
} | ||
} |