| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\Framework\Request; |
| 4 |
|
| 5 |
trait FileHandler |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Prepares HTTP files for Request |
| 9 |
* |
| 10 |
* @param array $files |
| 11 |
* |
| 12 |
* @return array |
| 13 |
*/ |
| 14 |
public function prepareFiles($files = []) |
| 15 |
{ |
| 16 |
foreach ($files as $key => &$file) { |
| 17 |
$file = $this->convertFileInformation($file); |
| 18 |
} |
| 19 |
|
| 20 |
return $files; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* @taken from \Symfony\Component\HttpFoundation\FileBag |
| 25 |
* |
| 26 |
* Converts uploaded files to UploadedFile instances. |
| 27 |
* |
| 28 |
* @param array|File $file A (multi-dimensional) array of uploaded file information |
| 29 |
* |
| 30 |
* @return File[]|File|null A (multi-dimensional) array of File instances |
| 31 |
*/ |
| 32 |
protected function convertFileInformation($file) |
| 33 |
{ |
| 34 |
$fileKeys = array('error', 'name', 'size', 'tmp_name', 'type'); |
| 35 |
|
| 36 |
if ($file instanceof File) { |
| 37 |
return $file; |
| 38 |
} |
| 39 |
|
| 40 |
$file = $this->fixPhpFilesArray($file); |
| 41 |
|
| 42 |
if (is_array($file)) { |
| 43 |
$keys = array_keys($file); |
| 44 |
sort($keys); |
| 45 |
|
| 46 |
if ($keys == $fileKeys) { |
| 47 |
if (UPLOAD_ERR_NO_FILE == $file['error']) { |
| 48 |
$file = null; |
| 49 |
} else { |
| 50 |
$file = new File($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error']); |
| 51 |
} |
| 52 |
} else { |
| 53 |
$file = array_map(array($this, 'convertFileInformation'), $file); |
| 54 |
if (array_keys($keys) === $keys) { |
| 55 |
$file = array_filter($file); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
return $file; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @taken from \Symfony\Component\HttpFoundation\FileBag |
| 65 |
* |
| 66 |
* Fixes a malformed PHP $_FILES array. |
| 67 |
* |
| 68 |
* PHP has a bug that the format of the $_FILES array differs, depending on |
| 69 |
* whether the uploaded file fields had normal field names or array-like |
| 70 |
* field names ("normal" vs. "parent[child]"). |
| 71 |
* |
| 72 |
* This method fixes the array to look like the "normal" $_FILES array. |
| 73 |
* |
| 74 |
* It's safe to pass an already converted array, in which case this method |
| 75 |
* just returns the original array unmodified. |
| 76 |
* |
| 77 |
* @return array |
| 78 |
*/ |
| 79 |
protected function fixPhpFilesArray($data) |
| 80 |
{ |
| 81 |
$fileKeys = array('error', 'name', 'size', 'tmp_name', 'type'); |
| 82 |
|
| 83 |
if (! is_array($data)) { |
| 84 |
return $data; |
| 85 |
} |
| 86 |
|
| 87 |
$keys = array_keys($data); |
| 88 |
sort($keys); |
| 89 |
|
| 90 |
if ($fileKeys != $keys || ! isset($data['name']) || ! is_array($data['name'])) { |
| 91 |
return $data; |
| 92 |
} |
| 93 |
|
| 94 |
$files = $data; |
| 95 |
foreach ($fileKeys as $k) { |
| 96 |
unset($files[$k]); |
| 97 |
} |
| 98 |
|
| 99 |
foreach ($data['name'] as $key => $name) { |
| 100 |
$files[$key] = $this->fixPhpFilesArray(array( |
| 101 |
'error' => $data['error'][$key], |
| 102 |
'name' => $name, |
| 103 |
'type' => $data['type'][$key], |
| 104 |
'tmp_name' => $data['tmp_name'][$key], |
| 105 |
'size' => $data['size'][$key], |
| 106 |
)); |
| 107 |
} |
| 108 |
|
| 109 |
return $files; |
| 110 |
} |
| 111 |
} |