7
7
use InvalidArgumentException ;
8
8
use JsonException ;
9
9
use WordPress \AiClient \Common \AbstractDataTransferObject ;
10
+ use WordPress \AiClient \Providers \Http \Collections \HeadersCollection ;
10
11
use WordPress \AiClient \Providers \Http \Enums \HttpMethodEnum ;
11
12
12
13
/**
@@ -44,14 +45,9 @@ class Request extends AbstractDataTransferObject
44
45
protected string $ uri ;
45
46
46
47
/**
47
- * @var array<string, list<string>> The request headers.
48
+ * @var HeadersCollection The request headers.
48
49
*/
49
- protected array $ headers ;
50
-
51
- /**
52
- * @var array<string, string> Map of lowercase header names to actual header names for fast lookup.
53
- */
54
- protected array $ headersMap ;
50
+ protected HeadersCollection $ headers ;
55
51
56
52
/**
57
53
* @var string|array<string, mixed>|null The request data.
@@ -78,8 +74,7 @@ public function __construct(HttpMethodEnum $method, string $uri, array $headers
78
74
79
75
$ this ->method = $ method ;
80
76
$ this ->uri = $ uri ;
81
- $ this ->headers = $ this ->normalizeHeaders ($ headers );
82
- $ this ->headersMap = $ this ->buildHeadersMap ($ this ->headers );
77
+ $ this ->headers = new HeadersCollection ($ headers );
83
78
$ this ->data = $ data ;
84
79
}
85
80
@@ -124,7 +119,7 @@ public function getUri(): string
124
119
*/
125
120
public function getHeaders (): array
126
121
{
127
- return $ this ->headers ;
122
+ return $ this ->headers -> getAll () ;
128
123
}
129
124
130
125
/**
@@ -137,11 +132,7 @@ public function getHeaders(): array
137
132
*/
138
133
public function getHeader (string $ name ): ?array
139
134
{
140
- $ lower = strtolower ($ name );
141
- if (!isset ($ this ->headersMap [$ lower ])) {
142
- return null ;
143
- }
144
- return $ this ->headers [$ this ->headersMap [$ lower ]];
135
+ return $ this ->headers ->get ($ name );
145
136
}
146
137
147
138
/**
@@ -152,10 +143,9 @@ public function getHeader(string $name): ?array
152
143
* @param string $name The header name (case-insensitive).
153
144
* @return string|null The first header value or null if not found.
154
145
*/
155
- public function getHeaderLine (string $ name ): ?string
146
+ public function getHeaderAsString (string $ name ): ?string
156
147
{
157
- $ values = $ this ->getHeader ($ name );
158
- return $ values !== null ? implode (', ' , $ values ) : null ;
148
+ return $ this ->headers ->getAsString ($ name );
159
149
}
160
150
161
151
/**
@@ -168,7 +158,7 @@ public function getHeaderLine(string $name): ?string
168
158
*/
169
159
public function hasHeader (string $ name ): bool
170
160
{
171
- return isset ( $ this ->headersMap [ strtolower ($ name)] );
161
+ return $ this ->headers -> has ($ name );
172
162
}
173
163
174
164
/**
@@ -242,10 +232,10 @@ private function getContentType(): ?string
242
232
*/
243
233
public function withHeader (string $ name , $ value ): self
244
234
{
245
- $ headers = $ this ->headers ;
246
- $ headers [ $ name ] = is_array ( $ value ) ? array_values ( $ value ) : [ $ value ] ;
247
-
248
- return new self ( $ this -> method , $ this -> uri , $ headers , $ this -> data ) ;
235
+ $ newHeaders = $ this ->headers -> withHeader ( $ name , $ value ) ;
236
+ $ new = clone $ this ;
237
+ $ new -> headers = $ newHeaders ;
238
+ return $ new ;
249
239
}
250
240
251
241
/**
@@ -258,41 +248,9 @@ public function withHeader(string $name, $value): self
258
248
*/
259
249
public function withData ($ data ): self
260
250
{
261
- return new self ($ this ->method , $ this ->uri , $ this ->headers , $ data );
262
- }
263
-
264
- /**
265
- * Normalizes headers to ensure they are all arrays.
266
- *
267
- * @since n.e.x.t
268
- *
269
- * @param array<string, string|list<string>> $headers The headers to normalize.
270
- * @return array<string, list<string>> The normalized headers.
271
- */
272
- private function normalizeHeaders (array $ headers ): array
273
- {
274
- $ normalized = [];
275
- foreach ($ headers as $ name => $ value ) {
276
- $ normalized [$ name ] = is_array ($ value ) ? array_values ($ value ) : [$ value ];
277
- }
278
- return $ normalized ;
279
- }
280
-
281
- /**
282
- * Builds a map of lowercase header names to actual header names.
283
- *
284
- * @since n.e.x.t
285
- *
286
- * @param array<string, list<string>> $headers The headers.
287
- * @return array<string, string> The headers map.
288
- */
289
- private function buildHeadersMap (array $ headers ): array
290
- {
291
- $ map = [];
292
- foreach (array_keys ($ headers ) as $ name ) {
293
- $ map [strtolower ($ name )] = $ name ;
294
- }
295
- return $ map ;
251
+ $ new = clone $ this ;
252
+ $ new ->data = $ data ;
253
+ return $ new ;
296
254
}
297
255
298
256
/**
@@ -354,7 +312,7 @@ public function toArray(): array
354
312
$ array = [
355
313
self ::KEY_METHOD => $ this ->method ->value ,
356
314
self ::KEY_URI => $ this ->uri ,
357
- self ::KEY_HEADERS => $ this ->headers ,
315
+ self ::KEY_HEADERS => $ this ->headers -> getAll () ,
358
316
];
359
317
360
318
if ($ this ->data !== null ) {
0 commit comments