@@ -184,15 +184,15 @@ private function buildServerRequestFromGlobals(ServerRequestInterface $request,
184
184
->withUploadedFiles ($ this ->normalizeFiles ($ files ));
185
185
186
186
$ headers = [];
187
- foreach ($ server as $ key => $ value ) {
188
- if (0 === strpos ($ key , 'HTTP_ ' )) {
189
- $ key = substr ($ key , 5 );
190
- } elseif (!\in_array ($ key , ['CONTENT_TYPE ' , 'CONTENT_LENGTH ' , 'CONTENT_MD5 ' ], true )) {
187
+ foreach ($ server as $ k => $ v ) {
188
+ if (0 === strpos ($ k , 'HTTP_ ' )) {
189
+ $ k = substr ($ k , 5 );
190
+ } elseif (!\in_array ($ k , ['CONTENT_TYPE ' , 'CONTENT_LENGTH ' , 'CONTENT_MD5 ' ], true )) {
191
191
continue ;
192
192
}
193
- $ key = str_replace (' ' , '- ' , ucwords (strtolower (str_replace ('_ ' , ' ' , $ key ))));
193
+ $ k = str_replace (' ' , '- ' , ucwords (strtolower (str_replace ('_ ' , ' ' , $ k ))));
194
194
195
- $ headers [$ key ] = $ value ;
195
+ $ headers [$ k ] = $ v ;
196
196
}
197
197
198
198
if (!isset ($ headers ['Authorization ' ])) {
@@ -205,9 +205,9 @@ private function buildServerRequestFromGlobals(ServerRequestInterface $request,
205
205
}
206
206
}
207
207
208
- foreach ($ headers as $ key => $ value ) {
208
+ foreach ($ headers as $ k => $ v ) {
209
209
try {
210
- $ request = $ request ->withHeader ($ key , $ value );
210
+ $ request = $ request ->withHeader ($ k , $ v );
211
211
} catch (\InvalidArgumentException $ e ) {
212
212
// ignore invalid headers
213
213
}
@@ -257,26 +257,47 @@ private function buildUriFromGlobals(UriInterface $uri, array $server): UriInter
257
257
258
258
private function normalizeFiles (array $ files ): array
259
259
{
260
- $ normalized = [];
261
-
262
- foreach ($ files as $ key => $ value ) {
263
- if ($ value instanceof UploadedFileInterface) {
264
- $ normalized [$ key ] = $ value ;
265
- } elseif (!\is_array ($ value )) {
260
+ foreach ($ files as $ k => $ v ) {
261
+ if ($ v instanceof UploadedFileInterface) {
266
262
continue ;
267
- } elseif (!isset ($ value ['tmp_name ' ])) {
268
- $ normalized [$ key ] = $ this ->normalizeFiles ($ value );
269
- } elseif (\is_array ($ value ['tmp_name ' ])) {
270
- foreach ($ value ['tmp_name ' ] as $ k => $ v ) {
271
- $ file = $ this ->createStreamFromFile ($ value ['tmp_name ' ][$ k ], 'r ' );
272
- $ normalized [$ key ][$ k ] = $ this ->createUploadedFile ($ file , $ value ['size ' ][$ k ], $ value ['error ' ][$ k ], $ value ['name ' ][$ k ], $ value ['type ' ][$ k ]);
273
- }
263
+ }
264
+ if (!\is_array ($ v )) {
265
+ unset($ files [$ k ]);
266
+ } elseif (!isset ($ v ['tmp_name ' ])) {
267
+ $ files [$ k ] = $ this ->normalizeFiles ($ v );
274
268
} else {
275
- $ file = $ this ->createStreamFromFile ($ value ['tmp_name ' ], 'r ' );
276
- $ normalized [$ key ] = $ this ->createUploadedFile ($ file , $ value ['size ' ], $ value ['error ' ], $ value ['name ' ], $ value ['type ' ]);
269
+ $ files [$ k ] = $ this ->createUploadedFileFromSpec ($ v );
277
270
}
278
271
}
279
272
280
- return $ normalized ;
273
+ return $ files ;
274
+ }
275
+
276
+ /**
277
+ * Create and return an UploadedFile instance from a $_FILES specification.
278
+ *
279
+ * @param array $value $_FILES struct
280
+ *
281
+ * @return UploadedFileInterface|UploadedFileInterface[]
282
+ */
283
+ private function createUploadedFileFromSpec (array $ value )
284
+ {
285
+ if (!is_array ($ tmpName = $ value ['tmp_name ' ])) {
286
+ $ file = $ this ->createStreamFromFile ($ tmpName , 'r ' );
287
+
288
+ return $ this ->createUploadedFile ($ file , $ value ['size ' ], $ value ['error ' ], $ value ['name ' ], $ value ['type ' ]);
289
+ }
290
+
291
+ foreach ($ tmpName as $ k => $ v ) {
292
+ $ tmpName [$ k ] = $ this ->createUploadedFileFromSpec ([
293
+ 'tmp_name ' => $ v ,
294
+ 'size ' => $ value ['size ' ][$ k ] ?? null ,
295
+ 'error ' => $ value ['error ' ][$ k ] ?? null ,
296
+ 'name ' => $ value ['name ' ][$ k ] ?? null ,
297
+ 'type ' => $ value ['type ' ][$ k ] ?? null ,
298
+ ]);
299
+ }
300
+
301
+ return $ tmpName ;
281
302
}
282
303
}
0 commit comments