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