-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessage.php
185 lines (148 loc) · 4.08 KB
/
Message.php
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
declare(strict_types=1);
namespace SonsOfPHP\Component\HttpMessage;
use InvalidArgumentException;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamInterface;
/**
* {@inheritdoc}
*
* @author Joshua Estes <joshua@sonsofphp.com>
*/
class Message implements MessageInterface
{
public const DEFAULT_PROTOCOL_VERSION = '1.1';
private string $protocolVersion = self::DEFAULT_PROTOCOL_VERSION;
private array $headers = [];
private array $normalizedHeaders = [];
private StreamInterface $body;
/**
* {@inheritdoc}
*/
public function getProtocolVersion(): string
{
return $this->protocolVersion;
}
/**
* {@inheritdoc}
*/
public function withProtocolVersion(string $version): MessageInterface
{
$that = clone $this;
$that->protocolVersion = $version;
return $that;
}
/**
* {@inheritdoc}
*/
public function getHeaders(): array
{
return $this->headers;
}
/**
* {@inheritdoc}
*/
public function hasHeader(string $name): bool
{
return array_key_exists(strtolower($name), $this->normalizedHeaders);
}
/**
* {@inheritdoc}
*/
public function getHeader(string $name): array
{
if ($this->hasHeader($name)) {
return $this->normalizedHeaders[strtolower($name)];
}
return [];
}
/**
* {@inheritdoc}
*/
public function getHeaderLine(string $name): string
{
if (!$this->hasHeader($name)) {
return '';
}
return implode(', ', $this->getHeader($name));
}
/**
* {@inheritdoc}
*/
public function withHeader(string $name, $value): MessageInterface
{
if (preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $name) === 0 || preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $name) === false) {
throw new InvalidArgumentException(sprintf('"%s" is not valid header name.', $name));
}
$that = clone $this;
if (!is_array($value)) {
$value = [$value];
}
$that->normalizedHeaders[$name] = $value;
array_walk($value, function (&$val, $key): void {
$val = strtolower($val);
});
$that->headers[strtolower($name)] = $value;
return $that;
}
/**
* {@inheritdoc}
*/
public function withAddedHeader(string $name, $value): MessageInterface
{
if (preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $name) === 0 || preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $name) === false) {
throw new InvalidArgumentException(sprintf('"%s" is not valid header name.', $name));
}
if (!$this->hasHeader($name)) {
return $this->withHeader($name, $value);
}
if (is_string($value)) {
$value = [$value];
}
$that = clone $this;
$that->normalizedHeaders[$name][] = $value;
$values = $value;
array_walk($values, function (&$val, $key): void {
$val = strtolower($val);
});
$that->headers[strtolower($name)][] = $value;
return $that;
}
/**
* {@inheritdoc}
*/
public function withoutHeader(string $name): MessageInterface
{
if (!$this->hasHeader($name)) {
return $this;
}
$that = clone $this;
foreach (array_keys($this->headers) as $header) {
if (0 === strcasecmp($header, $name)) {
unset(
$that->headers[$header],
$that->normalizedHeaders[strtolower($name)]
);
break;
}
}
return $that;
}
/**
* {@inheritdoc}
*/
public function getBody(): StreamInterface
{
return $this->body;
}
/**
* {@inheritdoc}
*/
public function withBody(StreamInterface $body): MessageInterface
{
// @todo \InvalidArgumentException When the body is not valid.
$that = clone $this;
$that->body = $body;
return $that;
}
}