ServerRequest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 This class should never be extended. See https://github.com/Nyholm/psr7/blob/master/doc/final.md
  11. */
  12. class ServerRequest implements ServerRequestInterface
  13. {
  14. use MessageTrait;
  15. use RequestTrait;
  16. /** @var array */
  17. private $attributes = [];
  18. /** @var array */
  19. private $cookieParams = [];
  20. /** @var array|object|null */
  21. private $parsedBody;
  22. /** @var array */
  23. private $queryParams = [];
  24. /** @var array */
  25. private $serverParams;
  26. /** @var UploadedFileInterface[] */
  27. private $uploadedFiles = [];
  28. /**
  29. * @param string $method HTTP method
  30. * @param string|UriInterface $uri URI
  31. * @param array $headers Request headers
  32. * @param string|resource|StreamInterface|null $body Request body
  33. * @param string $version Protocol version
  34. * @param array $serverParams Typically the $_SERVER superglobal
  35. */
  36. public function __construct(string $method, $uri, array $headers = [], $body = null, string $version = '1.1', array $serverParams = [])
  37. {
  38. $this->serverParams = $serverParams;
  39. if (!($uri instanceof UriInterface)) {
  40. $uri = new Uri($uri);
  41. }
  42. $this->method = $method;
  43. $this->uri = $uri;
  44. $this->setHeaders($headers);
  45. $this->protocol = $version;
  46. \parse_str($uri->getQuery(), $this->queryParams);
  47. if (!$this->hasHeader('Host')) {
  48. $this->updateHostFromUri();
  49. }
  50. // If we got no body, defer initialization of the stream until ServerRequest::getBody()
  51. if ('' !== $body && null !== $body) {
  52. $this->stream = Stream::create($body);
  53. }
  54. }
  55. public function getServerParams(): array
  56. {
  57. return $this->serverParams;
  58. }
  59. public function getUploadedFiles(): array
  60. {
  61. return $this->uploadedFiles;
  62. }
  63. /**
  64. * @return static
  65. */
  66. public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface
  67. {
  68. $new = clone $this;
  69. $new->uploadedFiles = $uploadedFiles;
  70. return $new;
  71. }
  72. public function getCookieParams(): array
  73. {
  74. return $this->cookieParams;
  75. }
  76. /**
  77. * @return static
  78. */
  79. public function withCookieParams(array $cookies): ServerRequestInterface
  80. {
  81. $new = clone $this;
  82. $new->cookieParams = $cookies;
  83. return $new;
  84. }
  85. public function getQueryParams(): array
  86. {
  87. return $this->queryParams;
  88. }
  89. /**
  90. * @return static
  91. */
  92. public function withQueryParams(array $query): ServerRequestInterface
  93. {
  94. $new = clone $this;
  95. $new->queryParams = $query;
  96. return $new;
  97. }
  98. /**
  99. * @return array|object|null
  100. */
  101. public function getParsedBody()
  102. {
  103. return $this->parsedBody;
  104. }
  105. /**
  106. * @return static
  107. */
  108. public function withParsedBody($data): ServerRequestInterface
  109. {
  110. if (!\is_array($data) && !\is_object($data) && null !== $data) {
  111. throw new \InvalidArgumentException('First parameter to withParsedBody MUST be object, array or null');
  112. }
  113. $new = clone $this;
  114. $new->parsedBody = $data;
  115. return $new;
  116. }
  117. public function getAttributes(): array
  118. {
  119. return $this->attributes;
  120. }
  121. /**
  122. * @return mixed
  123. */
  124. public function getAttribute($attribute, $default = null)
  125. {
  126. if (!\is_string($attribute)) {
  127. throw new \InvalidArgumentException('Attribute name must be a string');
  128. }
  129. if (false === \array_key_exists($attribute, $this->attributes)) {
  130. return $default;
  131. }
  132. return $this->attributes[$attribute];
  133. }
  134. /**
  135. * @return static
  136. */
  137. public function withAttribute($attribute, $value): ServerRequestInterface
  138. {
  139. if (!\is_string($attribute)) {
  140. throw new \InvalidArgumentException('Attribute name must be a string');
  141. }
  142. $new = clone $this;
  143. $new->attributes[$attribute] = $value;
  144. return $new;
  145. }
  146. /**
  147. * @return static
  148. */
  149. public function withoutAttribute($attribute): ServerRequestInterface
  150. {
  151. if (!\is_string($attribute)) {
  152. throw new \InvalidArgumentException('Attribute name must be a string');
  153. }
  154. if (false === \array_key_exists($attribute, $this->attributes)) {
  155. return $this;
  156. }
  157. $new = clone $this;
  158. unset($new->attributes[$attribute]);
  159. return $new;
  160. }
  161. }