Response.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC\Http\Client;
  9. use OCP\Http\Client\IResponse;
  10. use Psr\Http\Message\ResponseInterface;
  11. /**
  12. * Class Response
  13. *
  14. * @package OC\Http
  15. */
  16. class Response implements IResponse {
  17. /** @var ResponseInterface */
  18. private $response;
  19. /**
  20. * @var bool
  21. */
  22. private $stream;
  23. /**
  24. * @param ResponseInterface $response
  25. * @param bool $stream
  26. */
  27. public function __construct(ResponseInterface $response, $stream = false) {
  28. $this->response = $response;
  29. $this->stream = $stream;
  30. }
  31. /**
  32. * @return string|resource
  33. */
  34. public function getBody() {
  35. return $this->stream ?
  36. $this->response->getBody()->detach():
  37. $this->response->getBody()->getContents();
  38. }
  39. /**
  40. * @return int
  41. */
  42. public function getStatusCode(): int {
  43. return $this->response->getStatusCode();
  44. }
  45. /**
  46. * @param string $key
  47. * @return string
  48. */
  49. public function getHeader(string $key): string {
  50. $headers = $this->response->getHeader($key);
  51. if (count($headers) === 0) {
  52. return '';
  53. }
  54. return $headers[0];
  55. }
  56. /**
  57. * @return array
  58. */
  59. public function getHeaders(): array {
  60. return $this->response->getHeaders();
  61. }
  62. }