1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Kotchasan\Http;
12:
13: use Psr\Http\Message\MessageInterface;
14: use Psr\Http\Message\StreamInterface;
15:
16: 17: 18: 19: 20: 21: 22:
23: abstract class AbstractMessage implements MessageInterface
24: {
25: 26: 27:
28: protected $headers = array();
29: 30: 31:
32: protected $protocol = '1.1';
33: 34: 35:
36: protected $stream;
37:
38: 39: 40: 41: 42:
43: public function __construct($with_header = false)
44: {
45: if ($with_header) {
46: $this->headers = $this->getRequestHeaders();
47: }
48: }
49:
50: 51: 52: 53: 54:
55: public function getBody()
56: {
57: return $this->stream;
58: }
59:
60: 61: 62: 63: 64: 65: 66: 67:
68: public function getHeader($name)
69: {
70: return isset($this->headers[$name]) ? $this->headers[$name] : array();
71: }
72:
73: 74: 75: 76: 77: 78: 79: 80:
81: public function getHeaderLine($name)
82: {
83: $values = $this->getHeader($name);
84: return empty($values) ? '' : implode(',', $values);
85: }
86:
87: 88: 89: 90: 91:
92: public function getHeaders()
93: {
94: return $this->headers;
95: }
96:
97: 98: 99: 100: 101: 102:
103: public function getProtocolVersion()
104: {
105: return $this->protocol;
106: }
107:
108: 109: 110: 111: 112: 113: 114: 115:
116: public function hasHeader($name)
117: {
118: return isset($this->headers[$name]);
119: }
120:
121: 122: 123: 124: 125: 126: 127: 128: 129: 130:
131: public function withAddedHeader($name, $value)
132: {
133: $this->filterHeader($name);
134: $clone = clone $this;
135: if (is_array($value)) {
136: foreach ($value as $item) {
137: $clone->headers[$name][] = $item;
138: }
139: } else {
140: $clone->headers[$name][] = $value;
141: }
142: return $clone;
143: }
144:
145: 146: 147: 148: 149: 150: 151:
152: public function withBody(StreamInterface $body)
153: {
154: $clone = clone $this;
155: $clone->stream = $body;
156: return $clone;
157: }
158:
159: 160: 161: 162: 163: 164: 165: 166: 167: 168:
169: public function withHeader($name, $value)
170: {
171: $this->filterHeader($name);
172: $clone = clone $this;
173: $clone->headers[$name] = is_array($value) ? $value : (array) $value;
174: return $clone;
175: }
176:
177: 178: 179: 180: 181: 182: 183: 184: 185:
186: public function withHeaders($headers)
187: {
188: $clone = clone $this;
189: foreach ($headers as $name => $value) {
190: $this->filterHeader($name);
191: $clone->headers[$name] = is_array($value) ? $value : (array) $value;
192: }
193: return $clone;
194: }
195:
196: 197: 198: 199: 200: 201: 202:
203: public function withProtocolVersion($version)
204: {
205: $clone = clone $this;
206: $clone->protocol = $version;
207: return $clone;
208: }
209:
210: 211: 212: 213: 214: 215: 216:
217: public function withoutHeader($name)
218: {
219: $clone = clone $this;
220: unset($clone->headers[$name]);
221: return $clone;
222: }
223:
224: 225: 226: 227: 228: 229: 230:
231: protected function filterHeader($name)
232: {
233: if (!preg_match('/^[a-zA-Z0-9\-]+$/', $name)) {
234: throw new \InvalidArgumentException('Invalid header name');
235: }
236: }
237:
238: 239: 240: 241: 242:
243: protected function getRequestHeaders()
244: {
245: $headers = array();
246: if (function_exists("apache_request_headers")) {
247: foreach (apache_request_headers() as $key => $value) {
248: if (preg_match('/^[a-zA-Z0-9\-]+$/', $key)) {
249: $headers[$key] = array($value);
250: }
251: }
252: } else {
253: foreach ($_SERVER as $key => $value) {
254: if (preg_match('/^HTTP_([a-zA-Z0-9_]+)$/', $key, $match)) {
255: $headers[str_replace(' ', '-', ucwords(strtolower(str_replace(array('_', '-'), ' ', $match[1]))))] = array($value);
256: }
257: }
258: }
259: return $headers;
260: }
261: }
262: