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