1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodeJet\Http; |
4
|
|
|
|
5
|
|
|
use Doctrine\Instantiator\Exception\InvalidArgumentException; |
6
|
|
|
use Psr\Http\Message\MessageInterface; |
7
|
|
|
use Psr\Http\Message\StreamInterface; |
8
|
|
|
|
9
|
|
|
abstract class Message implements MessageInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $protocolVersion = '1.1'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array[string] |
18
|
|
|
*/ |
19
|
|
|
private $validProtocolVersions = ['1.0', '1.1']; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array[string] |
23
|
|
|
*/ |
24
|
|
|
protected $headers = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var StreamInterface |
28
|
|
|
*/ |
29
|
|
|
protected $body; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array[string] |
33
|
|
|
*/ |
34
|
|
|
protected $normalizedHeaderIndex = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritDoc |
38
|
|
|
*/ |
39
|
|
|
public function getProtocolVersion() |
40
|
|
|
{ |
41
|
|
|
return $this->protocolVersion; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
|
|
public function withProtocolVersion($protocolVersion) |
48
|
|
|
{ |
49
|
|
|
if (!in_array($protocolVersion, $this->validProtocolVersions)) { |
50
|
|
|
throw new InvalidArgumentException('Invalid Protocol Version.'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$clone = clone $this; |
54
|
|
|
$clone->protocolVersion = $protocolVersion; |
55
|
|
|
|
56
|
|
|
return $clone; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritDoc |
61
|
|
|
*/ |
62
|
|
|
public function getHeaders() |
63
|
|
|
{ |
64
|
|
|
return $this->headers; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritDoc |
69
|
|
|
*/ |
70
|
|
|
public function hasHeader($name) |
71
|
|
|
{ |
72
|
|
|
if (isset($this->normalizedHeaderIndex[strtolower($name)])) { |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritDoc |
81
|
|
|
*/ |
82
|
|
|
public function getHeader($name) |
83
|
|
|
{ |
84
|
|
|
if (!$this->hasHeader($name)) { |
85
|
|
|
return []; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->headers[$this->normalizedHeaderIndex[strtolower($name)]]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritDoc |
93
|
|
|
*/ |
94
|
|
|
public function getHeaderLine($name) |
95
|
|
|
{ |
96
|
|
|
if (!$this->hasHeader($name)) { |
97
|
|
|
return ''; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return implode(", ", $this->getHeader($name)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritDoc |
105
|
|
|
*/ |
106
|
|
|
public function withHeader($name, $value) |
107
|
|
|
{ |
108
|
|
|
$clone = clone $this; |
109
|
|
|
|
110
|
|
|
if ($this->hasHeader($name)) { |
111
|
|
|
unset($clone->headers[$this->normalizedHeaderIndex[strtolower($name)]]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$clone->normalizedHeaderIndex[strtolower($name)] = $name; |
115
|
|
|
$clone->headers[$name] = $value; |
116
|
|
|
|
117
|
|
|
return $clone; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritDoc |
122
|
|
|
*/ |
123
|
|
|
public function withAddedHeader($name, $value) |
124
|
|
|
{ |
125
|
|
|
if (!$this->hasHeader($name)) { |
126
|
|
|
return $this->withHeader($name, $value); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$headerIndex = $this->normalizedHeaderIndex[strtolower($name)]; |
130
|
|
|
|
131
|
|
|
$clone = clone $this; |
132
|
|
|
|
133
|
|
|
if (!is_array($clone->headers[$headerIndex])) { |
134
|
|
|
$clone->headers[$headerIndex] = [ |
135
|
|
|
$clone->headers[$headerIndex] |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
array_push($clone->headers[$headerIndex], $value); |
140
|
|
|
|
141
|
|
|
return $clone; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @inheritDoc |
146
|
|
|
*/ |
147
|
|
|
public function withoutHeader($name) |
148
|
|
|
{ |
149
|
|
|
if (!$this->hasHeader($name)) { |
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$clone = clone $this; |
154
|
|
|
$normalizedHeaderIndex = $this->normalizedHeaderIndex[strtolower($name)]; |
155
|
|
|
unset($clone->headers[$normalizedHeaderIndex]); |
156
|
|
|
unset($clone->normalizedHeaderIndex[strtolower($name)]); |
157
|
|
|
|
158
|
|
|
return $clone; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @inheritDoc |
163
|
|
|
*/ |
164
|
|
|
public function getBody() |
165
|
|
|
{ |
166
|
|
|
return $this->body; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @inheritDoc |
171
|
|
|
*/ |
172
|
|
|
public function withBody(StreamInterface $body) |
173
|
|
|
{ |
174
|
|
|
$clone = clone $this; |
175
|
|
|
$clone->body = $body; |
176
|
|
|
|
177
|
|
|
return $clone; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|