| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\GuzzleHttp\Psr7; |
| 5 |
|
| 6 |
use InvalidArgumentException; |
| 7 |
use WCPOS\Vendor\Psr\Http\Message\UploadedFileInterface; |
| 8 |
/** |
| 9 |
* @internal |
| 10 |
* |
| 11 |
* @phpstan-type UploadedFileTree array<array-key, UploadedFileInterface|array> |
| 12 |
*/ |
| 13 |
final class UploadedFileNormalizer |
| 14 |
{ |
| 15 |
private function __construct() |
| 16 |
{ |
| 17 |
} |
| 18 |
/** |
| 19 |
* Return an UploadedFile instance array. |
| 20 |
* |
| 21 |
* @param array $files An array which respect $_FILES structure |
| 22 |
* |
| 23 |
* @return UploadedFileTree |
| 24 |
* |
| 25 |
* @throws InvalidArgumentException for unrecognized values |
| 26 |
*/ |
| 27 |
public static function normalize(array $files) : array |
| 28 |
{ |
| 29 |
$normalized = []; |
| 30 |
foreach ($files as $key => $value) { |
| 31 |
if ($value instanceof UploadedFileInterface) { |
| 32 |
$normalized[$key] = $value; |
| 33 |
} elseif (\is_array($value) && \array_key_exists('tmp_name', $value)) { |
| 34 |
$normalized[$key] = self::createUploadedFileFromSpec($value); |
| 35 |
} elseif (\is_array($value)) { |
| 36 |
$normalized[$key] = self::normalize($value); |
| 37 |
continue; |
| 38 |
} else { |
| 39 |
throw new InvalidArgumentException('Invalid value in files specification'); |
| 40 |
} |
| 41 |
} |
| 42 |
return $normalized; |
| 43 |
} |
| 44 |
/** |
| 45 |
* Create and return an UploadedFile instance from a $_FILES specification. |
| 46 |
* |
| 47 |
* If the specification represents an array of values, this method will |
| 48 |
* delegate to normalizeNestedFileSpec() and return that return value. |
| 49 |
* |
| 50 |
* @param array $value $_FILES struct |
| 51 |
* |
| 52 |
* @return UploadedFileInterface|UploadedFileTree |
| 53 |
*/ |
| 54 |
private static function createUploadedFileFromSpec(array $value) |
| 55 |
{ |
| 56 |
self::assertFileSpec($value); |
| 57 |
if (\is_array($value['tmp_name'])) { |
| 58 |
return self::normalizeNestedFileSpec($value); |
| 59 |
} |
| 60 |
return new UploadedFile($value['tmp_name'], Integers::assertNonNegativeInteger($value['size'], 'Uploaded file size'), Integers::assertNonNegativeInteger($value['error'], 'Uploaded file error'), $value['name'] ?? null, $value['type'] ?? null); |
| 61 |
} |
| 62 |
private static function assertFileSpec(array $value) : void |
| 63 |
{ |
| 64 |
if (!isset($value['tmp_name'], $value['size'], $value['error'])) { |
| 65 |
throw new InvalidArgumentException('Invalid file specification; expected keys "tmp_name", "size", and "error".'); |
| 66 |
} |
| 67 |
} |
| 68 |
/** |
| 69 |
* Normalize an array of file specifications. |
| 70 |
* |
| 71 |
* Loops through all nested files and returns a normalized array of |
| 72 |
* UploadedFileInterface instances. |
| 73 |
* |
| 74 |
* @return UploadedFileTree |
| 75 |
*/ |
| 76 |
private static function normalizeNestedFileSpec(array $files = []) : array |
| 77 |
{ |
| 78 |
self::assertNestedFileSpec($files); |
| 79 |
$normalizedFiles = []; |
| 80 |
foreach (\array_keys($files['tmp_name']) as $key) { |
| 81 |
if (!\array_key_exists($key, $files['size']) || !\array_key_exists($key, $files['error'])) { |
| 82 |
throw new InvalidArgumentException('Invalid nested file specification; expected "tmp_name", "size", and "error" arrays to have matching keys.'); |
| 83 |
} |
| 84 |
$spec = ['tmp_name' => $files['tmp_name'][$key], 'size' => $files['size'][$key], 'error' => $files['error'][$key], 'name' => $files['name'][$key] ?? null, 'type' => $files['type'][$key] ?? null]; |
| 85 |
$normalizedFiles[$key] = self::createUploadedFileFromSpec($spec); |
| 86 |
} |
| 87 |
return $normalizedFiles; |
| 88 |
} |
| 89 |
private static function assertNestedFileSpec(array $files) : void |
| 90 |
{ |
| 91 |
foreach (['tmp_name', 'size', 'error'] as $key) { |
| 92 |
if (!isset($files[$key]) || !\is_array($files[$key])) { |
| 93 |
throw new InvalidArgumentException('Invalid nested file specification; expected keys "tmp_name", "size", and "error" to be arrays.'); |
| 94 |
} |
| 95 |
} |
| 96 |
foreach (['name', 'type'] as $key) { |
| 97 |
if (isset($files[$key]) && !\is_array($files[$key])) { |
| 98 |
throw new InvalidArgumentException(\sprintf('Invalid nested file specification; expected key "%s" to be an array when present.', $key)); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|