ServerRequest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. declare(strict_types=1);
  3. namespace Nyholm\Psr7;
  4. use Psr\Http\Message\{ServerRequestInterface, StreamInterface, UploadedFileInterface, UriInterface};
  5. /**
  6. * @author Michael Dowling and contributors to guzzlehttp/psr7
  7. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  8. * @author Martijn van der Ven <martijn@vanderven.se>
  9. */
  10. final class ServerRequest implements ServerRequestInterface
  11. {
  12. use MessageTrait;
  13. use RequestTrait;
  14. /** @var array */
  15. private $attributes = [];
  16. /** @var array */
  17. private $cookieParams = [];
  18. /** @var array|object|null */
  19. private $parsedBody;
  20. /** @var array */
  21. private $queryParams = [];
  22. /** @var array */
  23. private $serverParams;
  24. /** @var UploadedFileInterface[] */
  25. private $uploadedFiles = [];
  26. /**
  27. * @param string $method HTTP method
  28. * @param string|UriInterface $uri URI
  29. * @param array $headers Request headers
  30. * @param string|resource|StreamInterface|null $body Request body
  31. * @param string $version Protocol version
  32. * @param array $serverParams Typically the $_SERVER superglobal
  33. */
  34. public function __construct(string $method, $uri, array $headers = [], $body = null, string $version = '1.1', array $serverParams = [])
  35. {
  36. $this->serverParams = $serverParams;
  37. if (!($uri instanceof UriInterface)) {
  38. $uri = new Uri($uri);
  39. }
  40. $this->method = $method;
  41. $this->uri = $uri;
  42. $this->setHeaders($headers);
  43. $this->protocol = $version;
  44. if (!$this->hasHeader('Host')) {
  45. $this->updateHostFromUri();
  46. }
  47. // If we got no body, defer initialization of the stream until ServerRequest::getBody()
  48. if ('' !== $body && null !== $body) {
  49. $this->stream = Stream::create($body);
  50. }
  51. }
  52. public function getServerParams(): array
  53. {
  54. return $this->serverParams;
  55. }
  56. public function getUploadedFiles(): array
  57. {
  58. return $this->uploadedFiles;
  59. }
  60. public function withUploadedFiles(array $uploadedFiles)
  61. {
  62. $new = clone $this;
  63. $new->uploadedFiles = $uploadedFiles;
  64. return $new;
  65. }
  66. public function getCookieParams(): array
  67. {
  68. return $this->cookieParams;
  69. }
  70. public function withCookieParams(array $cookies)
  71. {
  72. $new = clone $this;
  73. $new->cookieParams = $cookies;
  74. return $new;
  75. }
  76. public function getQueryParams(): array
  77. {
  78. return $this->queryParams;
  79. }
  80. public function withQueryParams(array $query)
  81. {
  82. $new = clone $this;
  83. $new->queryParams = $query;
  84. return $new;
  85. }
  86. public function getParsedBody()
  87. {
  88. return $this->parsedBody;
  89. }
  90. public function withParsedBody($data)
  91. {
  92. if (!\is_array($data) && !\is_object($data) && null !== $data) {
  93. throw new \InvalidArgumentException('First parameter to withParsedBody MUST be object, array or null');
  94. }
  95. $new = clone $this;
  96. $new->parsedBody = $data;
  97. return $new;
  98. }
  99. public function getAttributes(): array
  100. {
  101. return $this->attributes;
  102. }
  103. public function getAttribute($attribute, $default = null)
  104. {
  105. if (false === \array_key_exists($attribute, $this->attributes)) {
  106. return $default;
  107. }
  108. return $this->attributes[$attribute];
  109. }
  110. public function withAttribute($attribute, $value): self
  111. {
  112. $new = clone $this;
  113. $new->attributes[$attribute] = $value;
  114. return $new;
  115. }
  116. public function withoutAttribute($attribute): self
  117. {
  118. if (false === \array_key_exists($attribute, $this->attributes)) {
  119. return $this;
  120. }
  121. $new = clone $this;
  122. unset($new->attributes[$attribute]);
  123. return $new;
  124. }
  125. }