ServerRequestInterface.php 9.9 KB

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