説明なし

MessageTrait.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 Cached HTTP header collection with lowercase key to values */
  10. private $headers = [];
  11. /** @var array Actual key to list of values per header. */
  12. private $headerLines = [];
  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->headerLines;
  33. }
  34. public function hasHeader($header)
  35. {
  36. return isset($this->headers[strtolower($header)]);
  37. }
  38. public function getHeader($header)
  39. {
  40. $name = strtolower($header);
  41. return isset($this->headers[$name]) ? $this->headers[$name] : [];
  42. }
  43. public function getHeaderLine($header)
  44. {
  45. return implode(', ', $this->getHeader($header));
  46. }
  47. public function withHeader($header, $value)
  48. {
  49. $new = clone $this;
  50. $header = trim($header);
  51. $name = strtolower($header);
  52. if (!is_array($value)) {
  53. $new->headers[$name] = [trim($value)];
  54. } else {
  55. $new->headers[$name] = $value;
  56. foreach ($new->headers[$name] as &$v) {
  57. $v = trim($v);
  58. }
  59. }
  60. // Remove the header lines.
  61. foreach (array_keys($new->headerLines) as $key) {
  62. if (strtolower($key) === $name) {
  63. unset($new->headerLines[$key]);
  64. }
  65. }
  66. // Add the header line.
  67. $new->headerLines[$header] = $new->headers[$name];
  68. return $new;
  69. }
  70. public function withAddedHeader($header, $value)
  71. {
  72. if (!$this->hasHeader($header)) {
  73. return $this->withHeader($header, $value);
  74. }
  75. $new = clone $this;
  76. $new->headers[strtolower($header)][] = $value;
  77. $new->headerLines[$header][] = $value;
  78. return $new;
  79. }
  80. public function withoutHeader($header)
  81. {
  82. if (!$this->hasHeader($header)) {
  83. return $this;
  84. }
  85. $new = clone $this;
  86. $name = strtolower($header);
  87. unset($new->headers[$name]);
  88. foreach (array_keys($new->headerLines) as $key) {
  89. if (strtolower($key) === $name) {
  90. unset($new->headerLines[$key]);
  91. }
  92. }
  93. return $new;
  94. }
  95. public function getBody()
  96. {
  97. if (!$this->stream) {
  98. $this->stream = stream_for('');
  99. }
  100. return $this->stream;
  101. }
  102. public function withBody(StreamInterface $body)
  103. {
  104. if ($body === $this->stream) {
  105. return $this;
  106. }
  107. $new = clone $this;
  108. $new->stream = $body;
  109. return $new;
  110. }
  111. private function setHeaders(array $headers)
  112. {
  113. $this->headerLines = $this->headers = [];
  114. foreach ($headers as $header => $value) {
  115. $header = trim($header);
  116. $name = strtolower($header);
  117. if (!is_array($value)) {
  118. $value = trim($value);
  119. $this->headers[$name][] = $value;
  120. $this->headerLines[$header][] = $value;
  121. } else {
  122. foreach ($value as $v) {
  123. $v = trim($v);
  124. $this->headers[$name][] = $v;
  125. $this->headerLines[$header][] = $v;
  126. }
  127. }
  128. }
  129. }
  130. }