# Class HttpSoft\UploadedFile\UploadedFileNormalizer

Normalizes the tree structure of downloaded files, with each leaf an instance of Psr\Http\Message\UploadedFileInterface.

Source code on GitHub.

use HttpSoft\UploadedFile\UploadedFileNormalizer;

// Example #1

print_r($_FILES);
/*
[
    'file' => [
        'name' => 'file.txt',
        'type' => 'text/plain',
        'tmp_name' => '/tmp/phpN3FmFr',
        'error' => UPLOAD_ERR_OK,
        'size' => 1024,
    ],
];
*/

print_r(UploadedFileNormalizer::normalize($_FILES));
/*
[
    'file' => `HttpSoft\UploadedFile\UploadedFile Object`,
];
*/

// Example #2

/*
[
    'file_1' => [
        'name' => 'file.txt',
        'type' => 'text/plain',
        'tmp_name' => '/tmp/phpN3FmFr',
        'error' => UPLOAD_ERR_OK,
        'size' => 1024,
    ],
    'file_2' => [
        'name' => 'image.png',
        'type' => 'image/png',
        'tmp_name' => '/tmp/phpLs7DaJ',
        'error' => UPLOAD_ERR_OK,
        'size' => 98760,
    ],
];
*/

print_r(UploadedFileNormalizer::normalize($_FILES));
/*
[
    'file_1' => `HttpSoft\UploadedFile\UploadedFile Object`,
    'file_2' => `HttpSoft\UploadedFile\UploadedFile Object`,
];
*/

// Example #3

print_r($_FILES);
/*
[
    'files' => [
        'name' => [
            'file_1' => 'file.txt',
            'file_2' => 'image.png',
        ],
        'type' => [
            'file_1' => 'text/plain',
            'file_2' => 'image/png',
        ],
        'tmp_name' => [
            'file_1' => '/tmp/phpN3FmFr',
            'file_2' => '/tmp/phpLs7DaJ',
        ],
        'error' => [
            'file_1' => 0,
            'file_2' => 0,
        ],
        'size' => [
            'file_1' => 1024,
            'file_2' => 98174,
        ],
    ],
];
*/

print_r(UploadedFileNormalizer::normalize($_FILES));
/*
[
    'files' => [
        'file_1' => `HttpSoft\UploadedFile\UploadedFile Object`,
        'file_2' => `HttpSoft\UploadedFile\UploadedFile Object`,
    ],
];
*/

// Example #4

/*
[
    'files' => [
        'name' => [
            'data' => [
                'nested' => [
                    'file_1' => 'file.txt',
                    'file_2' => 'image.png',
                ],
            ],
        ],
        'type' => [
            'data' => [
                'nested' => [
                    'file_1' => 'text/plain',
                    'file_2' => 'image/png',
                ],
            ],
        ],
        'tmp_name' => [
            'data' => [
                'nested' => [
                    'file_1' => '/tmp/phpN3FmFr',
                    'file_2' => '/tmp/phpLs7DaJ',
                ],
            ],
        ],
        'error' => [
            'data' => [
                'nested' => [
                    'file_1' => UPLOAD_ERR_OK,
                    'file_2' => UPLOAD_ERR_OK,
                ],
            ],
        ],
        'size' => [
            'data' => [
                'nested' => [
                    'file_1' => 1024,
                    'file_2' => 98174,
                ],
            ],
        ],
    ],
];
*/

print_r(UploadedFileNormalizer::normalize($_FILES));
/*
[
    'files' => [
        'data' => [
            'nested' => [
                'file_1' => `HttpSoft\UploadedFile\UploadedFile Object`,
                'file_2' => `HttpSoft\UploadedFile\UploadedFile Object`,
            ],
        ],
    ],
];
*/

# Public methods

This class has only one public static method.

# normalize

Converts each value of the multidimensional array $files to an Psr\Http\Message\UploadedFileInterface instance.

public static function normalize(array $files = []): array;

The method uses recursion, so the $files array can be of any nesting type.

All key names in the $files array will be saved.

The array structure must be the same as the global $_FILES array.

If specified invalid value file specification, the \InvalidArgumentException exception will be thrown.

For more information about the structure of uploaded files, see here.