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