File indexing completed on 2025-01-26 05:29:15

0001 <?php
0002 
0003 namespace Psr\Http\Message;
0004 
0005 /**
0006  * Representation of an incoming, server-side HTTP request.
0007  *
0008  * Per the HTTP specification, this interface includes properties for
0009  * each of the following:
0010  *
0011  * - Protocol version
0012  * - HTTP method
0013  * - URI
0014  * - Headers
0015  * - Message body
0016  *
0017  * Additionally, it encapsulates all data as it has arrived to the
0018  * application from the CGI and/or PHP environment, including:
0019  *
0020  * - The values represented in $_SERVER.
0021  * - Any cookies provided (generally via $_COOKIE)
0022  * - Query string arguments (generally via $_GET, or as parsed via parse_str())
0023  * - Upload files, if any (as represented by $_FILES)
0024  * - Deserialized body parameters (generally from $_POST)
0025  *
0026  * $_SERVER values MUST be treated as immutable, as they represent application
0027  * state at the time of request; as such, no methods are provided to allow
0028  * modification of those values. The other values provide such methods, as they
0029  * can be restored from $_SERVER or the request body, and may need treatment
0030  * during the application (e.g., body parameters may be deserialized based on
0031  * content type).
0032  *
0033  * Additionally, this interface recognizes the utility of introspecting a
0034  * request to derive and match additional parameters (e.g., via URI path
0035  * matching, decrypting cookie values, deserializing non-form-encoded body
0036  * content, matching authorization headers to users, etc). These parameters
0037  * are stored in an "attributes" property.
0038  *
0039  * Requests are considered immutable; all methods that might change state MUST
0040  * be implemented such that they retain the internal state of the current
0041  * message and return an instance that contains the changed state.
0042  */
0043 interface ServerRequestInterface extends RequestInterface
0044 {
0045     /**
0046      * Retrieve server parameters.
0047      *
0048      * Retrieves data related to the incoming request environment,
0049      * typically derived from PHP's $_SERVER superglobal. The data IS NOT
0050      * REQUIRED to originate from $_SERVER.
0051      *
0052      * @return array
0053      */
0054     public function getServerParams();
0055 
0056     /**
0057      * Retrieve cookies.
0058      *
0059      * Retrieves cookies sent by the client to the server.
0060      *
0061      * The data MUST be compatible with the structure of the $_COOKIE
0062      * superglobal.
0063      *
0064      * @return array
0065      */
0066     public function getCookieParams();
0067 
0068     /**
0069      * Return an instance with the specified cookies.
0070      *
0071      * The data IS NOT REQUIRED to come from the $_COOKIE superglobal, but MUST
0072      * be compatible with the structure of $_COOKIE. Typically, this data will
0073      * be injected at instantiation.
0074      *
0075      * This method MUST NOT update the related Cookie header of the request
0076      * instance, nor related values in the server params.
0077      *
0078      * This method MUST be implemented in such a way as to retain the
0079      * immutability of the message, and MUST return an instance that has the
0080      * updated cookie values.
0081      *
0082      * @param array $cookies Array of key/value pairs representing cookies.
0083      * @return static
0084      */
0085     public function withCookieParams(array $cookies);
0086 
0087     /**
0088      * Retrieve query string arguments.
0089      *
0090      * Retrieves the deserialized query string arguments, if any.
0091      *
0092      * Note: the query params might not be in sync with the URI or server
0093      * params. If you need to ensure you are only getting the original
0094      * values, you may need to parse the query string from `getUri()->getQuery()`
0095      * or from the `QUERY_STRING` server param.
0096      *
0097      * @return array
0098      */
0099     public function getQueryParams();
0100 
0101     /**
0102      * Return an instance with the specified query string arguments.
0103      *
0104      * These values SHOULD remain immutable over the course of the incoming
0105      * request. They MAY be injected during instantiation, such as from PHP's
0106      * $_GET superglobal, or MAY be derived from some other value such as the
0107      * URI. In cases where the arguments are parsed from the URI, the data
0108      * MUST be compatible with what PHP's parse_str() would return for
0109      * purposes of how duplicate query parameters are handled, and how nested
0110      * sets are handled.
0111      *
0112      * Setting query string arguments MUST NOT change the URI stored by the
0113      * request, nor the values in the server params.
0114      *
0115      * This method MUST be implemented in such a way as to retain the
0116      * immutability of the message, and MUST return an instance that has the
0117      * updated query string arguments.
0118      *
0119      * @param array $query Array of query string arguments, typically from
0120      *     $_GET.
0121      * @return static
0122      */
0123     public function withQueryParams(array $query);
0124 
0125     /**
0126      * Retrieve normalized file upload data.
0127      *
0128      * This method returns upload metadata in a normalized tree, with each leaf
0129      * an instance of Psr\Http\Message\UploadedFileInterface.
0130      *
0131      * These values MAY be prepared from $_FILES or the message body during
0132      * instantiation, or MAY be injected via withUploadedFiles().
0133      *
0134      * @return array An array tree of UploadedFileInterface instances; an empty
0135      *     array MUST be returned if no data is present.
0136      */
0137     public function getUploadedFiles();
0138 
0139     /**
0140      * Create a new instance with the specified uploaded files.
0141      *
0142      * This method MUST be implemented in such a way as to retain the
0143      * immutability of the message, and MUST return an instance that has the
0144      * updated body parameters.
0145      *
0146      * @param array $uploadedFiles An array tree of UploadedFileInterface instances.
0147      * @return static
0148      * @throws \InvalidArgumentException if an invalid structure is provided.
0149      */
0150     public function withUploadedFiles(array $uploadedFiles);
0151 
0152     /**
0153      * Retrieve any parameters provided in the request body.
0154      *
0155      * If the request Content-Type is either application/x-www-form-urlencoded
0156      * or multipart/form-data, and the request method is POST, this method MUST
0157      * return the contents of $_POST.
0158      *
0159      * Otherwise, this method may return any results of deserializing
0160      * the request body content; as parsing returns structured content, the
0161      * potential types MUST be arrays or objects only. A null value indicates
0162      * the absence of body content.
0163      *
0164      * @return null|array|object The deserialized body parameters, if any.
0165      *     These will typically be an array or object.
0166      */
0167     public function getParsedBody();
0168 
0169     /**
0170      * Return an instance with the specified body parameters.
0171      *
0172      * These MAY be injected during instantiation.
0173      *
0174      * If the request Content-Type is either application/x-www-form-urlencoded
0175      * or multipart/form-data, and the request method is POST, use this method
0176      * ONLY to inject the contents of $_POST.
0177      *
0178      * The data IS NOT REQUIRED to come from $_POST, but MUST be the results of
0179      * deserializing the request body content. Deserialization/parsing returns
0180      * structured data, and, as such, this method ONLY accepts arrays or objects,
0181      * or a null value if nothing was available to parse.
0182      *
0183      * As an example, if content negotiation determines that the request data
0184      * is a JSON payload, this method could be used to create a request
0185      * instance with the deserialized parameters.
0186      *
0187      * This method MUST be implemented in such a way as to retain the
0188      * immutability of the message, and MUST return an instance that has the
0189      * updated body parameters.
0190      *
0191      * @param null|array|object $data The deserialized body data. This will
0192      *     typically be in an array or object.
0193      * @return static
0194      * @throws \InvalidArgumentException if an unsupported argument type is
0195      *     provided.
0196      */
0197     public function withParsedBody($data);
0198 
0199     /**
0200      * Retrieve attributes derived from the request.
0201      *
0202      * The request "attributes" may be used to allow injection of any
0203      * parameters derived from the request: e.g., the results of path
0204      * match operations; the results of decrypting cookies; the results of
0205      * deserializing non-form-encoded message bodies; etc. Attributes
0206      * will be application and request specific, and CAN be mutable.
0207      *
0208      * @return array Attributes derived from the request.
0209      */
0210     public function getAttributes();
0211 
0212     /**
0213      * Retrieve a single derived request attribute.
0214      *
0215      * Retrieves a single derived request attribute as described in
0216      * getAttributes(). If the attribute has not been previously set, returns
0217      * the default value as provided.
0218      *
0219      * This method obviates the need for a hasAttribute() method, as it allows
0220      * specifying a default value to return if the attribute is not found.
0221      *
0222      * @see getAttributes()
0223      * @param string $name The attribute name.
0224      * @param mixed $default Default value to return if the attribute does not exist.
0225      * @return mixed
0226      */
0227     public function getAttribute($name, $default = null);
0228 
0229     /**
0230      * Return an instance with the specified derived request attribute.
0231      *
0232      * This method allows setting a single derived request attribute as
0233      * described in getAttributes().
0234      *
0235      * This method MUST be implemented in such a way as to retain the
0236      * immutability of the message, and MUST return an instance that has the
0237      * updated attribute.
0238      *
0239      * @see getAttributes()
0240      * @param string $name The attribute name.
0241      * @param mixed $value The value of the attribute.
0242      * @return static
0243      */
0244     public function withAttribute($name, $value);
0245 
0246     /**
0247      * Return an instance that removes the specified derived request attribute.
0248      *
0249      * This method allows removing a single derived request attribute as
0250      * described in getAttributes().
0251      *
0252      * This method MUST be implemented in such a way as to retain the
0253      * immutability of the message, and MUST return an instance that removes
0254      * the attribute.
0255      *
0256      * @see getAttributes()
0257      * @param string $name The attribute name.
0258      * @return static
0259      */
0260     public function withoutAttribute($name);
0261 }