Skip to content

Commit 70d7c44

Browse files
author
Paul M. Jones
committed
Merge pull request #16 from weierophinney/feature/immutability
Immutability + URI interface
2 parents 18619ee + 2721ad1 commit 70d7c44

10 files changed

+812
-478
lines changed

src/IncomingRequestInterface.php

Lines changed: 0 additions & 171 deletions
This file was deleted.

src/IncomingResponseInterface.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/MessageInterface.php

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
* from a server to a client. This interface defines the methods common to
88
* each.
99
*
10+
* Messages are considered immutable; all methods that might change state MUST
11+
* be implemented such that they retain the internal state of the current
12+
* message and return a new instance that contains the changed state.
13+
*
1014
* @link http://www.ietf.org/rfc/rfc7230.txt
1115
* @link http://www.ietf.org/rfc/rfc7231.txt
1216
*/
1317
interface MessageInterface
1418
{
1519
/**
16-
* Gets the HTTP protocol version as a string.
20+
* Retrieves the HTTP protocol version as a string.
1721
*
1822
* The string MUST contain only the HTTP version number (e.g., "1.1", "1.0").
1923
*
@@ -22,14 +26,22 @@ interface MessageInterface
2226
public function getProtocolVersion();
2327

2428
/**
25-
* Gets the body of the message.
29+
* Create a new instance with the specified HTTP protocol version.
30+
*
31+
* The version string MUST contain only the HTTP version number (e.g.,
32+
* "1.1", "1.0").
2633
*
27-
* @return StreamableInterface|null Returns the body, or null if not set.
34+
* This method MUST be implemented in such a way as to retain the
35+
* immutability of the message, and MUST return a new instance that has the
36+
* new protocol version.
37+
*
38+
* @param string $version HTTP protocol version
39+
* @return self
2840
*/
29-
public function getBody();
41+
public function withProtocolVersion($version);
3042

3143
/**
32-
* Gets all message headers.
44+
* Retrieves all message headers.
3345
*
3446
* The keys represent the header name as it will be sent over the wire, and
3547
* each value is an array of strings associated with the header.
@@ -69,7 +81,8 @@ public function hasHeader($header);
6981
* a comma.
7082
*
7183
* NOTE: Not all header values may be appropriately represented using
72-
* comma concatenation.
84+
* comma concatenation. For such headers, use getHeaderLines() instead
85+
* and supply your own delimiter when concatenating.
7386
*
7487
* @param string $header Case-insensitive header name.
7588
* @return string
@@ -82,5 +95,78 @@ public function getHeader($header);
8295
* @param string $header Case-insensitive header name.
8396
* @return string[]
8497
*/
85-
public function getHeaderAsArray($header);
98+
public function getHeaderLines($header);
99+
100+
/**
101+
* Create a new instance with the provided header, replacing any existing
102+
* values of any headers with the same case-insensitive name.
103+
*
104+
* The header name is case-insensitive. The header values MUST be a string
105+
* or an array of strings.
106+
*
107+
* This method MUST be implemented in such a way as to retain the
108+
* immutability of the message, and MUST return a new instance that has the
109+
* new and/or updated header and value.
110+
*
111+
* @param string $header Header name
112+
* @param string|string[] $value Header value(s).
113+
* @return self
114+
* @throws \InvalidArgumentException for invalid header names or values.
115+
*/
116+
public function withHeader($header, $value);
117+
118+
/**
119+
* Creates a new instance, with the specified header appended with the
120+
* given value.
121+
*
122+
* Existing values for the specified header will be maintained. The new
123+
* value(s) will be appended to the existing list. If the header did not
124+
* exist previously, it will be added.
125+
*
126+
* This method MUST be implemented in such a way as to retain the
127+
* immutability of the message, and MUST return a new instance that has the
128+
* new header and/or value.
129+
*
130+
* @param string $header Header name to add
131+
* @param string|string[] $value Header value(s).
132+
* @return self
133+
* @throws \InvalidArgumentException for invalid header names or values.
134+
*/
135+
public function withAddedHeader($header, $value);
136+
137+
/**
138+
* Creates a new instance, without the specified header.
139+
*
140+
* Header resolution MUST be done without case-sensitivity.
141+
*
142+
* This method MUST be implemented in such a way as to retain the
143+
* immutability of the message, and MUST return a new instance that removes
144+
* the named header.
145+
*
146+
* @param string $header HTTP header to remove
147+
* @return self
148+
*/
149+
public function withoutHeader($header);
150+
151+
/**
152+
* Gets the body of the message.
153+
*
154+
* @return StreamableInterface Returns the body as a stream.
155+
*/
156+
public function getBody();
157+
158+
/**
159+
* Create a new instance, with the specified message body.
160+
*
161+
* The body MUST be a StreamableInterface object.
162+
*
163+
* This method MUST be implemented in such a way as to retain the
164+
* immutability of the message, and MUST return a new instance that has the
165+
* new body stream.
166+
*
167+
* @param StreamableInterface $body Body.
168+
* @return self
169+
* @throws \InvalidArgumentException When the body is not valid.
170+
*/
171+
public function withBody(StreamableInterface $body);
86172
}

0 commit comments

Comments
 (0)