1919 * @phpstan-type RequestArrayShape array{
2020 * method: string,
2121 * uri: string,
22- * headers: array<string, string| list<string>>,
22+ * headers: array<string, list<string>>,
2323 * body?: string|null
2424 * }
2525 *
@@ -43,7 +43,7 @@ class Request extends AbstractDataTransferObject
4343 protected string $ uri ;
4444
4545 /**
46- * @var array<string, string| list<string>> The request headers.
46+ * @var array<string, list<string>> The request headers.
4747 */
4848 protected array $ headers ;
4949
@@ -79,7 +79,7 @@ public function __construct($method, string $uri, array $headers = [], ?string $
7979 }
8080
8181 $ this ->uri = $ uri ;
82- $ this ->headers = $ headers ;
82+ $ this ->headers = $ this -> normalizeHeaders ( $ headers) ;
8383 $ this ->body = $ body ;
8484 }
8585
@@ -124,7 +124,7 @@ public function getUri(): string
124124 *
125125 * @since n.e.x.t
126126 *
127- * @return array<string, string| list<string>> The headers.
127+ * @return array<string, list<string>> The headers.
128128 */
129129 public function getHeaders (): array
130130 {
@@ -155,7 +155,7 @@ public function getBody(): ?string
155155 public function withHeader (string $ name , $ value ): self
156156 {
157157 $ headers = $ this ->headers ;
158- $ headers [$ name ] = $ value ;
158+ $ headers [$ name ] = is_array ( $ value) ? array_values ( $ value ) : [ $ value ] ;
159159
160160 return new self ($ this ->method , $ this ->uri , $ headers , $ this ->body );
161161 }
@@ -173,6 +173,23 @@ public function withBody(string $body): self
173173 return new self ($ this ->method , $ this ->uri , $ this ->headers , $ body );
174174 }
175175
176+ /**
177+ * Normalizes headers to ensure they are all arrays.
178+ *
179+ * @since n.e.x.t
180+ *
181+ * @param array<string, string|list<string>> $headers The headers to normalize.
182+ * @return array<string, list<string>> The normalized headers.
183+ */
184+ private function normalizeHeaders (array $ headers ): array
185+ {
186+ $ normalized = [];
187+ foreach ($ headers as $ name => $ value ) {
188+ $ normalized [$ name ] = is_array ($ value ) ? array_values ($ value ) : [$ value ];
189+ }
190+ return $ normalized ;
191+ }
192+
176193 /**
177194 * Gets the request data as an array.
178195 *
@@ -220,13 +237,8 @@ public static function getJsonSchema(): array
220237 self ::KEY_HEADERS => [
221238 'type ' => 'object ' ,
222239 'additionalProperties ' => [
223- 'oneOf ' => [
224- ['type ' => 'string ' ],
225- [
226- 'type ' => 'array ' ,
227- 'items ' => ['type ' => 'string ' ],
228- ],
229- ],
240+ 'type ' => 'array ' ,
241+ 'items ' => ['type ' => 'string ' ],
230242 ],
231243 'description ' => 'The request headers. ' ,
232244 ],
@@ -273,7 +285,7 @@ public static function fromArray(array $array): self
273285 return new self (
274286 $ array [self ::KEY_METHOD ],
275287 $ array [self ::KEY_URI ],
276- $ array [self ::KEY_HEADERS ],
288+ $ array [self ::KEY_HEADERS ] ?? [] ,
277289 $ array [self ::KEY_BODY ] ?? null
278290 );
279291 }
0 commit comments