TextPlainResponse.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCP\AppFramework\Http;
  8. use OCP\AppFramework\Http;
  9. /**
  10. * A renderer for text responses
  11. * @since 22.0.0
  12. * @template S of int
  13. * @template H of array<string, mixed>
  14. * @template-extends Response<int, array<string, mixed>>
  15. */
  16. class TextPlainResponse extends Response {
  17. /** @var string */
  18. private $text = '';
  19. /**
  20. * constructor of TextPlainResponse
  21. * @param string $text The text body
  22. * @param S $statusCode the Http status code, defaults to 200
  23. * @param H $headers
  24. * @since 22.0.0
  25. */
  26. public function __construct(string $text = '', int $statusCode = Http::STATUS_OK, array $headers = []) {
  27. parent::__construct($statusCode, $headers);
  28. $this->text = $text;
  29. $this->addHeader('Content-Type', 'text/plain');
  30. }
  31. /**
  32. * Returns the text
  33. * @return string
  34. * @since 22.0.0
  35. * @throws \Exception If data could not get encoded
  36. */
  37. public function render() : string {
  38. return $this->text;
  39. }
  40. }