3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
8 namespace Zend\Diactoros;
10 use InvalidArgumentException;
11 use Psr\Http\Message\ServerRequestInterface;
12 use Psr\Http\Message\StreamInterface;
13 use Psr\Http\Message\UploadedFileInterface;
14 use Psr\Http\Message\UriInterface;
17 * Server-side HTTP request
19 * Extends the Request definition to add methods for accessing incoming data,
20 * specifically server parameters, cookies, matched path parameters, query
21 * string arguments, body parameters, and upload file information.
23 * "Attributes" are discovered via decomposing the request (and usually
24 * specifically the URI path), and typically will be injected by the application.
26 * Requests are considered immutable; all methods that might change state are
27 * implemented such that they retain the internal state of the current
28 * message and return a new instance that contains the changed state.
30 class ServerRequest implements ServerRequestInterface
37 private $attributes = [];
42 private $cookieParams = [];
45 * @var null|array|object
52 private $queryParams = [];
57 private $serverParams;
62 private $uploadedFiles;
65 * @param array $serverParams Server parameters, typically from $_SERVER
66 * @param array $uploadedFiles Upload file information, a tree of UploadedFiles
67 * @param null|string|UriInterface $uri URI for the request, if any.
68 * @param null|string $method HTTP method for the request, if any.
69 * @param string|resource|StreamInterface $body Message body, if any.
70 * @param array $headers Headers for the message, if any.
71 * @param array $cookies Cookies for the message, if any.
72 * @param array $queryParams Query params for the message, if any.
73 * @param null|array|object $parsedBody The deserialized body parameters, if any.
74 * @param string $protocol HTTP protocol version.
75 * @throws InvalidArgumentException for any invalid value.
77 public function __construct(
78 array $serverParams = [],
79 array $uploadedFiles = [],
82 $body = 'php://input',
85 array $queryParams = [],
89 $this->validateUploadedFiles($uploadedFiles);
91 if ($body === 'php://input') {
92 $body = new PhpInputStream();
95 $this->initialize($uri, $method, $body, $headers);
96 $this->serverParams = $serverParams;
97 $this->uploadedFiles = $uploadedFiles;
98 $this->cookieParams = $cookies;
99 $this->queryParams = $queryParams;
100 $this->parsedBody = $parsedBody;
101 $this->protocol = $protocol;
107 public function getServerParams()
109 return $this->serverParams;
115 public function getUploadedFiles()
117 return $this->uploadedFiles;
123 public function withUploadedFiles(array $uploadedFiles)
125 $this->validateUploadedFiles($uploadedFiles);
127 $new->uploadedFiles = $uploadedFiles;
134 public function getCookieParams()
136 return $this->cookieParams;
142 public function withCookieParams(array $cookies)
145 $new->cookieParams = $cookies;
152 public function getQueryParams()
154 return $this->queryParams;
160 public function withQueryParams(array $query)
163 $new->queryParams = $query;
170 public function getParsedBody()
172 return $this->parsedBody;
178 public function withParsedBody($data)
181 $new->parsedBody = $data;
188 public function getAttributes()
190 return $this->attributes;
196 public function getAttribute($attribute, $default = null)
198 if (! array_key_exists($attribute, $this->attributes)) {
202 return $this->attributes[$attribute];
208 public function withAttribute($attribute, $value)
211 $new->attributes[$attribute] = $value;
218 public function withoutAttribute($attribute)
221 unset($new->attributes[$attribute]);
226 * Proxy to receive the request method.
228 * This overrides the parent functionality to ensure the method is never
229 * empty; if no method is present, it returns 'GET'.
233 public function getMethod()
235 if (empty($this->method)) {
238 return $this->method;
242 * Set the request method.
244 * Unlike the regular Request implementation, the server-side
245 * normalizes the method to uppercase to ensure consistency
246 * and make checking the method simpler.
248 * This methods returns a new instance.
250 * @param string $method
253 public function withMethod($method)
255 $this->validateMethod($method);
257 $new->method = $method;
262 * Recursively validate the structure in an uploaded files array.
264 * @param array $uploadedFiles
265 * @throws InvalidArgumentException if any leaf is not an UploadedFileInterface instance.
267 private function validateUploadedFiles(array $uploadedFiles)
269 foreach ($uploadedFiles as $file) {
270 if (is_array($file)) {
271 $this->validateUploadedFiles($file);
275 if (! $file instanceof UploadedFileInterface) {
276 throw new InvalidArgumentException('Invalid leaf in uploaded files structure');