diff --git a/src/ApiRequester.php b/src/ApiRequester.php index e4240e8..674b746 100644 --- a/src/ApiRequester.php +++ b/src/ApiRequester.php @@ -69,25 +69,28 @@ public function request($method, $endpoint, array $options = []) * @throws \Vindi\Exceptions\RequestException */ public function response(ResponseInterface $response) - { - $this->lastResponse = $response; - - $content = $response->getBody()->getContents(); +{ + $this->lastResponse = $response; - $decoded = json_decode($content); // parse as object - $data = $decoded; + $content = $response->getBody()->getContents(); - if (!empty($decoded)) { - reset($decoded); - $data = current($decoded); // get first attribute from array, e.g.: subscription, subscriptions, errors. - } + // Decodifica como array ao invés de objeto + $decoded = json_decode($content, true); - $this->checkRateLimit($response) - ->checkForErrors($response, $data); + $data = $decoded; - return $data; + if (!empty($decoded)) { + // Se $decoded é um array, obtém o primeiro elemento + $data = is_array($decoded) ? reset($decoded) : $decoded; } + $this->checkRateLimit($response) + ->checkForErrors($response, $data); + + return $data; +} + + /** * @param ResponseInterface $response *