MessageInterface.php 6.8 KB

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