Request.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace AsyncAws\Core;
  3. use AsyncAws\Core\Exception\LogicException;
  4. use AsyncAws\Core\Stream\RequestStream;
  5. /**
  6. * Representation of an HTTP Request.
  7. *
  8. * @author Jérémy Derussé <jeremy@derusse.com>
  9. *
  10. * @internal
  11. */
  12. class Request
  13. {
  14. private $method;
  15. private $uri;
  16. private $headers;
  17. private $body;
  18. private $query;
  19. private $endpoint;
  20. private $parsed;
  21. /**
  22. * @param string[] $query
  23. * @param string[] $headers
  24. */
  25. public function __construct(string $method, string $uri, array $query, array $headers, RequestStream $body)
  26. {
  27. $this->method = $method;
  28. $this->uri = $uri;
  29. $this->headers = [];
  30. foreach ($headers as $key => $value) {
  31. $this->headers[strtolower($key)] = (string) $value;
  32. }
  33. $this->body = $body;
  34. $this->query = $query;
  35. $this->endpoint = '';
  36. }
  37. public function getMethod(): string
  38. {
  39. return $this->method;
  40. }
  41. public function setMethod(string $method): void
  42. {
  43. $this->method = $method;
  44. }
  45. public function getUri(): string
  46. {
  47. return $this->uri;
  48. }
  49. public function hasHeader($name): bool
  50. {
  51. return \array_key_exists(strtolower($name), $this->headers);
  52. }
  53. public function setHeader($name, ?string $value): void
  54. {
  55. $this->headers[strtolower($name)] = $value;
  56. }
  57. public function getHeaders(): array
  58. {
  59. return $this->headers;
  60. }
  61. public function getHeader(string $name): ?string
  62. {
  63. return $this->headers[strtolower($name)] ?? null;
  64. }
  65. public function removeHeader(string $name): void
  66. {
  67. unset($this->headers[strtolower($name)]);
  68. }
  69. public function getBody(): RequestStream
  70. {
  71. return $this->body;
  72. }
  73. public function setBody(RequestStream $body)
  74. {
  75. $this->body = $body;
  76. }
  77. public function hasQueryAttribute($name): bool
  78. {
  79. return \array_key_exists($name, $this->query);
  80. }
  81. public function removeQueryAttribute($name): void
  82. {
  83. unset($this->query[$name]);
  84. $this->endpoint = '';
  85. }
  86. public function setQueryAttribute($name, $value): void
  87. {
  88. $this->query[$name] = $value;
  89. $this->endpoint = '';
  90. }
  91. public function getQueryAttribute(string $name): ?string
  92. {
  93. return $this->query[$name] ?? null;
  94. }
  95. public function getQuery(): array
  96. {
  97. return $this->query;
  98. }
  99. public function getEndpoint(): string
  100. {
  101. if (empty($this->endpoint)) {
  102. $this->endpoint = $this->parsed['scheme'] . '://' . $this->parsed['host'] . (isset($this->parsed['port']) ? ':' . $this->parsed['port'] : '') . $this->uri . ($this->query ? (false === strpos($this->uri, '?') ? '?' : '&') . http_build_query($this->query, '', '&', \PHP_QUERY_RFC3986) : '');
  103. }
  104. return $this->endpoint;
  105. }
  106. public function setEndpoint(string $endpoint): void
  107. {
  108. if (!empty($this->endpoint)) {
  109. throw new LogicException('Request::$endpoint cannot be changed after it has a value.');
  110. }
  111. $this->endpoint = $endpoint;
  112. $this->parsed = parse_url($this->endpoint);
  113. parse_str($this->parsed['query'] ?? '', $this->query);
  114. $this->uri = $this->parsed['path'] ?? '/';
  115. }
  116. }