66use Lkrms \Contract \IImmutable ;
77use Lkrms \Exception \InvalidArgumentException ;
88use Lkrms \Exception \InvalidArgumentTypeException ;
9+ use Lkrms \Http \Catalog \HttpProtocolVersion ;
910use Lkrms \Http \Contract \HttpHeadersInterface ;
1011use Psr \Http \Message \MessageInterface ;
1112use Psr \Http \Message \StreamInterface ;
@@ -32,29 +33,11 @@ abstract class HttpMessage implements MessageInterface, IImmutable
3233 public function __construct (
3334 $ body = null ,
3435 $ headers = null ,
35- string $ protocolVersion = '1.1 '
36+ string $ version = '1.1 '
3637 ) {
37- $ this ->ProtocolVersion = $ protocolVersion ;
38-
39- if ($ headers instanceof HttpHeadersInterface) {
40- $ this ->Headers = $ headers ;
41- } elseif (is_array ($ headers ) || $ headers === null ) {
42- $ this ->Headers = new HttpHeaders ($ headers ?: []);
43- } else {
44- throw new InvalidArgumentTypeException (2 , 'headers ' , 'HttpHeadersInterface|array<string,string[]>|null ' , $ headers );
45- }
46-
47- if ($ body instanceof StreamInterface) {
48- $ this ->Body = $ body ;
49- } elseif (is_string ($ body ) || $ body === null ) {
50- $ this ->Body = Stream::fromContents ((string ) $ body );
51- } else {
52- try {
53- $ this ->Body = new Stream ($ body );
54- } catch (InvalidArgumentException $ ex ) {
55- throw new InvalidArgumentTypeException (1 , 'body ' , 'StreamInterface|resource|string|null ' , $ body );
56- }
57- }
38+ $ this ->ProtocolVersion = $ this ->filterProtocolVersion ($ version );
39+ $ this ->Headers = $ this ->filterHeaders ($ headers );
40+ $ this ->Body = $ this ->filterBody ($ body );
5841 }
5942
6043 /**
@@ -65,14 +48,6 @@ public function getProtocolVersion(): string
6548 return $ this ->ProtocolVersion ;
6649 }
6750
68- /**
69- * @inheritDoc
70- */
71- public function withProtocolVersion (string $ version ): self
72- {
73- return $ this ->with ('ProtocolVersion ' , $ version );
74- }
75-
7651 /**
7752 * @inheritDoc
7853 */
@@ -105,6 +80,22 @@ public function getHeaderLine(string $name): string
10580 return $ this ->Headers ->getHeaderLine ($ name );
10681 }
10782
83+ /**
84+ * @inheritDoc
85+ */
86+ public function getBody (): StreamInterface
87+ {
88+ return $ this ->Body ;
89+ }
90+
91+ /**
92+ * @inheritDoc
93+ */
94+ public function withProtocolVersion (string $ version ): self
95+ {
96+ return $ this ->with ('ProtocolVersion ' , $ this ->filterProtocolVersion ($ version ));
97+ }
98+
10899 /**
109100 * @inheritDoc
110101 */
@@ -132,16 +123,61 @@ public function withoutHeader(string $name): self
132123 /**
133124 * @inheritDoc
134125 */
135- public function getBody ( ): StreamInterface
126+ public function withBody ( StreamInterface $ body ): self
136127 {
137- return $ this ->Body ;
128+ return $ this ->with ('Body ' , $ body );
129+ }
130+
131+ private function filterProtocolVersion (string $ version ): string
132+ {
133+ if (!HttpProtocolVersion::hasValue ($ version )) {
134+ throw new InvalidArgumentException (
135+ sprintf ('Invalid HTTP protocol version: %s ' , $ version )
136+ );
137+ }
138+ return $ version ;
138139 }
139140
140141 /**
141- * @inheritDoc
142+ * @param HttpHeadersInterface|array<string,string[]>|null $headers
142143 */
143- public function withBody ( StreamInterface $ body ): self
144+ private function filterHeaders ( $ headers ): HttpHeadersInterface
144145 {
145- return $ this ->with ('Body ' , $ body );
146+ if ($ headers instanceof HttpHeadersInterface) {
147+ return $ headers ;
148+ }
149+ if (is_array ($ headers ) || $ headers === null ) {
150+ return new HttpHeaders ($ headers ?: []);
151+ }
152+ // @phpstan-ignore-next-line
153+ throw new InvalidArgumentTypeException (
154+ 1 ,
155+ 'headers ' ,
156+ 'HttpHeadersInterface|array<string,string[]>|null ' ,
157+ $ headers
158+ );
159+ }
160+
161+ /**
162+ * @param StreamInterface|resource|string|null $body
163+ */
164+ private function filterBody ($ body ): StreamInterface
165+ {
166+ if ($ body instanceof StreamInterface) {
167+ return $ body ;
168+ }
169+ if (is_string ($ body ) || $ body === null ) {
170+ return Stream::fromContents ((string ) $ body );
171+ }
172+ try {
173+ return new Stream ($ body );
174+ } catch (InvalidArgumentException $ ex ) {
175+ throw new InvalidArgumentTypeException (
176+ 1 ,
177+ 'body ' ,
178+ 'StreamInterface|resource|string|null ' ,
179+ $ body
180+ );
181+ }
146182 }
147183}
0 commit comments