No Description

MessageInterface.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace Psr\Http\Message;
  3. /**
  4. * HTTP messages consist of requests from a client to a server and responses
  5. * from a server to a client. This interface defines the methods common to
  6. * each.
  7. *
  8. * Messages are considered immutable; all methods that might change state MUST
  9. * be implemented such that they retain the internal state of the current
  10. * message and return an instance that contains the changed state.
  11. *
  12. * @link http://www.ietf.org/rfc/rfc7230.txt
  13. * @link http://www.ietf.org/rfc/rfc7231.txt
  14. */
  15. interface MessageInterface
  16. {
  17. /**
  18. * Retrieves the HTTP protocol version as a string.
  19. *
  20. * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0").
  21. *
  22. * @return string HTTP protocol version.
  23. */
  24. public function getProtocolVersion();
  25. /**
  26. * Return an instance with the specified HTTP protocol version.
  27. *
  28. * The version string MUST contain only the HTTP version number (e.g.,
  29. * "1.1", "1.0").
  30. *
  31. * This method MUST be implemented in such a way as to retain the
  32. * immutability of the message, and MUST return an instance that has the
  33. * new protocol version.
  34. *
  35. * @param string $version HTTP protocol version
  36. * @return self
  37. */
  38. public function withProtocolVersion($version);
  39. /**
  40. * Retrieves all message header values.
  41. *
  42. * The keys represent the header name as it will be sent over the wire, and
  43. * each value is an array of strings associated with the header.
  44. *
  45. * // Represent the headers as a string
  46. * foreach ($message->getHeaders() as $name => $values) {
  47. * echo $name . ": " . implode(", ", $values);
  48. * }
  49. *
  50. * // Emit headers iteratively:
  51. * foreach ($message->getHeaders() as $name => $values) {
  52. * foreach ($values as $value) {
  53. * header(sprintf('%s: %s', $name, $value), false);
  54. * }
  55. * }
  56. *
  57. * While header names are not case-sensitive, getHeaders() will preserve the
  58. * exact case in which headers were originally specified.
  59. *
  60. * @return array Returns an associative array of the message's headers. Each
  61. * key MUST be a header name, and each value MUST be an array of strings
  62. * for that header.
  63. */
  64. public function getHeaders();
  65. /**
  66. * Checks if a header exists by the given case-insensitive name.
  67. *
  68. * @param string $name Case-insensitive header field name.
  69. * @return bool Returns true if any header names match the given header
  70. * name using a case-insensitive string comparison. Returns false if
  71. * no matching header name is found in the message.
  72. */
  73. public function hasHeader($name);
  74. /**
  75. * Retrieves a message header value by the given case-insensitive name.
  76. *
  77. * This method returns an array of all the header values of the given
  78. * case-insensitive header name.
  79. *
  80. * If the header does not appear in the message, this method MUST return an
  81. * empty array.
  82. *
  83. * @param string $name Case-insensitive header field name.
  84. * @return string[] An array of string values as provided for the given
  85. * header. If the header does not appear in the message, this method MUST
  86. * return an empty array.
  87. */
  88. public function getHeader($name);
  89. /**
  90. * Retrieves a comma-separated string of the values for a single header.
  91. *
  92. * This method returns all of the header values of the given
  93. * case-insensitive header name as a string concatenated together using
  94. * a comma.
  95. *
  96. * NOTE: Not all header values may be appropriately represented using
  97. * comma concatenation. For such headers, use getHeader() instead
  98. * and supply your own delimiter when concatenating.
  99. *
  100. * If the header does not appear in the message, this method MUST return
  101. * an empty string.
  102. *
  103. * @param string $name Case-insensitive header field name.
  104. * @return string A string of values as provided for the given header
  105. * concatenated together using a comma. If the header does not appear in
  106. * the message, this method MUST return an empty string.
  107. */
  108. public function getHeaderLine($name);
  109. /**
  110. * Return an instance with the provided value replacing the specified header.
  111. *
  112. * While header names are case-insensitive, the casing of the header will
  113. * be preserved by this function, and returned from getHeaders().
  114. *
  115. * This method MUST be implemented in such a way as to retain the
  116. * immutability of the message, and MUST return an instance that has the
  117. * new and/or updated header and value.
  118. *
  119. * @param string $name Case-insensitive header field name.
  120. * @param string|string[] $value Header value(s).
  121. * @return self
  122. * @throws \InvalidArgumentException for invalid header names or values.
  123. */
  124. public function withHeader($name, $value);
  125. /**
  126. * Return an instance with the specified header appended with the given value.
  127. *
  128. * Existing values for the specified header will be maintained. The new
  129. * value(s) will be appended to the existing list. If the header did not
  130. * exist previously, it will be added.
  131. *
  132. * This method MUST be implemented in such a way as to retain the
  133. * immutability of the message, and MUST return an instance that has the
  134. * new header and/or value.
  135. *
  136. * @param string $name Case-insensitive header field name to add.
  137. * @param string|string[] $value Header value(s).
  138. * @return self
  139. * @throws \InvalidArgumentException for invalid header names or values.
  140. */
  141. public function withAddedHeader($name, $value);
  142. /**
  143. * Return an instance without the specified header.
  144. *
  145. * Header resolution MUST be done without case-sensitivity.
  146. *
  147. * This method MUST be implemented in such a way as to retain the
  148. * immutability of the message, and MUST return an instance that removes
  149. * the named header.
  150. *
  151. * @param string $name Case-insensitive header field name to remove.
  152. * @return self
  153. */
  154. public function withoutHeader($name);
  155. /**
  156. * Gets the body of the message.
  157. *
  158. * @return StreamInterface Returns the body as a stream.
  159. */
  160. public function getBody();
  161. /**
  162. * Return an instance with the specified message body.
  163. *
  164. * The body MUST be a StreamInterface object.
  165. *
  166. * This method MUST be implemented in such a way as to retain the
  167. * immutability of the message, and MUST return a new instance that has the
  168. * new body stream.
  169. *
  170. * @param StreamInterface $body Body.
  171. * @return self
  172. * @throws \InvalidArgumentException When the body is not valid.
  173. */
  174. public function withBody(StreamInterface $body);
  175. }