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;
16 use function array_key_exists;
17 use function is_array;
20 * Server-side HTTP request
22 * Extends the Request definition to add methods for accessing incoming data,
23 * specifically server parameters, cookies, matched path parameters, query
24 * string arguments, body parameters, and upload file information.
26 * "Attributes" are discovered via decomposing the request (and usually
27 * specifically the URI path), and typically will be injected by the application.
29 * Requests are considered immutable; all methods that might change state are
30 * implemented such that they retain the internal state of the current
31 * message and return a new instance that contains the changed state.
33 class ServerRequest implements ServerRequestInterface
40 private $attributes = [];
45 private $cookieParams = [];
48 * @var null|array|object
55 private $queryParams = [];
60 private $serverParams;
65 private $uploadedFiles;
68 * @param array $serverParams Server parameters, typically from $_SERVER
69 * @param array $uploadedFiles Upload file information, a tree of UploadedFiles
70 * @param null|string|UriInterface $uri URI for the request, if any.
71 * @param null|string $method HTTP method for the request, if any.
72 * @param string|resource|StreamInterface $body Message body, if any.
73 * @param array $headers Headers for the message, if any.
74 * @param array $cookies Cookies for the message, if any.
75 * @param array $queryParams Query params for the message, if any.
76 * @param null|array|object $parsedBody The deserialized body parameters, if any.
77 * @param string $protocol HTTP protocol version.
78 * @throws InvalidArgumentException for any invalid value.
80 public function __construct(
81 array $serverParams = [],
82 array $uploadedFiles = [],
85 $body = 'php://input',
88 array $queryParams = [],
92 $this->validateUploadedFiles($uploadedFiles);
94 if ($body === 'php://input') {
95 $body = new PhpInputStream();
98 $this->initialize($uri, $method, $body, $headers);
99 $this->serverParams = $serverParams;
100 $this->uploadedFiles = $uploadedFiles;
101 $this->cookieParams = $cookies;
102 $this->queryParams = $queryParams;
103 $this->parsedBody = $parsedBody;
104 $this->protocol = $protocol;
110 public function getServerParams()
112 return $this->serverParams;
118 public function getUploadedFiles()
120 return $this->uploadedFiles;
126 public function withUploadedFiles(array $uploadedFiles)
128 $this->validateUploadedFiles($uploadedFiles);
130 $new->uploadedFiles = $uploadedFiles;
137 public function getCookieParams()
139 return $this->cookieParams;
145 public function withCookieParams(array $cookies)
148 $new->cookieParams = $cookies;
155 public function getQueryParams()
157 return $this->queryParams;
163 public function withQueryParams(array $query)
166 $new->queryParams = $query;
173 public function getParsedBody()
175 return $this->parsedBody;
181 public function withParsedBody($data)
183 if (! is_array($data) && ! is_object($data) && null !== $data) {
184 throw new InvalidArgumentException(sprintf(
185 '%s expects a null, array, or object argument; received %s',
192 $new->parsedBody = $data;
199 public function getAttributes()
201 return $this->attributes;
207 public function getAttribute($attribute, $default = null)
209 if (! array_key_exists($attribute, $this->attributes)) {
213 return $this->attributes[$attribute];
219 public function withAttribute($attribute, $value)
222 $new->attributes[$attribute] = $value;
229 public function withoutAttribute($attribute)
232 unset($new->attributes[$attribute]);
237 * Proxy to receive the request method.
239 * This overrides the parent functionality to ensure the method is never
240 * empty; if no method is present, it returns 'GET'.
244 public function getMethod()
246 if (empty($this->method)) {
249 return $this->method;
253 * Set the request method.
255 * Unlike the regular Request implementation, the server-side
256 * normalizes the method to uppercase to ensure consistency
257 * and make checking the method simpler.
259 * This methods returns a new instance.
261 * @param string $method
264 public function withMethod($method)
266 $this->validateMethod($method);
268 $new->method = $method;
273 * Recursively validate the structure in an uploaded files array.
275 * @param array $uploadedFiles
276 * @throws InvalidArgumentException if any leaf is not an UploadedFileInterface instance.
278 private function validateUploadedFiles(array $uploadedFiles)
280 foreach ($uploadedFiles as $file) {
281 if (is_array($file)) {
282 $this->validateUploadedFiles($file);
286 if (! $file instanceof UploadedFileInterface) {
287 throw new InvalidArgumentException('Invalid leaf in uploaded files structure');