MessageTrait.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace GuzzleHttp\Psr7;
  3. use Psr\Http\Message\StreamInterface;
  4. /**
  5. * Trait implementing functionality common to requests and responses.
  6. */
  7. trait MessageTrait
  8. {
  9. /** @var array Map of all registered headers, as original name => array of values */
  10. private $headers = [];
  11. /** @var array Map of lowercase header name => original name at registration */
  12. private $headerNames = [];
  13. /** @var string */
  14. private $protocol = '1.1';
  15. /** @var StreamInterface */
  16. private $stream;
  17. public function getProtocolVersion()
  18. {
  19. return $this->protocol;
  20. }
  21. public function withProtocolVersion($version)
  22. {
  23. if ($this->protocol === $version) {
  24. return $this;
  25. }
  26. $new = clone $this;
  27. $new->protocol = $version;
  28. return $new;
  29. }
  30. public function getHeaders()
  31. {
  32. return $this->headers;
  33. }
  34. public function hasHeader($header)
  35. {
  36. return isset($this->headerNames[strtolower($header)]);
  37. }
  38. public function getHeader($header)
  39. {
  40. $header = strtolower($header);
  41. if (!isset($this->headerNames[$header])) {
  42. return [];
  43. }
  44. $header = $this->headerNames[$header];
  45. return $this->headers[$header];
  46. }
  47. public function getHeaderLine($header)
  48. {
  49. return implode(', ', $this->getHeader($header));
  50. }
  51. public function withHeader($header, $value)
  52. {
  53. if (!is_array($value)) {
  54. $value = [$value];
  55. }
  56. $value = $this->trimHeaderValues($value);
  57. $normalized = strtolower($header);
  58. $new = clone $this;
  59. if (isset($new->headerNames[$normalized])) {
  60. unset($new->headers[$new->headerNames[$normalized]]);
  61. }
  62. $new->headerNames[$normalized] = $header;
  63. $new->headers[$header] = $value;
  64. return $new;
  65. }
  66. public function withAddedHeader($header, $value)
  67. {
  68. if (!is_array($value)) {
  69. $value = [$value];
  70. }
  71. $value = $this->trimHeaderValues($value);
  72. $normalized = strtolower($header);
  73. $new = clone $this;
  74. if (isset($new->headerNames[$normalized])) {
  75. $header = $this->headerNames[$normalized];
  76. $new->headers[$header] = array_merge($this->headers[$header], $value);
  77. } else {
  78. $new->headerNames[$normalized] = $header;
  79. $new->headers[$header] = $value;
  80. }
  81. return $new;
  82. }
  83. public function withoutHeader($header)
  84. {
  85. $normalized = strtolower($header);
  86. if (!isset($this->headerNames[$normalized])) {
  87. return $this;
  88. }
  89. $header = $this->headerNames[$normalized];
  90. $new = clone $this;
  91. unset($new->headers[$header], $new->headerNames[$normalized]);
  92. return $new;
  93. }
  94. public function getBody()
  95. {
  96. if (!$this->stream) {
  97. $this->stream = stream_for('');
  98. }
  99. return $this->stream;
  100. }
  101. public function withBody(StreamInterface $body)
  102. {
  103. if ($body === $this->stream) {
  104. return $this;
  105. }
  106. $new = clone $this;
  107. $new->stream = $body;
  108. return $new;
  109. }
  110. private function setHeaders(array $headers)
  111. {
  112. $this->headerNames = $this->headers = [];
  113. foreach ($headers as $header => $value) {
  114. if (!is_array($value)) {
  115. $value = [$value];
  116. }
  117. $value = $this->trimHeaderValues($value);
  118. $normalized = strtolower($header);
  119. if (isset($this->headerNames[$normalized])) {
  120. $header = $this->headerNames[$normalized];
  121. $this->headers[$header] = array_merge($this->headers[$header], $value);
  122. } else {
  123. $this->headerNames[$normalized] = $header;
  124. $this->headers[$header] = $value;
  125. }
  126. }
  127. }
  128. /**
  129. * Trims whitespace from the header values.
  130. *
  131. * Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field.
  132. *
  133. * header-field = field-name ":" OWS field-value OWS
  134. * OWS = *( SP / HTAB )
  135. *
  136. * @param string[] $values Header values
  137. *
  138. * @return string[] Trimmed header values
  139. *
  140. * @see https://tools.ietf.org/html/rfc7230#section-3.2.4
  141. */
  142. private function trimHeaderValues(array $values)
  143. {
  144. return array_map(function ($value) {
  145. return trim($value, " \t");
  146. }, $values);
  147. }
  148. }