Request.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace Nyholm\Psr7;
  3. use Psr\Http\Message\{RequestInterface, StreamInterface, UriInterface};
  4. /**
  5. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  6. * @author Martijn van der Ven <martijn@vanderven.se>
  7. *
  8. * @final This class should never be extended. See https://github.com/Nyholm/psr7/blob/master/doc/final.md
  9. */
  10. class Request implements RequestInterface
  11. {
  12. use MessageTrait;
  13. use RequestTrait;
  14. /**
  15. * @param string $method HTTP method
  16. * @param string|UriInterface $uri URI
  17. * @param array $headers Request headers
  18. * @param string|resource|StreamInterface|null $body Request body
  19. * @param string $version Protocol version
  20. */
  21. public function __construct($method, $uri, $headers = [], $body = null, $version = '1.1')
  22. {
  23. if (!($uri instanceof UriInterface)) {
  24. $uri = new Uri($uri);
  25. }
  26. $this->method = $method;
  27. $this->uri = $uri;
  28. $this->setHeaders($headers);
  29. $this->protocol = $version;
  30. if (!$this->hasHeader('Host')) {
  31. $this->updateHostFromUri();
  32. }
  33. // If we got no body, defer initialization of the stream until Request::getBody()
  34. if ('' !== $body && null !== $body) {
  35. $this->stream = Stream::create($body);
  36. }
  37. }
  38. }