| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace Give\Vendors\LiquidWeb\LicensingApiClient\Http; |
| 4 |
|
| 5 |
use JsonException; |
| 6 |
use Give\Vendors\LiquidWeb\LicensingApiClient\Exceptions\DecodingException; |
| 7 |
|
| 8 |
/** |
| 9 |
* Decodes JSON response bodies into arrays for response mappers. |
| 10 |
*/ |
| 11 |
final class JsonDecoder |
| 12 |
{ |
| 13 |
/** |
| 14 |
* |
| 15 |
* @throws DecodingException |
| 16 |
* @return array<array-key, mixed> |
| 17 |
*/ |
| 18 |
public function decode(string $json): array { |
| 19 |
try { |
| 20 |
$decoded = json_decode($json, true, 512, JSON_THROW_ON_ERROR); |
| 21 |
} catch (JsonException $exception) { |
| 22 |
throw new DecodingException('Unable to decode JSON response.', 0, $exception); |
| 23 |
} |
| 24 |
|
| 25 |
if (!is_array($decoded)) { |
| 26 |
throw new DecodingException('Decoded JSON response must be an array.'); |
| 27 |
} |
| 28 |
|
| 29 |
return $decoded; |
| 30 |
} |
| 31 |
} |
| 32 |
|