@@ -55,7 +55,7 @@ public function getProtocolVersion(): string
5555 *
5656 * @param string $version HTTP protocol version
5757 */
58- public function withProtocolVersion ($ version ): static
58+ public function withProtocolVersion (mixed $ version ): static
5959 {
6060 if ($ this ->protocol === $ version ) {
6161 return $ this ;
@@ -100,7 +100,7 @@ public function getHeaders(): array
100100 * name using a case-insensitive string comparison. Returns false if
101101 * no matching header name is found in the message.
102102 */
103- public function hasHeader ($ name ): bool
103+ public function hasHeader (mixed $ name ): bool
104104 {
105105 return isset ($ this ->headerNames [strtolower ($ name )]);
106106 }
@@ -117,7 +117,7 @@ public function hasHeader($name): bool
117117 * header. If the header does not appear in the message, this method MUST
118118 * return an empty array.
119119 */
120- public function getHeader ($ name ): array
120+ public function getHeader (mixed $ name ): array
121121 {
122122 $ name = strtolower ($ name );
123123
@@ -146,7 +146,7 @@ public function getHeader($name): array
146146 * concatenated together using a comma. If the header does not appear in
147147 * the message, this method MUST return an empty string.
148148 */
149- public function getHeaderLine ($ name ): string
149+ public function getHeaderLine (mixed $ name ): string
150150 {
151151 return implode (', ' , $ this ->getHeader ($ name ));
152152 }
@@ -163,7 +163,7 @@ public function getHeaderLine($name): string
163163 * @param string|string[] $value header value(s)
164164 * @throws InvalidArgumentException for invalid header names or values
165165 */
166- public function withHeader ($ name , $ value ): static
166+ public function withHeader (mixed $ name , mixed $ value ): static
167167 {
168168 if (! is_array ($ value )) {
169169 $ value = [$ value ];
@@ -204,7 +204,7 @@ public function withHeaders(array $headers): static
204204 * @param string|string[] $value header value(s)
205205 * @throws InvalidArgumentException for invalid header names or values
206206 */
207- public function withAddedHeader ($ name , $ value ): static
207+ public function withAddedHeader (mixed $ name , mixed $ value ): static
208208 {
209209 if (! is_array ($ value )) {
210210 $ value = [$ value ];
@@ -234,7 +234,7 @@ public function withAddedHeader($name, $value): static
234234 *
235235 * @param string $name case-insensitive header field name to remove
236236 */
237- public function withoutHeader ($ name ): static
237+ public function withoutHeader (mixed $ name ): static
238238 {
239239 $ normalized = strtolower ($ name );
240240
@@ -324,7 +324,92 @@ public function isMultipart(): bool
324324 }
325325 }
326326
327- private function setHeaders (array $ headers ): static
327+ public function setProtocolVersion (string $ version ): static
328+ {
329+ $ this ->protocol = $ version ;
330+ return $ this ;
331+ }
332+
333+ public function setHeader (string $ name , mixed $ value ): static
334+ {
335+ if (! is_array ($ value )) {
336+ $ value = [$ value ];
337+ }
338+
339+ $ value = $ this ->trimHeaderValues ($ value );
340+ $ normalized = strtolower ($ name );
341+
342+ if (isset ($ this ->headerNames [$ normalized ])) {
343+ unset($ this ->headers [$ this ->headerNames [$ normalized ]]);
344+ }
345+ $ this ->headerNames [$ normalized ] = $ name ;
346+ $ this ->headers [$ name ] = $ value ;
347+
348+ return $ this ;
349+ }
350+
351+ public function addHeader (string $ name , mixed $ value ): static
352+ {
353+ if (! is_array ($ value )) {
354+ $ value = [$ value ];
355+ }
356+
357+ $ value = $ this ->trimHeaderValues ($ value );
358+ $ normalized = strtolower ($ name );
359+
360+ if (isset ($ this ->headerNames [$ normalized ])) {
361+ $ name = $ this ->headerNames [$ normalized ];
362+ $ this ->headers [$ name ] = array_merge ($ this ->headers [$ name ], $ value );
363+ } else {
364+ $ this ->headerNames [$ normalized ] = $ name ;
365+ $ this ->headers [$ name ] = $ value ;
366+ }
367+
368+ return $ this ;
369+ }
370+
371+ public function unsetHeader (string $ name ): static
372+ {
373+ $ normalized = strtolower ($ name );
374+
375+ if (! isset ($ this ->headerNames [$ normalized ])) {
376+ return $ this ;
377+ }
378+
379+ $ name = $ this ->headerNames [$ normalized ];
380+
381+ unset($ this ->headers [$ name ], $ this ->headerNames [$ normalized ]);
382+
383+ return $ this ;
384+ }
385+
386+ public function getStandardHeaders (): array
387+ {
388+ $ headers = $ this ->getHeaders ();
389+ if (! $ this ->hasHeader ('connection ' )) {
390+ $ headers ['Connection ' ] = [$ this ->shouldKeepAlive () ? 'keep-alive ' : 'close ' ];
391+ }
392+ if (! $ this ->hasHeader ('content-length ' )) {
393+ $ headers ['Content-Length ' ] = [(string ) ($ this ->getBody ()->getSize () ?? 0 )];
394+ }
395+ return $ headers ;
396+ }
397+
398+ public function shouldKeepAlive (): bool
399+ {
400+ return strtolower ($ this ->getHeaderLine ('Connection ' )) === 'keep-alive ' ;
401+ }
402+
403+ public function setBody (StreamInterface $ body ): static
404+ {
405+ $ this ->stream = $ body ;
406+ return $ this ;
407+ }
408+
409+ /**
410+ * @param array<string, array<string>|string> $headers
411+ */
412+ public function setHeaders (array $ headers ): static
328413 {
329414 $ this ->headerNames = $ this ->headers = [];
330415 foreach ($ headers as $ header => $ value ) {
0 commit comments