forked from ringcentral/psr7
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MessageTrait.php
162 lines (133 loc) · 3.83 KB
/
MessageTrait.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
<?php
namespace GuzzleHttp\Psr7;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamableInterface;
/**
* Trait implementing functionality common to requests and responses.
*/
trait MessageTrait
{
/** @var array Cached HTTP header collection with lowercase key to values */
private $headers = [];
/** @var array Actual key to list of values per header. */
private $headerLines = [];
/** @var string */
private $protocol = '1.1';
/** @var StreamableInterface */
private $stream;
public function getProtocolVersion()
{
return $this->protocol;
}
public function withProtocolVersion($version)
{
if ($this->protocol === $version) {
return $this;
}
$new = clone $this;
$new->protocol = $version;
return $new;
}
public function getHeaders()
{
return $this->headerLines;
}
public function hasHeader($header)
{
return isset($this->headers[strtolower($header)]);
}
public function getHeader($header)
{
$name = strtolower($header);
return isset($this->headers[$name])
? implode(', ', $this->headers[$name])
: '';
}
public function getHeaderLines($header)
{
$name = strtolower($header);
return isset($this->headers[$name]) ? $this->headers[$name] : [];
}
public function withHeader($header, $value)
{
$new = clone $this;
$header = trim($header);
$name = strtolower($header);
if (!is_array($value)) {
$new->headers[$name] = [trim($value)];
} else {
foreach ($value as $v) {
$new->headers[$name][] = trim($v);
}
}
// Remove the header lines.
foreach (array_keys($new->headerLines) as $key) {
if (strtolower($key) === $name) {
unset($new->headerLines[$key]);
}
}
// Add the header line.
$new->headerLines[$header] = $new->headers[$name];
return $new;
}
public function withAddedHeader($header, $value)
{
if (!$this->hasHeader($header)) {
return $this->withHeader($header, $value);
}
$new = clone $this;
$new->headers[strtolower($header)][] = $value;
$new->headerLines[$header][] = $value;
return $new;
}
public function withoutHeader($header)
{
if (!$this->hasHeader($header)) {
return $this;
}
$new = clone $this;
$name = strtolower($header);
unset($new->headers[$name]);
foreach (array_keys($new->headerLines) as $key) {
if (strtolower($key) === $name) {
unset($new->headerLines[$key]);
}
}
return $new;
}
public function getBody()
{
if (!$this->stream) {
$this->stream = stream_for('');
}
return $this->stream;
}
public function withBody(StreamableInterface $body)
{
if ($body === $this->stream) {
return $this;
}
$new = clone $this;
$new->stream = $body;
return $new;
}
private function setHeaders(array $headers)
{
$this->headerLines = $this->headers = [];
foreach ($headers as $header => $value) {
$header = trim($header);
$name = strtolower($header);
if (!is_array($value)) {
$value = trim($value);
$this->headers[$name][] = $value;
$this->headerLines[$header][] = $value;
} else {
foreach ($value as $v) {
$v = trim($v);
$this->headers[$name][] = $v;
$this->headerLines[$header][] = $v;
}
}
}
}
}