1
|
|
|
<?php |
2
|
|
|
namespace RingCentral\Psr7; |
3
|
|
|
|
4
|
|
|
use Psr\Http\Message\StreamInterface; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Trait implementing functionality common to requests and responses. |
8
|
|
|
*/ |
9
|
|
|
abstract class MessageTrait |
10
|
|
|
{ |
11
|
|
|
/** @var array Cached HTTP header collection with lowercase key to values */ |
12
|
|
|
protected $headers = array(); |
13
|
|
|
|
14
|
|
|
/** @var array Actual key to list of values per header. */ |
15
|
|
|
protected $headerLines = array(); |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
protected $protocol = '1.1'; |
19
|
|
|
|
20
|
|
|
/** @var StreamInterface */ |
21
|
|
|
protected $stream; |
22
|
|
|
|
23
|
|
|
public function getProtocolVersion() |
24
|
|
|
{ |
25
|
|
|
return $this->protocol; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function withProtocolVersion($version) |
29
|
|
|
{ |
30
|
|
|
if ($this->protocol === $version) { |
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$new = clone $this; |
35
|
|
|
$new->protocol = $version; |
36
|
|
|
return $new; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getHeaders() |
40
|
|
|
{ |
41
|
|
|
return $this->headerLines; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function hasHeader($header) |
45
|
|
|
{ |
46
|
|
|
return isset($this->headers[strtolower($header)]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getHeader($header) |
50
|
|
|
{ |
51
|
|
|
$name = strtolower($header); |
52
|
|
|
return isset($this->headers[$name]) ? $this->headers[$name] : array(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getHeaderLine($header) |
56
|
|
|
{ |
57
|
|
|
return implode(', ', $this->getHeader($header)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function withHeader($header, $value) |
61
|
|
|
{ |
62
|
|
|
$new = clone $this; |
63
|
|
|
$header = trim($header); |
64
|
|
|
$name = strtolower($header); |
65
|
|
|
|
66
|
|
|
if (!is_array($value)) { |
67
|
|
|
$new->headers[$name] = array(trim($value)); |
68
|
|
|
} else { |
69
|
|
|
$new->headers[$name] = $value; |
70
|
|
|
foreach ($new->headers[$name] as &$v) { |
71
|
|
|
$v = trim($v); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Remove the header lines. |
76
|
|
|
foreach (array_keys($new->headerLines) as $key) { |
77
|
|
|
if (strtolower($key) === $name) { |
78
|
|
|
unset($new->headerLines[$key]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Add the header line. |
83
|
|
|
$new->headerLines[$header] = $new->headers[$name]; |
84
|
|
|
|
85
|
|
|
return $new; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function withAddedHeader($header, $value) |
89
|
|
|
{ |
90
|
|
|
if (!$this->hasHeader($header)) { |
91
|
|
|
return $this->withHeader($header, $value); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$header = trim($header); |
95
|
|
|
$name = strtolower($header); |
96
|
|
|
|
97
|
|
|
$value = (array) $value; |
98
|
|
|
foreach ($value as &$v) { |
99
|
|
|
$v = trim($v); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$new = clone $this; |
103
|
|
|
$new->headers[$name] = array_merge($new->headers[$name], $value); |
104
|
|
|
$new->headerLines[$header] = array_merge($new->headerLines[$header], $value); |
105
|
|
|
|
106
|
|
|
return $new; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function withoutHeader($header) |
110
|
|
|
{ |
111
|
|
|
if (!$this->hasHeader($header)) { |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$new = clone $this; |
116
|
|
|
$name = strtolower($header); |
117
|
|
|
unset($new->headers[$name]); |
118
|
|
|
|
119
|
|
|
foreach (array_keys($new->headerLines) as $key) { |
120
|
|
|
if (strtolower($key) === $name) { |
121
|
|
|
unset($new->headerLines[$key]); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $new; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getBody() |
129
|
|
|
{ |
130
|
|
|
if (!$this->stream) { |
131
|
|
|
$this->stream = stream_for(''); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->stream; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function withBody(StreamInterface $body) |
138
|
|
|
{ |
139
|
|
|
if ($body === $this->stream) { |
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$new = clone $this; |
144
|
|
|
$new->stream = $body; |
145
|
|
|
return $new; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
protected function setHeaders(array $headers) |
149
|
|
|
{ |
150
|
|
|
$this->headerLines = $this->headers = array(); |
151
|
|
|
foreach ($headers as $header => $value) { |
152
|
|
|
$header = trim($header); |
153
|
|
|
$name = strtolower($header); |
154
|
|
|
if (!is_array($value)) { |
155
|
|
|
$value = trim($value); |
156
|
|
|
$this->headers[$name][] = $value; |
157
|
|
|
$this->headerLines[$header][] = $value; |
158
|
|
|
} else { |
159
|
|
|
foreach ($value as $v) { |
160
|
|
|
$v = trim($v); |
161
|
|
|
$this->headers[$name][] = $v; |
162
|
|
|
$this->headerLines[$header][] = $v; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|