3 * Zend Framework (http://framework.zend.com/)
5 * @see http://github.com/zendframework/zend-diactoros for the canonical source repository
6 * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
10 namespace Zend\Diactoros;
12 use InvalidArgumentException;
13 use Psr\Http\Message\ServerRequestInterface;
14 use Psr\Http\Message\StreamInterface;
15 use Psr\Http\Message\UploadedFileInterface;
16 use Psr\Http\Message\UriInterface;
19 * Server-side HTTP request
21 * Extends the Request definition to add methods for accessing incoming data,
22 * specifically server parameters, cookies, matched path parameters, query
23 * string arguments, body parameters, and upload file information.
25 * "Attributes" are discovered via decomposing the request (and usually
26 * specifically the URI path), and typically will be injected by the application.
28 * Requests are considered immutable; all methods that might change state are
29 * implemented such that they retain the internal state of the current
30 * message and return a new instance that contains the changed state.
32 class ServerRequest implements ServerRequestInterface
39 private $attributes = [];
44 private $cookieParams = [];
47 * @var null|array|object
54 private $queryParams = [];
59 private $serverParams;
64 private $uploadedFiles;
67 * @param array $serverParams Server parameters, typically from $_SERVER
68 * @param array $uploadedFiles Upload file information, a tree of UploadedFiles
69 * @param null|string|UriInterface $uri URI for the request, if any.
70 * @param null|string $method HTTP method for the request, if any.
71 * @param string|resource|StreamInterface $body Message body, if any.
72 * @param array $headers Headers for the message, if any.
73 * @param array $cookies Cookies for the message, if any.
74 * @param array $queryParams Query params for the message, if any.
75 * @param null|array|object $parsedBody The deserialized body parameters, if any.
76 * @param string $protocol HTTP protocol version.
77 * @throws InvalidArgumentException for any invalid value.
79 public function __construct(
80 array $serverParams = [],
81 array $uploadedFiles = [],
84 $body = 'php://input',
87 array $queryParams = [],
91 $this->validateUploadedFiles($uploadedFiles);
93 if ($body === 'php://input') {
94 $body = new PhpInputStream();
97 $this->initialize($uri, $method, $body, $headers);
98 $this->serverParams = $serverParams;
99 $this->uploadedFiles = $uploadedFiles;
100 $this->cookieParams = $cookies;
101 $this->queryParams = $queryParams;
102 $this->parsedBody = $parsedBody;
103 $this->protocol = $protocol;
109 public function getServerParams()
111 return $this->serverParams;
117 public function getUploadedFiles()
119 return $this->uploadedFiles;
125 public function withUploadedFiles(array $uploadedFiles)
127 $this->validateUploadedFiles($uploadedFiles);
129 $new->uploadedFiles = $uploadedFiles;
136 public function getCookieParams()
138 return $this->cookieParams;
144 public function withCookieParams(array $cookies)
147 $new->cookieParams = $cookies;
154 public function getQueryParams()
156 return $this->queryParams;
162 public function withQueryParams(array $query)
165 $new->queryParams = $query;
172 public function getParsedBody()
174 return $this->parsedBody;
180 public function withParsedBody($data)
183 $new->parsedBody = $data;
190 public function getAttributes()
192 return $this->attributes;
198 public function getAttribute($attribute, $default = null)
200 if (! array_key_exists($attribute, $this->attributes)) {
204 return $this->attributes[$attribute];
210 public function withAttribute($attribute, $value)
213 $new->attributes[$attribute] = $value;
220 public function withoutAttribute($attribute)
223 unset($new->attributes[$attribute]);
228 * Proxy to receive the request method.
230 * This overrides the parent functionality to ensure the method is never
231 * empty; if no method is present, it returns 'GET'.
235 public function getMethod()
237 if (empty($this->method)) {
240 return $this->method;
244 * Set the request method.
246 * Unlike the regular Request implementation, the server-side
247 * normalizes the method to uppercase to ensure consistency
248 * and make checking the method simpler.
250 * This methods returns a new instance.
252 * @param string $method
255 public function withMethod($method)
257 $this->validateMethod($method);
259 $new->method = $method;
264 * Recursively validate the structure in an uploaded files array.
266 * @param array $uploadedFiles
267 * @throws InvalidArgumentException if any leaf is not an UploadedFileInterface instance.
269 private function validateUploadedFiles(array $uploadedFiles)
271 foreach ($uploadedFiles as $file) {
272 if (is_array($file)) {
273 $this->validateUploadedFiles($file);
277 if (! $file instanceof UploadedFileInterface) {
278 throw new InvalidArgumentException('Invalid leaf in uploaded files structure');