Response.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace oauth2\components;
  3. use Yii;
  4. use Psr\Http\Message\ResponseInterface;
  5. use Psr\Http\Message\StreamInterface;
  6. /**
  7. * Class Response
  8. * @package common\models\oauth2
  9. * @author jianyan74 <751393839@qq.com>
  10. */
  11. class Response implements ResponseInterface
  12. {
  13. /**
  14. * @var Stream
  15. */
  16. protected $stream;
  17. /**
  18. * Gets the response status code.
  19. *
  20. * The status code is a 3-digit integer result code of the server's attempt
  21. * to understand and satisfy the request.
  22. *
  23. * @return int Status code.
  24. */
  25. public function getStatusCode()
  26. {
  27. return Yii::$app->response->statusCode;
  28. }
  29. /**
  30. * Return an instance with the specified status code and, optionally, reason phrase.
  31. *
  32. * If no reason phrase is specified, implementations MAY choose to default
  33. * to the RFC 7231 or IANA recommended reason phrase for the response's
  34. * status code.
  35. *
  36. * This method MUST be implemented in such a way as to retain the
  37. * immutability of the message, and MUST return an instance that has the
  38. * updated status and reason phrase.
  39. *
  40. * @link http://tools.ietf.org/html/rfc7231#section-6
  41. * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
  42. * @param int $code The 3-digit integer result code to set.
  43. * @param string $reasonPhrase The reason phrase to use with the
  44. * provided status code; if none is provided, implementations MAY
  45. * use the defaults as suggested in the HTTP specification.
  46. * @return static
  47. * @throws \InvalidArgumentException For invalid status code arguments.
  48. */
  49. public function withStatus($code, $reasonPhrase = '')
  50. {
  51. Yii::$app->response->setStatusCode($code, $reasonPhrase);
  52. return $this;
  53. }
  54. /**
  55. * Gets the response reason phrase associated with the status code.
  56. *
  57. * Because a reason phrase is not a required element in a response
  58. * status line, the reason phrase value MAY be null. Implementations MAY
  59. * choose to return the default RFC 7231 recommended reason phrase (or those
  60. * listed in the IANA HTTP Status Code Registry) for the response's
  61. * status code.
  62. *
  63. * @link http://tools.ietf.org/html/rfc7231#section-6
  64. * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
  65. * @return string Reason phrase; must return an empty string if none present.
  66. */
  67. public function getReasonPhrase()
  68. {
  69. }
  70. /**
  71. * Retrieves the HTTP protocol version as a string.
  72. *
  73. * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0").
  74. *
  75. * @return string HTTP protocol version.
  76. */
  77. public function getProtocolVersion()
  78. {
  79. return Yii::$app->response->version;
  80. }
  81. /**
  82. * Return an instance with the specified HTTP protocol version.
  83. *
  84. * The version string MUST contain only the HTTP version number (e.g.,
  85. * "1.1", "1.0").
  86. *
  87. * This method MUST be implemented in such a way as to retain the
  88. * immutability of the message, and MUST return an instance that has the
  89. * new protocol version.
  90. *
  91. * @param string $version HTTP protocol version
  92. * @return static
  93. */
  94. public function withProtocolVersion($version)
  95. {
  96. }
  97. /**
  98. * Retrieves all message header values.
  99. *
  100. * The keys represent the header name as it will be sent over the wire, and
  101. * each value is an array of strings associated with the header.
  102. *
  103. * // Represent the headers as a string
  104. * foreach ($message->getHeaders() as $name => $values) {
  105. * echo $name . ": " . implode(", ", $values);
  106. * }
  107. *
  108. * // Emit headers iteratively:
  109. * foreach ($message->getHeaders() as $name => $values) {
  110. * foreach ($values as $value) {
  111. * header(sprintf('%s: %s', $name, $value), false);
  112. * }
  113. * }
  114. *
  115. * While header names are not case-sensitive, getHeaders() will preserve the
  116. * exact case in which headers were originally specified.
  117. *
  118. * @return string[][] Returns an associative array of the message's headers. Each
  119. * key MUST be a header name, and each value MUST be an array of strings
  120. * for that header.
  121. */
  122. public function getHeaders()
  123. {
  124. return Yii::$app->response->headers;
  125. }
  126. /**
  127. * Checks if a header exists by the given case-insensitive name.
  128. *
  129. * @param string $name Case-insensitive header field name.
  130. * @return bool Returns true if any header names match the given header
  131. * name using a case-insensitive string comparison. Returns false if
  132. * no matching header name is found in the message.
  133. */
  134. public function hasHeader($name)
  135. {
  136. return Yii::$app->response->headers->has($name);
  137. }
  138. /**
  139. * Retrieves a message header value by the given case-insensitive name.
  140. *
  141. * This method returns an array of all the header values of the given
  142. * case-insensitive header name.
  143. *
  144. * If the header does not appear in the message, this method MUST return an
  145. * empty array.
  146. *
  147. * @param string $name Case-insensitive header field name.
  148. * @return string[] An array of string values as provided for the given
  149. * header. If the header does not appear in the message, this method MUST
  150. * return an empty array.
  151. */
  152. public function getHeader($name)
  153. {
  154. return Yii::$app->response->headers->get($name);
  155. }
  156. /**
  157. * Retrieves a comma-separated string of the values for a single header.
  158. *
  159. * This method returns all of the header values of the given
  160. * case-insensitive header name as a string concatenated together using
  161. * a comma.
  162. *
  163. * NOTE: Not all header values may be appropriately represented using
  164. * comma concatenation. For such headers, use getHeader() instead
  165. * and supply your own delimiter when concatenating.
  166. *
  167. * If the header does not appear in the message, this method MUST return
  168. * an empty string.
  169. *
  170. * @param string $name Case-insensitive header field name.
  171. * @return string A string of values as provided for the given header
  172. * concatenated together using a comma. If the header does not appear in
  173. * the message, this method MUST return an empty string.
  174. */
  175. public function getHeaderLine($name)
  176. {
  177. }
  178. /**
  179. * Return an instance with the provided value replacing the specified header.
  180. *
  181. * While header names are case-insensitive, the casing of the header will
  182. * be preserved by this function, and returned from getHeaders().
  183. *
  184. * This method MUST be implemented in such a way as to retain the
  185. * immutability of the message, and MUST return an instance that has the
  186. * new and/or updated header and value.
  187. *
  188. * @param string $name Case-insensitive header field name.
  189. * @param string|string[] $value Header value(s).
  190. * @return static
  191. * @throws \InvalidArgumentException for invalid header names or values.
  192. */
  193. public function withHeader($name, $value)
  194. {
  195. Yii::$app->response->headers->set($name, $value);
  196. return $this;
  197. }
  198. /**
  199. * Return an instance with the specified header appended with the given value.
  200. *
  201. * Existing values for the specified header will be maintained. The new
  202. * value(s) will be appended to the existing list. If the header did not
  203. * exist previously, it will be added.
  204. *
  205. * This method MUST be implemented in such a way as to retain the
  206. * immutability of the message, and MUST return an instance that has the
  207. * new header and/or value.
  208. *
  209. * @param string $name Case-insensitive header field name to add.
  210. * @param string|string[] $value Header value(s).
  211. * @return static
  212. * @throws \InvalidArgumentException for invalid header names or values.
  213. */
  214. public function withAddedHeader($name, $value)
  215. {
  216. Yii::$app->response->headers->add($name, $value);
  217. return $this;
  218. }
  219. /**
  220. * Return an instance without the specified header.
  221. *
  222. * Header resolution MUST be done without case-sensitivity.
  223. *
  224. * This method MUST be implemented in such a way as to retain the
  225. * immutability of the message, and MUST return an instance that removes
  226. * the named header.
  227. *
  228. * @param string $name Case-insensitive header field name to remove.
  229. * @return static
  230. */
  231. public function withoutHeader($name)
  232. {
  233. }
  234. /**
  235. * Gets the body of the message.
  236. *
  237. * @return StreamInterface Returns the body as a stream.
  238. */
  239. public function getBody()
  240. {
  241. if (!$this->stream) {
  242. $this->stream = new Stream();
  243. }
  244. return $this->stream;
  245. }
  246. /**
  247. * Return an instance with the specified message body.
  248. *
  249. * The body MUST be a StreamInterface object.
  250. *
  251. * This method MUST be implemented in such a way as to retain the
  252. * immutability of the message, and MUST return a new instance that has the
  253. * new body stream.
  254. *
  255. * @param StreamInterface $body Body.
  256. * @return static
  257. * @throws \InvalidArgumentException When the body is not valid.
  258. */
  259. public function withBody(StreamInterface $body)
  260. {
  261. $this->stream = $body;
  262. return $this;
  263. }
  264. }
粤ICP备19079148号