Message.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Slim Framework (https://slimframework.com)
  4. *
  5. * @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE.md (MIT License)
  6. */
  7. declare(strict_types=1);
  8. namespace Slim\Psr7;
  9. use InvalidArgumentException;
  10. use Psr\Http\Message\MessageInterface;
  11. use Psr\Http\Message\StreamInterface;
  12. use Slim\Psr7\Interfaces\HeadersInterface;
  13. use function array_keys;
  14. use function header;
  15. use function header_remove;
  16. use function implode;
  17. use function sprintf;
  18. abstract class Message implements MessageInterface
  19. {
  20. /**
  21. * @var string
  22. */
  23. protected $protocolVersion = '1.1';
  24. /**
  25. * @var array
  26. */
  27. protected static $validProtocolVersions = [
  28. '1.0' => true,
  29. '1.1' => true,
  30. '2.0' => true,
  31. '2' => true,
  32. ];
  33. /**
  34. * @var HeadersInterface
  35. */
  36. protected $headers;
  37. /**
  38. * @var StreamInterface
  39. */
  40. protected $body;
  41. /**
  42. * Disable magic setter to ensure immutability
  43. *
  44. * @param string $name The property name
  45. * @param mixed $value The property value
  46. *
  47. * @return void
  48. */
  49. public function __set($name, $value): void
  50. {
  51. // Do nothing
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function getProtocolVersion(): string
  57. {
  58. return $this->protocolVersion;
  59. }
  60. /**
  61. * @return static
  62. * {@inheritdoc}
  63. */
  64. public function withProtocolVersion($version)
  65. {
  66. if (!isset(self::$validProtocolVersions[$version])) {
  67. throw new InvalidArgumentException(
  68. 'Invalid HTTP version. Must be one of: '
  69. . implode(', ', array_keys(self::$validProtocolVersions))
  70. );
  71. }
  72. $clone = clone $this;
  73. $clone->protocolVersion = $version;
  74. return $clone;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function getHeaders(): array
  80. {
  81. return $this->headers->getHeaders(true);
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function hasHeader($name): bool
  87. {
  88. return $this->headers->hasHeader($name);
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getHeader($name): array
  94. {
  95. return $this->headers->getHeader($name);
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function getHeaderLine($name): string
  101. {
  102. $values = $this->headers->getHeader($name);
  103. return implode(',', $values);
  104. }
  105. /**
  106. * @return static
  107. * {@inheritdoc}
  108. */
  109. public function withHeader($name, $value)
  110. {
  111. $clone = clone $this;
  112. $clone->headers->setHeader($name, $value);
  113. if ($this instanceof Response && $this->body instanceof NonBufferedBody) {
  114. header(sprintf('%s: %s', $name, $clone->getHeaderLine($name)));
  115. }
  116. return $clone;
  117. }
  118. /**
  119. * @return static
  120. * {@inheritdoc}
  121. */
  122. public function withAddedHeader($name, $value)
  123. {
  124. $clone = clone $this;
  125. $clone->headers->addHeader($name, $value);
  126. if ($this instanceof Response && $this->body instanceof NonBufferedBody) {
  127. header(sprintf('%s: %s', $name, $clone->getHeaderLine($name)));
  128. }
  129. return $clone;
  130. }
  131. /**
  132. * @return static
  133. * {@inheritdoc}
  134. */
  135. public function withoutHeader($name)
  136. {
  137. $clone = clone $this;
  138. $clone->headers->removeHeader($name);
  139. if ($this instanceof Response && $this->body instanceof NonBufferedBody) {
  140. header_remove($name);
  141. }
  142. return $clone;
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function getBody(): StreamInterface
  148. {
  149. return $this->body;
  150. }
  151. /**
  152. * @return static
  153. * {@inheritdoc}
  154. */
  155. public function withBody(StreamInterface $body)
  156. {
  157. $clone = clone $this;
  158. $clone->body = $body;
  159. return $clone;
  160. }
  161. }