ResponseInterface.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\HttpServer\Contract;
  12. use Hyperf\Contract\Arrayable;
  13. use Hyperf\Contract\Jsonable;
  14. use Hyperf\Contract\Xmlable;
  15. use Hyperf\HttpMessage\Cookie\Cookie;
  16. use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
  17. use Stringable;
  18. interface ResponseInterface
  19. {
  20. /**
  21. * Format data to JSON and return data with Content-Type:application/json header.
  22. *
  23. * @param array|Arrayable|Jsonable $data
  24. */
  25. public function json($data): PsrResponseInterface;
  26. /**
  27. * Format data to XML and return data with Content-Type:application/xml header.
  28. *
  29. * @param array|Arrayable|Xmlable $data
  30. * @param string $root the name of the root node
  31. */
  32. public function xml($data, string $root = 'root', string $charset = 'utf-8'): PsrResponseInterface;
  33. /**
  34. * Format data to a string and return data with Content-Type:text/plain header.
  35. * @param mixed|Stringable $data
  36. */
  37. public function raw($data, string $charset = 'utf-8'): PsrResponseInterface;
  38. /**
  39. * return data with content-type:text/html header.
  40. */
  41. public function html(string $html, string $charset = 'utf-8'): PsrResponseInterface;
  42. /**
  43. * Redirect to a URL.
  44. */
  45. public function redirect(string $toUrl, int $status = 302, string $schema = 'http'): PsrResponseInterface;
  46. /**
  47. * Create a file download response.
  48. *
  49. * @param string $file the file path which want to send to client
  50. * @param string $name the alias name of the file that client receive
  51. */
  52. public function download(string $file, string $name = ''): PsrResponseInterface;
  53. /**
  54. * Chunked transfer encoding.
  55. */
  56. public function write(string $data): bool;
  57. /**
  58. * Override a response with a cookie.
  59. */
  60. public function withCookie(Cookie $cookie): ResponseInterface;
  61. }