ServerRequestInterface.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. declare(strict_types=1);
  3. namespace Psr\Http\Message;
  4. /**
  5. * Representation of an incoming, server-side HTTP request.
  6. *
  7. * Per the HTTP specification, this interface includes properties for
  8. * each of the following:
  9. *
  10. * - Protocol version
  11. * - HTTP method
  12. * - URI
  13. * - Headers
  14. * - Message body
  15. *
  16. * Additionally, it encapsulates all data as it has arrived to the
  17. * application from the CGI and/or PHP environment, including:
  18. *
  19. * - The values represented in $_SERVER.
  20. * - Any cookies provided (generally via $_COOKIE)
  21. * - Query string arguments (generally via $_GET, or as parsed via parse_str())
  22. * - Upload files, if any (as represented by $_FILES)
  23. * - Deserialized body parameters (generally from $_POST)
  24. *
  25. * $_SERVER values MUST be treated as immutable, as they represent application
  26. * state at the time of request; as such, no methods are provided to allow
  27. * modification of those values. The other values provide such methods, as they
  28. * can be restored from $_SERVER or the request body, and may need treatment
  29. * during the application (e.g., body parameters may be deserialized based on
  30. * content type).
  31. *
  32. * Additionally, this interface recognizes the utility of introspecting a
  33. * request to derive and match additional parameters (e.g., via URI path
  34. * matching, decrypting cookie values, deserializing non-form-encoded body
  35. * content, matching authorization headers to users, etc). These parameters
  36. * are stored in an "attributes" property.
  37. *
  38. * Requests are considered immutable; all methods that might change state MUST
  39. * be implemented such that they retain the internal state of the current
  40. * message and return an instance that contains the changed state.
  41. */
  42. interface ServerRequestInterface extends RequestInterface
  43. {
  44. /**
  45. * Retrieve server parameters.
  46. *
  47. * Retrieves data related to the incoming request environment,
  48. * typically derived from PHP's $_SERVER superglobal. The data IS NOT
  49. * REQUIRED to originate from $_SERVER.
  50. *
  51. * @return array
  52. */
  53. public function getServerParams();
  54. /**
  55. * Retrieve cookies.
  56. *
  57. * Retrieves cookies sent by the client to the server.
  58. *
  59. * The data MUST be compatible with the structure of the $_COOKIE
  60. * superglobal.
  61. *
  62. * @return array
  63. */
  64. public function getCookieParams();
  65. /**
  66. * Return an instance with the specified cookies.
  67. *
  68. * The data IS NOT REQUIRED to come from the $_COOKIE superglobal, but MUST
  69. * be compatible with the structure of $_COOKIE. Typically, this data will
  70. * be injected at instantiation.
  71. *
  72. * This method MUST NOT update the related Cookie header of the request
  73. * instance, nor related values in the server params.
  74. *
  75. * This method MUST be implemented in such a way as to retain the
  76. * immutability of the message, and MUST return an instance that has the
  77. * updated cookie values.
  78. *
  79. * @param array $cookies Array of key/value pairs representing cookies.
  80. * @return static
  81. */
  82. public function withCookieParams(array $cookies);
  83. /**
  84. * Retrieve query string arguments.
  85. *
  86. * Retrieves the deserialized query string arguments, if any.
  87. *
  88. * Note: the query params might not be in sync with the URI or server
  89. * params. If you need to ensure you are only getting the original
  90. * values, you may need to parse the query string from `getUri()->getQuery()`
  91. * or from the `QUERY_STRING` server param.
  92. *
  93. * @return array
  94. */
  95. public function getQueryParams();
  96. /**
  97. * Return an instance with the specified query string arguments.
  98. *
  99. * These values SHOULD remain immutable over the course of the incoming
  100. * request. They MAY be injected during instantiation, such as from PHP's
  101. * $_GET superglobal, or MAY be derived from some other value such as the
  102. * URI. In cases where the arguments are parsed from the URI, the data
  103. * MUST be compatible with what PHP's parse_str() would return for
  104. * purposes of how duplicate query parameters are handled, and how nested
  105. * sets are handled.
  106. *
  107. * Setting query string arguments MUST NOT change the URI stored by the
  108. * request, nor the values in the server params.
  109. *
  110. * This method MUST be implemented in such a way as to retain the
  111. * immutability of the message, and MUST return an instance that has the
  112. * updated query string arguments.
  113. *
  114. * @param array $query Array of query string arguments, typically from
  115. * $_GET.
  116. * @return static
  117. */
  118. public function withQueryParams(array $query);
  119. /**
  120. * Retrieve normalized file upload data.
  121. *
  122. * This method returns upload metadata in a normalized tree, with each leaf
  123. * an instance of Psr\Http\Message\UploadedFileInterface.
  124. *
  125. * These values MAY be prepared from $_FILES or the message body during
  126. * instantiation, or MAY be injected via withUploadedFiles().
  127. *
  128. * @return array An array tree of UploadedFileInterface instances; an empty
  129. * array MUST be returned if no data is present.
  130. */
  131. public function getUploadedFiles();
  132. /**
  133. * Create a new instance with the specified uploaded files.
  134. *
  135. * This method MUST be implemented in such a way as to retain the
  136. * immutability of the message, and MUST return an instance that has the
  137. * updated body parameters.
  138. *
  139. * @param array $uploadedFiles An array tree of UploadedFileInterface instances.
  140. * @return static
  141. * @throws \InvalidArgumentException if an invalid structure is provided.
  142. */
  143. public function withUploadedFiles(array $uploadedFiles);
  144. /**
  145. * Retrieve any parameters provided in the request body.
  146. *
  147. * If the request Content-Type is either application/x-www-form-urlencoded
  148. * or multipart/form-data, and the request method is POST, this method MUST
  149. * return the contents of $_POST.
  150. *
  151. * Otherwise, this method may return any results of deserializing
  152. * the request body content; as parsing returns structured content, the
  153. * potential types MUST be arrays or objects only. A null value indicates
  154. * the absence of body content.
  155. *
  156. * @return null|array|object The deserialized body parameters, if any.
  157. * These will typically be an array or object.
  158. */
  159. public function getParsedBody();
  160. /**
  161. * Return an instance with the specified body parameters.
  162. *
  163. * These MAY be injected during instantiation.
  164. *
  165. * If the request Content-Type is either application/x-www-form-urlencoded
  166. * or multipart/form-data, and the request method is POST, use this method
  167. * ONLY to inject the contents of $_POST.
  168. *
  169. * The data IS NOT REQUIRED to come from $_POST, but MUST be the results of
  170. * deserializing the request body content. Deserialization/parsing returns
  171. * structured data, and, as such, this method ONLY accepts arrays or objects,
  172. * or a null value if nothing was available to parse.
  173. *
  174. * As an example, if content negotiation determines that the request data
  175. * is a JSON payload, this method could be used to create a request
  176. * instance with the deserialized parameters.
  177. *
  178. * This method MUST be implemented in such a way as to retain the
  179. * immutability of the message, and MUST return an instance that has the
  180. * updated body parameters.
  181. *
  182. * @param null|array|object $data The deserialized body data. This will
  183. * typically be in an array or object.
  184. * @return static
  185. * @throws \InvalidArgumentException if an unsupported argument type is
  186. * provided.
  187. */
  188. public function withParsedBody($data);
  189. /**
  190. * Retrieve attributes derived from the request.
  191. *
  192. * The request "attributes" may be used to allow injection of any
  193. * parameters derived from the request: e.g., the results of path
  194. * match operations; the results of decrypting cookies; the results of
  195. * deserializing non-form-encoded message bodies; etc. Attributes
  196. * will be application and request specific, and CAN be mutable.
  197. *
  198. * @return array Attributes derived from the request.
  199. */
  200. public function getAttributes();
  201. /**
  202. * Retrieve a single derived request attribute.
  203. *
  204. * Retrieves a single derived request attribute as described in
  205. * getAttributes(). If the attribute has not been previously set, returns
  206. * the default value as provided.
  207. *
  208. * This method obviates the need for a hasAttribute() method, as it allows
  209. * specifying a default value to return if the attribute is not found.
  210. *
  211. * @see getAttributes()
  212. * @param string $name The attribute name.
  213. * @param mixed $default Default value to return if the attribute does not exist.
  214. * @return mixed
  215. */
  216. public function getAttribute(string $name, $default = null);
  217. /**
  218. * Return an instance with the specified derived request attribute.
  219. *
  220. * This method allows setting a single derived request attribute as
  221. * described in getAttributes().
  222. *
  223. * This method MUST be implemented in such a way as to retain the
  224. * immutability of the message, and MUST return an instance that has the
  225. * updated attribute.
  226. *
  227. * @see getAttributes()
  228. * @param string $name The attribute name.
  229. * @param mixed $value The value of the attribute.
  230. * @return static
  231. */
  232. public function withAttribute(string $name, $value);
  233. /**
  234. * Return an instance that removes the specified derived request attribute.
  235. *
  236. * This method allows removing a single derived request attribute as
  237. * described in getAttributes().
  238. *
  239. * This method MUST be implemented in such a way as to retain the
  240. * immutability of the message, and MUST return an instance that removes
  241. * the attribute.
  242. *
  243. * @see getAttributes()
  244. * @param string $name The attribute name.
  245. * @return static
  246. */
  247. public function withoutAttribute(string $name);
  248. }