ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Message.php
Go to the documentation of this file.
1<?php
2
3namespace Sabre\HTTP;
4
14abstract class Message implements MessageInterface {
15
23 protected $body;
24
30 protected $headers = [];
31
37 protected $httpVersion = '1.1';
38
47 function getBodyAsStream() {
48
49 $body = $this->getBody();
50 if (is_string($body) || is_null($body)) {
51 $stream = fopen('php://temp', 'r+');
52 fwrite($stream, $body);
53 rewind($stream);
54 return $stream;
55 }
56 return $body;
57
58 }
59
68 function getBodyAsString() {
69
70 $body = $this->getBody();
71 if (is_string($body)) {
72 return $body;
73 }
74 if (is_null($body)) {
75 return '';
76 }
77 $contentLength = $this->getHeader('Content-Length');
78 if (is_int($contentLength) || ctype_digit($contentLength)) {
79 return stream_get_contents($body, $contentLength);
80 } else {
81 return stream_get_contents($body);
82 }
83 }
84
92 function getBody() {
93
94 return $this->body;
95
96 }
97
103 function setBody($body) {
104
105 $this->body = $body;
106
107 }
108
116 function getHeaders() {
117
118 $result = [];
119 foreach ($this->headers as $headerInfo) {
120 $result[$headerInfo[0]] = $headerInfo[1];
121 }
122 return $result;
123
124 }
125
132 function hasHeader($name) {
133
134 return isset($this->headers[strtolower($name)]);
135
136 }
137
154 function getHeader($name) {
155
156 $name = strtolower($name);
157
158 if (isset($this->headers[$name])) {
159 return implode(',', $this->headers[$name][1]);
160 }
161 return null;
162
163 }
164
177
178 $name = strtolower($name);
179
180 if (isset($this->headers[$name])) {
181 return $this->headers[$name][1];
182 }
183
184 return [];
185
186 }
187
199 function setHeader($name, $value) {
200
201 $this->headers[strtolower($name)] = [$name, (array)$value];
202
203 }
204
216 function setHeaders(array $headers) {
217
218 foreach ($headers as $name => $value) {
219 $this->setHeader($name, $value);
220 }
221
222 }
223
235 function addHeader($name, $value) {
236
237 $lName = strtolower($name);
238 if (isset($this->headers[$lName])) {
239 $this->headers[$lName][1] = array_merge(
240 $this->headers[$lName][1],
241 (array)$value
242 );
243 } else {
244 $this->headers[$lName] = [
245 $name,
246 (array)$value
247 ];
248 }
249
250 }
251
260 function addHeaders(array $headers) {
261
262 foreach ($headers as $name => $value) {
263 $this->addHeader($name, $value);
264 }
265
266 }
267
268
279 function removeHeader($name) {
280
281 $name = strtolower($name);
282 if (!isset($this->headers[$name])) {
283 return false;
284 }
285 unset($this->headers[$name]);
286 return true;
287
288 }
289
299
300 $this->httpVersion = $version;
301
302 }
303
309 function getHttpVersion() {
310
311 return $this->httpVersion;
312
313 }
314}
$result
$version
Definition: build.php:27
An exception for terminatinating execution or to throw for unit testing.
This is the abstract base class for both the Request and Response objects.
Definition: Message.php:14
removeHeader($name)
Removes a HTTP header.
Definition: Message.php:279
addHeader($name, $value)
Adds a HTTP header.
Definition: Message.php:235
getHeaderAsArray($name)
Returns a HTTP header as an array.
Definition: Message.php:176
getHttpVersion()
Returns the HTTP version.
Definition: Message.php:309
setHeader($name, $value)
Updates a HTTP header.
Definition: Message.php:199
setHttpVersion($version)
Sets the HTTP version.
Definition: Message.php:298
addHeaders(array $headers)
Adds a new set of HTTP headers.
Definition: Message.php:260
getBodyAsString()
Returns the body as a string.
Definition: Message.php:68
getHeaders()
Returns all the HTTP headers as an array.
Definition: Message.php:116
hasHeader($name)
Will return true or false, depending on if a HTTP header exists.
Definition: Message.php:132
setHeaders(array $headers)
Sets a new set of HTTP headers.
Definition: Message.php:216
getBody()
Returns the message body, as it's internal representation.
Definition: Message.php:92
getBodyAsStream()
Returns the body as a readable stream resource.
Definition: Message.php:47
getHeader($name)
Returns a specific HTTP header, based on it's name.
Definition: Message.php:154
setBody($body)
Replaces the body resource with a new stream or string.
Definition: Message.php:103
The MessageInterface is the base interface that's used by both the RequestInterface and ResponseInter...
if(preg_match('#\.( $contentLength[^/\.]+) $#D', $path, $type)) if($contentType===null)
Definition: module.php:165
$stream
PHP stream implementation.