19
19
* @phpstan-type RequestArrayShape array{
20
20
* method: string,
21
21
* uri: string,
22
- * headers: array<string, string| list<string>>,
22
+ * headers: array<string, list<string>>,
23
23
* body?: string|null
24
24
* }
25
25
*
@@ -43,7 +43,7 @@ class Request extends AbstractDataTransferObject
43
43
protected string $ uri ;
44
44
45
45
/**
46
- * @var array<string, string| list<string>> The request headers.
46
+ * @var array<string, list<string>> The request headers.
47
47
*/
48
48
protected array $ headers ;
49
49
@@ -79,7 +79,7 @@ public function __construct($method, string $uri, array $headers = [], ?string $
79
79
}
80
80
81
81
$ this ->uri = $ uri ;
82
- $ this ->headers = $ headers ;
82
+ $ this ->headers = $ this -> normalizeHeaders ( $ headers) ;
83
83
$ this ->body = $ body ;
84
84
}
85
85
@@ -124,7 +124,7 @@ public function getUri(): string
124
124
*
125
125
* @since n.e.x.t
126
126
*
127
- * @return array<string, string| list<string>> The headers.
127
+ * @return array<string, list<string>> The headers.
128
128
*/
129
129
public function getHeaders (): array
130
130
{
@@ -155,7 +155,7 @@ public function getBody(): ?string
155
155
public function withHeader (string $ name , $ value ): self
156
156
{
157
157
$ headers = $ this ->headers ;
158
- $ headers [$ name ] = $ value ;
158
+ $ headers [$ name ] = is_array ( $ value) ? array_values ( $ value ) : [ $ value ] ;
159
159
160
160
return new self ($ this ->method , $ this ->uri , $ headers , $ this ->body );
161
161
}
@@ -173,6 +173,23 @@ public function withBody(string $body): self
173
173
return new self ($ this ->method , $ this ->uri , $ this ->headers , $ body );
174
174
}
175
175
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
+
176
193
/**
177
194
* Gets the request data as an array.
178
195
*
@@ -220,13 +237,8 @@ public static function getJsonSchema(): array
220
237
self ::KEY_HEADERS => [
221
238
'type ' => 'object ' ,
222
239
'additionalProperties ' => [
223
- 'oneOf ' => [
224
- ['type ' => 'string ' ],
225
- [
226
- 'type ' => 'array ' ,
227
- 'items ' => ['type ' => 'string ' ],
228
- ],
229
- ],
240
+ 'type ' => 'array ' ,
241
+ 'items ' => ['type ' => 'string ' ],
230
242
],
231
243
'description ' => 'The request headers. ' ,
232
244
],
@@ -273,7 +285,7 @@ public static function fromArray(array $array): self
273
285
return new self (
274
286
$ array [self ::KEY_METHOD ],
275
287
$ array [self ::KEY_URI ],
276
- $ array [self ::KEY_HEADERS ],
288
+ $ array [self ::KEY_HEADERS ] ?? [] ,
277
289
$ array [self ::KEY_BODY ] ?? null
278
290
);
279
291
}
0 commit comments