Response.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @package Grav\Framework\Psr7
  5. *
  6. * @copyright Copyright (c) 2015 - 2022 Trilby Media, LLC. All rights reserved.
  7. * @license MIT License; see LICENSE file for details.
  8. */
  9. namespace Grav\Framework\Psr7;
  10. use Grav\Framework\Psr7\Traits\ResponseDecoratorTrait;
  11. use Psr\Http\Message\ResponseInterface;
  12. use Psr\Http\Message\StreamInterface;
  13. use RuntimeException;
  14. use function in_array;
  15. /**
  16. * Class Response
  17. * @package Slim\Http
  18. */
  19. class Response implements ResponseInterface
  20. {
  21. use ResponseDecoratorTrait;
  22. /** @var string EOL characters used for HTTP response. */
  23. private const EOL = "\r\n";
  24. /**
  25. * @param int $status Status code
  26. * @param array $headers Response headers
  27. * @param string|null|resource|StreamInterface $body Response body
  28. * @param string $version Protocol version
  29. * @param string|null $reason Reason phrase (optional)
  30. */
  31. public function __construct(int $status = 200, array $headers = [], $body = null, string $version = '1.1', string $reason = null)
  32. {
  33. $this->message = new \Nyholm\Psr7\Response($status, $headers, $body, $version, $reason);
  34. }
  35. /**
  36. * Json.
  37. *
  38. * Note: This method is not part of the PSR-7 standard.
  39. *
  40. * This method prepares the response object to return an HTTP Json
  41. * response to the client.
  42. *
  43. * @param mixed $data The data
  44. * @param int|null $status The HTTP status code.
  45. * @param int $options Json encoding options
  46. * @param int $depth Json encoding max depth
  47. * @return static
  48. * @phpstan-param positive-int $depth
  49. */
  50. public function withJson($data, int $status = null, int $options = 0, int $depth = 512): ResponseInterface
  51. {
  52. $json = (string) json_encode($data, $options, $depth);
  53. if (json_last_error() !== JSON_ERROR_NONE) {
  54. throw new RuntimeException(json_last_error_msg(), json_last_error());
  55. }
  56. $response = $this->getResponse()
  57. ->withHeader('Content-Type', 'application/json;charset=utf-8')
  58. ->withBody(new Stream($json));
  59. if ($status !== null) {
  60. $response = $response->withStatus($status);
  61. }
  62. $new = clone $this;
  63. $new->message = $response;
  64. return $new;
  65. }
  66. /**
  67. * Redirect.
  68. *
  69. * Note: This method is not part of the PSR-7 standard.
  70. *
  71. * This method prepares the response object to return an HTTP Redirect
  72. * response to the client.
  73. *
  74. * @param string $url The redirect destination.
  75. * @param int|null $status The redirect HTTP status code.
  76. * @return static
  77. */
  78. public function withRedirect(string $url, $status = null): ResponseInterface
  79. {
  80. $response = $this->getResponse()->withHeader('Location', $url);
  81. if ($status === null) {
  82. $status = 302;
  83. }
  84. $new = clone $this;
  85. $new->message = $response->withStatus($status);
  86. return $new;
  87. }
  88. /**
  89. * Is this response empty?
  90. *
  91. * Note: This method is not part of the PSR-7 standard.
  92. *
  93. * @return bool
  94. */
  95. public function isEmpty(): bool
  96. {
  97. return in_array($this->getResponse()->getStatusCode(), [204, 205, 304], true);
  98. }
  99. /**
  100. * Is this response OK?
  101. *
  102. * Note: This method is not part of the PSR-7 standard.
  103. *
  104. * @return bool
  105. */
  106. public function isOk(): bool
  107. {
  108. return $this->getResponse()->getStatusCode() === 200;
  109. }
  110. /**
  111. * Is this response a redirect?
  112. *
  113. * Note: This method is not part of the PSR-7 standard.
  114. *
  115. * @return bool
  116. */
  117. public function isRedirect(): bool
  118. {
  119. return in_array($this->getResponse()->getStatusCode(), [301, 302, 303, 307, 308], true);
  120. }
  121. /**
  122. * Is this response forbidden?
  123. *
  124. * Note: This method is not part of the PSR-7 standard.
  125. *
  126. * @return bool
  127. * @api
  128. */
  129. public function isForbidden(): bool
  130. {
  131. return $this->getResponse()->getStatusCode() === 403;
  132. }
  133. /**
  134. * Is this response not Found?
  135. *
  136. * Note: This method is not part of the PSR-7 standard.
  137. *
  138. * @return bool
  139. */
  140. public function isNotFound(): bool
  141. {
  142. return $this->getResponse()->getStatusCode() === 404;
  143. }
  144. /**
  145. * Is this response informational?
  146. *
  147. * Note: This method is not part of the PSR-7 standard.
  148. *
  149. * @return bool
  150. */
  151. public function isInformational(): bool
  152. {
  153. $response = $this->getResponse();
  154. return $response->getStatusCode() >= 100 && $response->getStatusCode() < 200;
  155. }
  156. /**
  157. * Is this response successful?
  158. *
  159. * Note: This method is not part of the PSR-7 standard.
  160. *
  161. * @return bool
  162. */
  163. public function isSuccessful(): bool
  164. {
  165. $response = $this->getResponse();
  166. return $response->getStatusCode() >= 200 && $response->getStatusCode() < 300;
  167. }
  168. /**
  169. * Is this response a redirection?
  170. *
  171. * Note: This method is not part of the PSR-7 standard.
  172. *
  173. * @return bool
  174. */
  175. public function isRedirection(): bool
  176. {
  177. $response = $this->getResponse();
  178. return $response->getStatusCode() >= 300 && $response->getStatusCode() < 400;
  179. }
  180. /**
  181. * Is this response a client error?
  182. *
  183. * Note: This method is not part of the PSR-7 standard.
  184. *
  185. * @return bool
  186. */
  187. public function isClientError(): bool
  188. {
  189. $response = $this->getResponse();
  190. return $response->getStatusCode() >= 400 && $response->getStatusCode() < 500;
  191. }
  192. /**
  193. * Is this response a server error?
  194. *
  195. * Note: This method is not part of the PSR-7 standard.
  196. *
  197. * @return bool
  198. */
  199. public function isServerError(): bool
  200. {
  201. $response = $this->getResponse();
  202. return $response->getStatusCode() >= 500 && $response->getStatusCode() < 600;
  203. }
  204. /**
  205. * Convert response to string.
  206. *
  207. * Note: This method is not part of the PSR-7 standard.
  208. *
  209. * @return string
  210. */
  211. public function __toString(): string
  212. {
  213. $response = $this->getResponse();
  214. $output = sprintf(
  215. 'HTTP/%s %s %s%s',
  216. $response->getProtocolVersion(),
  217. $response->getStatusCode(),
  218. $response->getReasonPhrase(),
  219. self::EOL
  220. );
  221. foreach ($response->getHeaders() as $name => $values) {
  222. $output .= sprintf('%s: %s', $name, $response->getHeaderLine($name)) . self::EOL;
  223. }
  224. $output .= self::EOL;
  225. $output .= $response->getBody();
  226. return $output;
  227. }
  228. }