Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit f311faf

Browse files
committed
Incorporates normalizeUploadedFileSpecification() into normalizeUploadedFiles()
There's no need for `normalizeUploadedFileSpecification()` to be exposed publicly, as it is in internal detail of `normalizeUploadedFiles()`.
1 parent b98584b commit f311faf

File tree

3 files changed

+43
-53
lines changed

3 files changed

+43
-53
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
"src/functions/marshal_uri_from_sapi.php",
4545
"src/functions/normalize_server.php",
4646
"src/functions/normalize_uploaded_files.php",
47-
"src/functions/normalize_uploaded_file_specification.php",
4847
"src/functions/parse_cookie_header.php"
4948
],
5049
"psr-4": {

src/functions/normalize_uploaded_file_specification.php

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/functions/normalize_uploaded_files.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,48 @@
2222
*/
2323
function normalizeUploadedFiles(array $files)
2424
{
25+
/**
26+
* Normalize an array of file specifications.
27+
*
28+
* Loops through all nested files (as determined by receiving an array to the
29+
* `tmp_name` key of a `$_FILES` specification) and returns a normalized array
30+
* of UploadedFile instances.
31+
*
32+
* This function normalizes a `$_FILES` array representing a nested set of
33+
* uploaded files as produced by the php-fpm SAPI, CGI SAPI, or mod_php
34+
* SAPI.
35+
*
36+
* @param array $files
37+
* @return UploadedFile[]
38+
*/
39+
$normalizeUploadedFileSpecification = function (array $files = []) {
40+
if (! isset($files['tmp_name']) || ! is_array($files['tmp_name'])
41+
|| ! isset($files['size']) || ! is_array($files['size'])
42+
|| ! isset($files['error']) || ! is_array($files['error'])
43+
) {
44+
throw new InvalidArgumentException(sprintf(
45+
'$files provided to %s MUST contain each of the keys "tmp_name",'
46+
. ' "size", and "error", with each represented as an array;'
47+
. ' one or more were missing or non-array values',
48+
__FUNCTION__
49+
));
50+
}
51+
52+
53+
$normalized = [];
54+
foreach (array_keys($files['tmp_name']) as $key) {
55+
$spec = [
56+
'tmp_name' => $files['tmp_name'][$key],
57+
'size' => $files['size'][$key],
58+
'error' => $files['error'][$key],
59+
'name' => isset($files['name'][$key]) ? $files['name'][$key] : null,
60+
'type' => isset($files['type'][$key]) ? $files['type'][$key] : null,
61+
];
62+
$normalized[$key] = createUploadedFile($spec);
63+
}
64+
return $normalized;
65+
};
66+
2567
$normalized = [];
2668
foreach ($files as $key => $value) {
2769
if ($value instanceof UploadedFileInterface) {
@@ -30,7 +72,7 @@ function normalizeUploadedFiles(array $files)
3072
}
3173

3274
if (is_array($value) && isset($value['tmp_name']) && is_array($value['tmp_name'])) {
33-
$normalized[$key] = normalizeUploadedFileSpecification($value);
75+
$normalized[$key] = $normalizeUploadedFileSpecification($value);
3476
continue;
3577
}
3678

0 commit comments

Comments
 (0)