ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Message.php
Go to the documentation of this file.
1<?php
9namespace Slim\Http;
10
11use InvalidArgumentException;
14
25abstract class Message implements MessageInterface
26{
32 protected $protocolVersion = '1.1';
33
39 protected static $validProtocolVersions = [
40 '1.0' => true,
41 '1.1' => true,
42 '2.0' => true,
43 '2' => true,
44 ];
45
51 protected $headers;
52
58 protected $body;
59
60
64 public function __set($name, $value)
65 {
66 // Do nothing
67 }
68
69 /*******************************************************************************
70 * Protocol
71 ******************************************************************************/
72
80 public function getProtocolVersion()
81 {
83 }
84
100 {
101 if (!isset(self::$validProtocolVersions[$version])) {
102 throw new InvalidArgumentException(
103 'Invalid HTTP version. Must be one of: '
104 . implode(', ', array_keys(self::$validProtocolVersions))
105 );
106 }
107 $clone = clone $this;
108 $clone->protocolVersion = $version;
109
110 return $clone;
111 }
112
113 /*******************************************************************************
114 * Headers
115 ******************************************************************************/
116
142 public function getHeaders()
143 {
144 return $this->headers->all();
145 }
146
155 public function hasHeader($name)
156 {
157 return $this->headers->has($name);
158 }
159
174 public function getHeader($name)
175 {
176 return $this->headers->get($name, []);
177 }
178
198 public function getHeaderLine($name)
199 {
200 return implode(',', $this->headers->get($name, []));
201 }
202
218 public function withHeader($name, $value)
219 {
220 $clone = clone $this;
221 $clone->headers->set($name, $value);
222
223 return $clone;
224 }
225
242 public function withAddedHeader($name, $value)
243 {
244 $clone = clone $this;
245 $clone->headers->add($name, $value);
246
247 return $clone;
248 }
249
262 public function withoutHeader($name)
263 {
264 $clone = clone $this;
265 $clone->headers->remove($name);
266
267 return $clone;
268 }
269
270 /*******************************************************************************
271 * Body
272 ******************************************************************************/
273
279 public function getBody()
280 {
281 return $this->body;
282 }
283
298 {
299 // TODO: Test for invalid body?
300 $clone = clone $this;
301 $clone->body = $body;
302
303 return $clone;
304 }
305}
$version
Definition: build.php:27
An exception for terminatinating execution or to throw for unit testing.
Abstract message (base class for Request and Response)
Definition: Message.php:26
withHeader($name, $value)
Return an instance with the provided value replacing the specified header.
Definition: Message.php:218
withAddedHeader($name, $value)
Return an instance with the specified header appended with the given value.
Definition: Message.php:242
getHeaderLine($name)
Retrieves a comma-separated string of the values for a single header.
Definition: Message.php:198
getProtocolVersion()
Retrieves the HTTP protocol version as a string.
Definition: Message.php:80
getHeader($name)
Retrieves a message header value by the given case-insensitive name.
Definition: Message.php:174
hasHeader($name)
Checks if a header exists by the given case-insensitive name.
Definition: Message.php:155
getHeaders()
Retrieves all message header values.
Definition: Message.php:142
static $validProtocolVersions
Definition: Message.php:39
withBody(StreamInterface $body)
Return an instance with the specified message body.
Definition: Message.php:297
getBody()
Gets the body of the message.
Definition: Message.php:279
withoutHeader($name)
Return an instance without the specified header.
Definition: Message.php:262
__set($name, $value)
Disable magic setter to ensure immutability.
Definition: Message.php:64
withProtocolVersion($version)
Return an instance with the specified HTTP protocol version.
Definition: Message.php:99
HTTP messages consist of requests from a client to a server and responses from a server to a client.
Describes a data stream.
Slim Framework (https://slimframework.com)
Definition: Body.php:9