Response.php 639 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. namespace WeWork\Http;
  3. class Response extends \GuzzleHttp\Psr7\Response
  4. {
  5. /**
  6. * @inheritdoc
  7. */
  8. public function getBody()
  9. {
  10. $stream = parent::getBody();
  11. $data = json_decode((string)$stream, true);
  12. if (JSON_ERROR_NONE === json_last_error() && $data['errcode'] !== 0) {
  13. throw new \InvalidArgumentException($data['errmsg'], $data['errcode']);
  14. }
  15. return $stream;
  16. }
  17. /**
  18. * @return array
  19. */
  20. public function toArray(): array
  21. {
  22. return \GuzzleHttp\json_decode((string)$this->getBody(), true);
  23. }
  24. }