| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpFoundation; |
| 13 |
|
| 14 |
use Symfony\Component\HttpFoundation\File\UploadedFile; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class FileBag extends ParameterBag |
| 23 |
{ |
| 24 |
private const FILE_KEYS = ['error', 'full_path', 'name', 'size', 'tmp_name', 'type']; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public function __construct(array $parameters = []) |
| 30 |
{ |
| 31 |
$this->replace($parameters); |
| 32 |
} |
| 33 |
|
| 34 |
public function replace(array $files = []): void |
| 35 |
{ |
| 36 |
$this->parameters = []; |
| 37 |
$this->add($files); |
| 38 |
} |
| 39 |
|
| 40 |
public function set(string $key, mixed $value): void |
| 41 |
{ |
| 42 |
if (!\is_array($value) && !$value instanceof UploadedFile) { |
| 43 |
throw new \InvalidArgumentException('An uploaded file must be an array or an instance of UploadedFile.'); |
| 44 |
} |
| 45 |
|
| 46 |
parent::set($key, $this->convertFileInformation($value)); |
| 47 |
} |
| 48 |
|
| 49 |
public function add(array $files = []): void |
| 50 |
{ |
| 51 |
foreach ($files as $key => $file) { |
| 52 |
$this->set($key, $file); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
protected function convertFileInformation(array|UploadedFile $file): array|UploadedFile|null |
| 62 |
{ |
| 63 |
if ($file instanceof UploadedFile) { |
| 64 |
return $file; |
| 65 |
} |
| 66 |
|
| 67 |
$file = $this->fixPhpFilesArray($file); |
| 68 |
$keys = array_keys($file + ['full_path' => null]); |
| 69 |
sort($keys); |
| 70 |
|
| 71 |
if (self::FILE_KEYS === $keys) { |
| 72 |
if (\UPLOAD_ERR_NO_FILE === $file['error']) { |
| 73 |
$file = null; |
| 74 |
} else { |
| 75 |
$file = new UploadedFile($file['tmp_name'], $file['full_path'] ?? $file['name'], $file['type'], $file['error'], false); |
| 76 |
} |
| 77 |
} else { |
| 78 |
$file = array_map(fn ($v) => $v instanceof UploadedFile || \is_array($v) ? $this->convertFileInformation($v) : $v, $file); |
| 79 |
if (array_is_list($file)) { |
| 80 |
$file = array_filter($file); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return $file; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
protected function fixPhpFilesArray(array $data): array |
| 100 |
{ |
| 101 |
$keys = array_keys($data + ['full_path' => null]); |
| 102 |
sort($keys); |
| 103 |
|
| 104 |
if (self::FILE_KEYS !== $keys || !isset($data['name']) || !\is_array($data['name'])) { |
| 105 |
return $data; |
| 106 |
} |
| 107 |
|
| 108 |
$files = $data; |
| 109 |
foreach (self::FILE_KEYS as $k) { |
| 110 |
unset($files[$k]); |
| 111 |
} |
| 112 |
|
| 113 |
foreach ($data['name'] as $key => $name) { |
| 114 |
$files[$key] = $this->fixPhpFilesArray([ |
| 115 |
'error' => $data['error'][$key], |
| 116 |
'name' => $name, |
| 117 |
'type' => $data['type'][$key], |
| 118 |
'tmp_name' => $data['tmp_name'][$key], |
| 119 |
'size' => $data['size'][$key], |
| 120 |
] + (isset($data['full_path'][$key]) ? [ |
| 121 |
'full_path' => $data['full_path'][$key], |
| 122 |
] : [])); |
| 123 |
} |
| 124 |
|
| 125 |
return $files; |
| 126 |
} |
| 127 |
} |