<?php
/**
 * @license GPL-2.0-or-later
 *
 * Modified using Strauss.
 * @see https://github.com/BrianHenryIE/strauss
 */ declare(strict_types=1);

namespace RCP\LiquidWeb\LicensingApiClient\Http;

use JsonException;
use RCP\LiquidWeb\LicensingApiClient\Exceptions\DecodingException;

/**
 * Decodes JSON response bodies into arrays for response mappers.
 */
final class JsonDecoder
{
	/**
	 *
	 * @throws DecodingException
	 * @return array<array-key, mixed>
	 */
	public function decode(string $json): array {
		try {
			$decoded = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
		} catch (JsonException $exception) {
			throw new DecodingException('Unable to decode JSON response.', 0, $exception);
		}

		if (!is_array($decoded)) {
			throw new DecodingException('Decoded JSON response must be an array.');
		}

		return $decoded;
	}
}
