| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- <?php
- declare(strict_types=1);
- /*
- * Copyright (C) 2013 Mailgun
- *
- * This software may be modified and distributed under the terms
- * of the MIT license. See the LICENSE file for details.
- */
- namespace Mailgun\Hydrator;
- use Mailgun\Exception\HydrationException;
- use Psr\Http\Message\ResponseInterface;
- /**
- * Serialize an HTTP response to array.
- *
- * @author Tobias Nyholm <tobias.nyholm@gmail.com>
- */
- final class ArrayHydrator implements Hydrator
- {
- /**
- * @param class-string $class
- * @return array
- * @throws \JsonException
- */
- public function hydrate(ResponseInterface $response, string $class)
- {
- $body = $response->getBody()->__toString();
- if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
- throw new HydrationException('The ArrayHydrator cannot hydrate response with Content-Type:'.$response->getHeaderLine('Content-Type'));
- }
- try {
- $content = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
- } catch (\JsonException $exception) {
- throw new HydrationException(sprintf('Error (%d) when trying to json_decode response: %s', $exception->getCode(), $exception->getMessage()));
- }
- return $content;
- }
- }
|