@@ -22,381 +22,381 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class ServerRequest extends Request implements ServerRequestInterface |
| 24 | 24 | { |
| 25 | - /** @var array The server parameters. */ |
|
| 26 | - private $serverParams; |
|
| 27 | - |
|
| 28 | - /** @var array The cookie parameters. */ |
|
| 29 | - private $cookieParams; |
|
| 30 | - |
|
| 31 | - /** @var array The query parameters. */ |
|
| 32 | - private $queryParams; |
|
| 33 | - |
|
| 34 | - /** @var array The post parameters. */ |
|
| 35 | - private $postParams; |
|
| 36 | - |
|
| 37 | - /** @var array The files parameters. */ |
|
| 38 | - private $filesParams; |
|
| 39 | - |
|
| 40 | - /** @var array The uploaded files. */ |
|
| 41 | - private $uploadedFiles; |
|
| 42 | - |
|
| 43 | - /** @var null|array|object The parsed body. */ |
|
| 44 | - private $parsedBody; |
|
| 45 | - |
|
| 46 | - /** @var array The attributes. */ |
|
| 47 | - private $attributes; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Construct a Request object with the given method, uri, version, headers & body. |
|
| 51 | - * |
|
| 52 | - * @global array $_SERVER The server parameters. |
|
| 53 | - * @global array $_COOKIE The cookie parameters. |
|
| 54 | - * @global array $_GET The query parameters. |
|
| 55 | - * @global array $_POST The post parameters. |
|
| 56 | - * @global array $_FILES The files parameters. |
|
| 57 | - * |
|
| 58 | - * @param string $method = '' |
|
| 59 | - * @param UriInterface|null $uri = null |
|
| 60 | - * @param string $version = self::DEFAULT_VERSION |
|
| 61 | - * @param array $headers = [] |
|
| 62 | - * @param StreamInterface|null $body = null |
|
| 63 | - */ |
|
| 64 | - public function __construct($method = '', UriInterface $uri = null, $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) |
|
| 65 | - { |
|
| 66 | - if ($body === null) { |
|
| 67 | - $body = new Stream(fopen('php://input', 'r')); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $this->serverParams = $_SERVER; |
|
| 71 | - $this->cookieParams = $_COOKIE; |
|
| 72 | - $this->queryParams = $_GET; |
|
| 73 | - $this->postParams = $_POST; |
|
| 74 | - $this->filesParams = $_FILES; |
|
| 75 | - $this->uploadedFiles = $this->initUploadedFiles($this->filesParams); |
|
| 76 | - $this->parsedBody = null; |
|
| 77 | - $this->attributes = []; |
|
| 78 | - |
|
| 79 | - parent::__construct($this->initMethod($method), $this->initUri($uri), $version, $this->initHeaders($headers), $body); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Initialize the method. |
|
| 84 | - * |
|
| 85 | - * @param string $method |
|
| 86 | - * @return string the method. |
|
| 87 | - */ |
|
| 88 | - private function initMethod($method) |
|
| 89 | - { |
|
| 90 | - return $method === '' && isset($this->getServerParams()['REQUEST_METHOD']) ? $this->getServerParams()['REQUEST_METHOD'] : $method; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Initialize the URI. |
|
| 95 | - * |
|
| 96 | - * @param UriInterface|null $uri |
|
| 97 | - * @return UriInterface the URI. |
|
| 98 | - */ |
|
| 99 | - private function initUri($uri) |
|
| 100 | - { |
|
| 101 | - if ($uri !== null) { |
|
| 102 | - return $uri; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - $scheme = isset($this->getServerParams()['HTTPS']) ? 'https://' : 'http://'; |
|
| 106 | - $host = isset($this->getServerParams()['HTTP_HOST']) ? $scheme . $this->getServerParams()['HTTP_HOST'] : ''; |
|
| 107 | - $path = isset($this->getServerParams()['REQUEST_URI']) ? $this->getServerParams()['REQUEST_URI'] : ''; |
|
| 108 | - |
|
| 109 | - return new URI($host . $path); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Initialize the headers. |
|
| 114 | - * |
|
| 115 | - * @param array $headers |
|
| 116 | - * @return array the headers. |
|
| 117 | - */ |
|
| 118 | - private function initHeaders($headers) |
|
| 119 | - { |
|
| 120 | - return $headers ?: getallheaders(); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Initialize the uploaded files. |
|
| 125 | - * |
|
| 126 | - * @param array $files |
|
| 127 | - * @return array the uploaded files. |
|
| 128 | - */ |
|
| 129 | - private function initUploadedFiles(array $files) |
|
| 130 | - { |
|
| 131 | - $result = []; |
|
| 132 | - |
|
| 133 | - foreach ($files as $key => $value) { |
|
| 134 | - $result[$key] = $this->parseUploadedFiles($value); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - return $result; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Parse uploaded files. |
|
| 142 | - * |
|
| 143 | - * @param array $files |
|
| 144 | - * @return UploadedFile|array uploaded files. |
|
| 145 | - */ |
|
| 146 | - private function parseUploadedFiles($files) |
|
| 147 | - { |
|
| 148 | - // Empty |
|
| 149 | - $first = reset($files); |
|
| 150 | - |
|
| 151 | - // Single |
|
| 152 | - if (!is_array($first)) { |
|
| 153 | - return $this->parseSingleUploadedFiles($files); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - // Multiple |
|
| 157 | - if (count(array_filter(array_keys($first), 'is_string')) === 0) { |
|
| 158 | - return $this->parseMultipleUploadedFiles($files); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - // Namespace |
|
| 162 | - return $this->initUploadedFiles($files); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Parse single uploaded file. |
|
| 167 | - * |
|
| 168 | - * @param array $file |
|
| 169 | - * @return UploadedFile single uploaded file. |
|
| 170 | - */ |
|
| 171 | - private function parseSingleUploadedFiles(array $file) |
|
| 172 | - { |
|
| 173 | - return new UploadedFile($file['name'], $file['type'], $file['tmp_name'], $file['error'], $file['size']); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Parse multiple uploaded files. |
|
| 178 | - * |
|
| 179 | - * @param array $files |
|
| 180 | - * @return UploadedFiles[] multiple uploaded files. |
|
| 181 | - */ |
|
| 182 | - private function parseMultipleUploadedFiles(array $files) |
|
| 183 | - { |
|
| 184 | - $count = count($files['name']); |
|
| 185 | - $result = []; |
|
| 186 | - |
|
| 187 | - for ($i = 0; $i < $count; $i++) { |
|
| 188 | - $result[] = new UploadedFile($files['name'][$i], $files['type'][$i], $files['tmp_name'][$i], $files['error'][$i], $files['size'][$i]); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - return $result; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * {@inheritdoc} |
|
| 196 | - */ |
|
| 197 | - public function getServerParams() |
|
| 198 | - { |
|
| 199 | - return $this->serverParams; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * {@inheritdoc} |
|
| 204 | - */ |
|
| 205 | - public function getCookieParams() |
|
| 206 | - { |
|
| 207 | - return $this->cookieParams; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Set the cookie params. |
|
| 212 | - * |
|
| 213 | - * @param array $cookieParams |
|
| 214 | - * @return $this |
|
| 215 | - */ |
|
| 216 | - private function setCookieParams(array $cookieParams) |
|
| 217 | - { |
|
| 218 | - $this->cookieParams = $cookieParams; |
|
| 219 | - |
|
| 220 | - return $this; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * {@inheritdoc} |
|
| 225 | - */ |
|
| 226 | - public function withCookieParams(array $cookieParams) |
|
| 227 | - { |
|
| 228 | - $result = clone $this; |
|
| 229 | - |
|
| 230 | - return $result->setCookieParams($cookieParams); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * {@inheritdoc} |
|
| 235 | - */ |
|
| 236 | - public function getQueryParams() |
|
| 237 | - { |
|
| 238 | - return $this->queryParams; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Set the query params. |
|
| 243 | - * |
|
| 244 | - * @param array $queryParams |
|
| 245 | - * @return $this |
|
| 246 | - */ |
|
| 247 | - private function setQueryParams(array $queryParams) |
|
| 248 | - { |
|
| 249 | - $this->queryParams = $queryParams; |
|
| 250 | - |
|
| 251 | - return $this; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * {@inheritdoc} |
|
| 256 | - */ |
|
| 257 | - public function withQueryParams(array $queryParams) |
|
| 258 | - { |
|
| 259 | - $result = clone $this; |
|
| 260 | - |
|
| 261 | - return $result->setQueryParams($queryParams); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * {@inheritdoc} |
|
| 266 | - */ |
|
| 267 | - public function getUploadedFiles() |
|
| 268 | - { |
|
| 269 | - return $this->uploadedFiles; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Set the uploaded files. |
|
| 274 | - * |
|
| 275 | - * @param array $uploadedFiles |
|
| 276 | - * @return $this |
|
| 277 | - */ |
|
| 278 | - private function setUploadedFiles(array $uploadedFiles) |
|
| 279 | - { |
|
| 280 | - $this->uploadedFiles = $uploadedFiles; |
|
| 281 | - |
|
| 282 | - return $this; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * {@inheritdoc} |
|
| 287 | - */ |
|
| 288 | - public function withUploadedFiles(array $uploadedFiles) |
|
| 289 | - { |
|
| 290 | - $result = clone $this; |
|
| 291 | - |
|
| 292 | - return $result->setUploadedFiles($uploadedFiles); |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * {@inheritdoc} |
|
| 297 | - */ |
|
| 298 | - public function getParsedBody() |
|
| 299 | - { |
|
| 300 | - if ($this->parsedBody !== null) { |
|
| 301 | - return $this->parsedBody; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - $contentType = $this->getHeaderLine('Content-Type'); |
|
| 305 | - |
|
| 306 | - if ($this->getMethod() === 'POST' && ($contentType === 'application/x-www-form-urlencoded' || $contentType === 'multipart/form-data')) { |
|
| 307 | - return $this->postParams; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - if ($contentType === 'application/json') { |
|
| 311 | - return json_decode((string) $this->getBody()); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - return null; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * Set the parsed body. |
|
| 319 | - * |
|
| 320 | - * @param null|array|object $parsedBody |
|
| 321 | - * @return $this |
|
| 322 | - */ |
|
| 323 | - private function setParsedBody($parsedBody) |
|
| 324 | - { |
|
| 325 | - $this->parsedBody = $parsedBody; |
|
| 326 | - |
|
| 327 | - return $this; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * {@inheritdoc} |
|
| 332 | - */ |
|
| 333 | - public function withParsedBody($parsedBody) |
|
| 334 | - { |
|
| 335 | - $result = clone $this; |
|
| 336 | - |
|
| 337 | - return $result->setParsedBody($parsedBody); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * {@inheritdoc} |
|
| 342 | - */ |
|
| 343 | - public function getAttributes() |
|
| 344 | - { |
|
| 345 | - return $this->attributes; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * {@inheritdoc} |
|
| 350 | - */ |
|
| 351 | - public function getAttribute($name, $default = null) |
|
| 352 | - { |
|
| 353 | - return isset($this->attributes[$name]) ? $this->attributes[$name] : $default; |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * Set the attribute. |
|
| 358 | - * |
|
| 359 | - * @param string $name |
|
| 360 | - * @param mixed $value |
|
| 361 | - * @return $this |
|
| 362 | - */ |
|
| 363 | - private function setAttribute($name, $value) |
|
| 364 | - { |
|
| 365 | - $this->attributes[$name] = $value; |
|
| 366 | - |
|
| 367 | - return $this; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * {@inheritdoc} |
|
| 372 | - */ |
|
| 373 | - public function withAttribute($name, $value) |
|
| 374 | - { |
|
| 375 | - $result = clone $this; |
|
| 376 | - |
|
| 377 | - return $result->setAttribute($name, $value); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * Remove the attribute. |
|
| 382 | - * |
|
| 383 | - * @param string $name |
|
| 384 | - * @return $this |
|
| 385 | - */ |
|
| 386 | - private function removeAttribute($name) |
|
| 387 | - { |
|
| 388 | - unset($this->attributes[$name]); |
|
| 389 | - |
|
| 390 | - return $this; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * {@inheritdoc} |
|
| 395 | - */ |
|
| 396 | - public function withoutAttribute($name) |
|
| 397 | - { |
|
| 398 | - $result = clone $this; |
|
| 399 | - |
|
| 400 | - return $result->removeAttribute($name); |
|
| 401 | - } |
|
| 25 | + /** @var array The server parameters. */ |
|
| 26 | + private $serverParams; |
|
| 27 | + |
|
| 28 | + /** @var array The cookie parameters. */ |
|
| 29 | + private $cookieParams; |
|
| 30 | + |
|
| 31 | + /** @var array The query parameters. */ |
|
| 32 | + private $queryParams; |
|
| 33 | + |
|
| 34 | + /** @var array The post parameters. */ |
|
| 35 | + private $postParams; |
|
| 36 | + |
|
| 37 | + /** @var array The files parameters. */ |
|
| 38 | + private $filesParams; |
|
| 39 | + |
|
| 40 | + /** @var array The uploaded files. */ |
|
| 41 | + private $uploadedFiles; |
|
| 42 | + |
|
| 43 | + /** @var null|array|object The parsed body. */ |
|
| 44 | + private $parsedBody; |
|
| 45 | + |
|
| 46 | + /** @var array The attributes. */ |
|
| 47 | + private $attributes; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Construct a Request object with the given method, uri, version, headers & body. |
|
| 51 | + * |
|
| 52 | + * @global array $_SERVER The server parameters. |
|
| 53 | + * @global array $_COOKIE The cookie parameters. |
|
| 54 | + * @global array $_GET The query parameters. |
|
| 55 | + * @global array $_POST The post parameters. |
|
| 56 | + * @global array $_FILES The files parameters. |
|
| 57 | + * |
|
| 58 | + * @param string $method = '' |
|
| 59 | + * @param UriInterface|null $uri = null |
|
| 60 | + * @param string $version = self::DEFAULT_VERSION |
|
| 61 | + * @param array $headers = [] |
|
| 62 | + * @param StreamInterface|null $body = null |
|
| 63 | + */ |
|
| 64 | + public function __construct($method = '', UriInterface $uri = null, $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) |
|
| 65 | + { |
|
| 66 | + if ($body === null) { |
|
| 67 | + $body = new Stream(fopen('php://input', 'r')); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $this->serverParams = $_SERVER; |
|
| 71 | + $this->cookieParams = $_COOKIE; |
|
| 72 | + $this->queryParams = $_GET; |
|
| 73 | + $this->postParams = $_POST; |
|
| 74 | + $this->filesParams = $_FILES; |
|
| 75 | + $this->uploadedFiles = $this->initUploadedFiles($this->filesParams); |
|
| 76 | + $this->parsedBody = null; |
|
| 77 | + $this->attributes = []; |
|
| 78 | + |
|
| 79 | + parent::__construct($this->initMethod($method), $this->initUri($uri), $version, $this->initHeaders($headers), $body); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Initialize the method. |
|
| 84 | + * |
|
| 85 | + * @param string $method |
|
| 86 | + * @return string the method. |
|
| 87 | + */ |
|
| 88 | + private function initMethod($method) |
|
| 89 | + { |
|
| 90 | + return $method === '' && isset($this->getServerParams()['REQUEST_METHOD']) ? $this->getServerParams()['REQUEST_METHOD'] : $method; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Initialize the URI. |
|
| 95 | + * |
|
| 96 | + * @param UriInterface|null $uri |
|
| 97 | + * @return UriInterface the URI. |
|
| 98 | + */ |
|
| 99 | + private function initUri($uri) |
|
| 100 | + { |
|
| 101 | + if ($uri !== null) { |
|
| 102 | + return $uri; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + $scheme = isset($this->getServerParams()['HTTPS']) ? 'https://' : 'http://'; |
|
| 106 | + $host = isset($this->getServerParams()['HTTP_HOST']) ? $scheme . $this->getServerParams()['HTTP_HOST'] : ''; |
|
| 107 | + $path = isset($this->getServerParams()['REQUEST_URI']) ? $this->getServerParams()['REQUEST_URI'] : ''; |
|
| 108 | + |
|
| 109 | + return new URI($host . $path); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Initialize the headers. |
|
| 114 | + * |
|
| 115 | + * @param array $headers |
|
| 116 | + * @return array the headers. |
|
| 117 | + */ |
|
| 118 | + private function initHeaders($headers) |
|
| 119 | + { |
|
| 120 | + return $headers ?: getallheaders(); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Initialize the uploaded files. |
|
| 125 | + * |
|
| 126 | + * @param array $files |
|
| 127 | + * @return array the uploaded files. |
|
| 128 | + */ |
|
| 129 | + private function initUploadedFiles(array $files) |
|
| 130 | + { |
|
| 131 | + $result = []; |
|
| 132 | + |
|
| 133 | + foreach ($files as $key => $value) { |
|
| 134 | + $result[$key] = $this->parseUploadedFiles($value); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + return $result; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Parse uploaded files. |
|
| 142 | + * |
|
| 143 | + * @param array $files |
|
| 144 | + * @return UploadedFile|array uploaded files. |
|
| 145 | + */ |
|
| 146 | + private function parseUploadedFiles($files) |
|
| 147 | + { |
|
| 148 | + // Empty |
|
| 149 | + $first = reset($files); |
|
| 150 | + |
|
| 151 | + // Single |
|
| 152 | + if (!is_array($first)) { |
|
| 153 | + return $this->parseSingleUploadedFiles($files); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + // Multiple |
|
| 157 | + if (count(array_filter(array_keys($first), 'is_string')) === 0) { |
|
| 158 | + return $this->parseMultipleUploadedFiles($files); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + // Namespace |
|
| 162 | + return $this->initUploadedFiles($files); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Parse single uploaded file. |
|
| 167 | + * |
|
| 168 | + * @param array $file |
|
| 169 | + * @return UploadedFile single uploaded file. |
|
| 170 | + */ |
|
| 171 | + private function parseSingleUploadedFiles(array $file) |
|
| 172 | + { |
|
| 173 | + return new UploadedFile($file['name'], $file['type'], $file['tmp_name'], $file['error'], $file['size']); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Parse multiple uploaded files. |
|
| 178 | + * |
|
| 179 | + * @param array $files |
|
| 180 | + * @return UploadedFiles[] multiple uploaded files. |
|
| 181 | + */ |
|
| 182 | + private function parseMultipleUploadedFiles(array $files) |
|
| 183 | + { |
|
| 184 | + $count = count($files['name']); |
|
| 185 | + $result = []; |
|
| 186 | + |
|
| 187 | + for ($i = 0; $i < $count; $i++) { |
|
| 188 | + $result[] = new UploadedFile($files['name'][$i], $files['type'][$i], $files['tmp_name'][$i], $files['error'][$i], $files['size'][$i]); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + return $result; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * {@inheritdoc} |
|
| 196 | + */ |
|
| 197 | + public function getServerParams() |
|
| 198 | + { |
|
| 199 | + return $this->serverParams; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * {@inheritdoc} |
|
| 204 | + */ |
|
| 205 | + public function getCookieParams() |
|
| 206 | + { |
|
| 207 | + return $this->cookieParams; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Set the cookie params. |
|
| 212 | + * |
|
| 213 | + * @param array $cookieParams |
|
| 214 | + * @return $this |
|
| 215 | + */ |
|
| 216 | + private function setCookieParams(array $cookieParams) |
|
| 217 | + { |
|
| 218 | + $this->cookieParams = $cookieParams; |
|
| 219 | + |
|
| 220 | + return $this; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * {@inheritdoc} |
|
| 225 | + */ |
|
| 226 | + public function withCookieParams(array $cookieParams) |
|
| 227 | + { |
|
| 228 | + $result = clone $this; |
|
| 229 | + |
|
| 230 | + return $result->setCookieParams($cookieParams); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * {@inheritdoc} |
|
| 235 | + */ |
|
| 236 | + public function getQueryParams() |
|
| 237 | + { |
|
| 238 | + return $this->queryParams; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Set the query params. |
|
| 243 | + * |
|
| 244 | + * @param array $queryParams |
|
| 245 | + * @return $this |
|
| 246 | + */ |
|
| 247 | + private function setQueryParams(array $queryParams) |
|
| 248 | + { |
|
| 249 | + $this->queryParams = $queryParams; |
|
| 250 | + |
|
| 251 | + return $this; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * {@inheritdoc} |
|
| 256 | + */ |
|
| 257 | + public function withQueryParams(array $queryParams) |
|
| 258 | + { |
|
| 259 | + $result = clone $this; |
|
| 260 | + |
|
| 261 | + return $result->setQueryParams($queryParams); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * {@inheritdoc} |
|
| 266 | + */ |
|
| 267 | + public function getUploadedFiles() |
|
| 268 | + { |
|
| 269 | + return $this->uploadedFiles; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Set the uploaded files. |
|
| 274 | + * |
|
| 275 | + * @param array $uploadedFiles |
|
| 276 | + * @return $this |
|
| 277 | + */ |
|
| 278 | + private function setUploadedFiles(array $uploadedFiles) |
|
| 279 | + { |
|
| 280 | + $this->uploadedFiles = $uploadedFiles; |
|
| 281 | + |
|
| 282 | + return $this; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * {@inheritdoc} |
|
| 287 | + */ |
|
| 288 | + public function withUploadedFiles(array $uploadedFiles) |
|
| 289 | + { |
|
| 290 | + $result = clone $this; |
|
| 291 | + |
|
| 292 | + return $result->setUploadedFiles($uploadedFiles); |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * {@inheritdoc} |
|
| 297 | + */ |
|
| 298 | + public function getParsedBody() |
|
| 299 | + { |
|
| 300 | + if ($this->parsedBody !== null) { |
|
| 301 | + return $this->parsedBody; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + $contentType = $this->getHeaderLine('Content-Type'); |
|
| 305 | + |
|
| 306 | + if ($this->getMethod() === 'POST' && ($contentType === 'application/x-www-form-urlencoded' || $contentType === 'multipart/form-data')) { |
|
| 307 | + return $this->postParams; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + if ($contentType === 'application/json') { |
|
| 311 | + return json_decode((string) $this->getBody()); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + return null; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * Set the parsed body. |
|
| 319 | + * |
|
| 320 | + * @param null|array|object $parsedBody |
|
| 321 | + * @return $this |
|
| 322 | + */ |
|
| 323 | + private function setParsedBody($parsedBody) |
|
| 324 | + { |
|
| 325 | + $this->parsedBody = $parsedBody; |
|
| 326 | + |
|
| 327 | + return $this; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * {@inheritdoc} |
|
| 332 | + */ |
|
| 333 | + public function withParsedBody($parsedBody) |
|
| 334 | + { |
|
| 335 | + $result = clone $this; |
|
| 336 | + |
|
| 337 | + return $result->setParsedBody($parsedBody); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * {@inheritdoc} |
|
| 342 | + */ |
|
| 343 | + public function getAttributes() |
|
| 344 | + { |
|
| 345 | + return $this->attributes; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * {@inheritdoc} |
|
| 350 | + */ |
|
| 351 | + public function getAttribute($name, $default = null) |
|
| 352 | + { |
|
| 353 | + return isset($this->attributes[$name]) ? $this->attributes[$name] : $default; |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * Set the attribute. |
|
| 358 | + * |
|
| 359 | + * @param string $name |
|
| 360 | + * @param mixed $value |
|
| 361 | + * @return $this |
|
| 362 | + */ |
|
| 363 | + private function setAttribute($name, $value) |
|
| 364 | + { |
|
| 365 | + $this->attributes[$name] = $value; |
|
| 366 | + |
|
| 367 | + return $this; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * {@inheritdoc} |
|
| 372 | + */ |
|
| 373 | + public function withAttribute($name, $value) |
|
| 374 | + { |
|
| 375 | + $result = clone $this; |
|
| 376 | + |
|
| 377 | + return $result->setAttribute($name, $value); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * Remove the attribute. |
|
| 382 | + * |
|
| 383 | + * @param string $name |
|
| 384 | + * @return $this |
|
| 385 | + */ |
|
| 386 | + private function removeAttribute($name) |
|
| 387 | + { |
|
| 388 | + unset($this->attributes[$name]); |
|
| 389 | + |
|
| 390 | + return $this; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * {@inheritdoc} |
|
| 395 | + */ |
|
| 396 | + public function withoutAttribute($name) |
|
| 397 | + { |
|
| 398 | + $result = clone $this; |
|
| 399 | + |
|
| 400 | + return $result->removeAttribute($name); |
|
| 401 | + } |
|
| 402 | 402 | } |
@@ -19,39 +19,39 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class ServerResponse extends Response |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * Construct a ServerResponse object with the given status code, reason phrase, version, headers & body. |
|
| 24 | - * |
|
| 25 | - * @param int $statusCode |
|
| 26 | - * @param string $reasonPhrase = '' |
|
| 27 | - * @param string $version = self::DEFAULT_VERSION |
|
| 28 | - * @param array $headers = [] |
|
| 29 | - * @param StreamInterface|null $body = null |
|
| 30 | - */ |
|
| 31 | - public function __construct($statusCode, $reasonPhrase = '', $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) |
|
| 32 | - { |
|
| 33 | - if ($body === null) { |
|
| 34 | - $body = new Stream(fopen('php://output', 'w')); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - parent::__construct($statusCode, $reasonPhrase, $version, $headers, $body); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Send the server response. |
|
| 42 | - * |
|
| 43 | - * @return null |
|
| 44 | - */ |
|
| 45 | - public function send() |
|
| 46 | - { |
|
| 47 | - if ($this->getProtocolVersion() && $this->getStatusCode()) { |
|
| 48 | - header(static::VERSION_DELIMITER . $this->getProtocolVersion() . ' ' . $this->getStatusCode() . ' ' . $this->getReasonPhrase()); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - foreach ($this->getHeaders() as $key => $value) { |
|
| 52 | - header($key . static::HEADER_DELIMITER . implode(static::HEADER_VALUE_DELIMITER, $value)); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - echo $this->getBody(); |
|
| 56 | - } |
|
| 22 | + /** |
|
| 23 | + * Construct a ServerResponse object with the given status code, reason phrase, version, headers & body. |
|
| 24 | + * |
|
| 25 | + * @param int $statusCode |
|
| 26 | + * @param string $reasonPhrase = '' |
|
| 27 | + * @param string $version = self::DEFAULT_VERSION |
|
| 28 | + * @param array $headers = [] |
|
| 29 | + * @param StreamInterface|null $body = null |
|
| 30 | + */ |
|
| 31 | + public function __construct($statusCode, $reasonPhrase = '', $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) |
|
| 32 | + { |
|
| 33 | + if ($body === null) { |
|
| 34 | + $body = new Stream(fopen('php://output', 'w')); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + parent::__construct($statusCode, $reasonPhrase, $version, $headers, $body); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Send the server response. |
|
| 42 | + * |
|
| 43 | + * @return null |
|
| 44 | + */ |
|
| 45 | + public function send() |
|
| 46 | + { |
|
| 47 | + if ($this->getProtocolVersion() && $this->getStatusCode()) { |
|
| 48 | + header(static::VERSION_DELIMITER . $this->getProtocolVersion() . ' ' . $this->getStatusCode() . ' ' . $this->getReasonPhrase()); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + foreach ($this->getHeaders() as $key => $value) { |
|
| 52 | + header($key . static::HEADER_DELIMITER . implode(static::HEADER_VALUE_DELIMITER, $value)); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + echo $this->getBody(); |
|
| 56 | + } |
|
| 57 | 57 | } |