fluent-boards
/
vendor
/
wpfluent
/
framework
/
src
/
WPFluent
/
Http
/
Request
/
InteractsWithFilesTrait.php
InteractsWithFilesTrait.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.40, at vendor/wpfluent/framework/src/WPFluent/Http/Request/InteractsWithFilesTrait.php
| 1 | <?php |
| 2 | |
| 3 | namespace FluentBoards\Framework\Http\Request; |
| 4 | |
| 5 | use FluentBoards\Framework\Support\Helper; |
| 6 | |
| 7 | trait InteractsWithFilesTrait |
| 8 | { |
| 9 | /** |
| 10 | * Prepares HTTP files for Request |
| 11 | * |
| 12 | * @param array $files |
| 13 | * |
| 14 | * @return array |
| 15 | */ |
| 16 | public function prepareFiles($files = []) |
| 17 | { |
| 18 | foreach ($files as $key => &$file) { |
| 19 | $file = $this->convertFileInformation($file); |
| 20 | } |
| 21 | |
| 22 | return $files; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @taken from \Symfony\Component\HttpFoundation\FileBag |
| 27 | * |
| 28 | * Converts uploaded files to UploadedFile instances. |
| 29 | * |
| 30 | * @param array|File $file A (multi-dimensional) array of uploaded file information |
| 31 | * |
| 32 | * @return File[]|File|null A (multi-dimensional) array of File instances |
| 33 | */ |
| 34 | protected function convertFileInformation($file) |
| 35 | { |
| 36 | $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type'); |
| 37 | |
| 38 | if ($file instanceof File) { |
| 39 | return $file; |
| 40 | } |
| 41 | |
| 42 | $file = $this->fixPhpFilesArray($file); |
| 43 | |
| 44 | if (is_array($file)) { |
| 45 | $keys = array_keys($file); |
| 46 | sort($keys); |
| 47 | |
| 48 | if ($keys == $fileKeys) { |
| 49 | if (UPLOAD_ERR_NO_FILE == $file['error']) { |
| 50 | $file = null; |
| 51 | } else { |
| 52 | $file = new File( |
| 53 | $file['tmp_name'], |
| 54 | $file['name'], |
| 55 | $file['type'], |
| 56 | $file['size'], |
| 57 | $file['error'] |
| 58 | ); |
| 59 | } |
| 60 | } else { |
| 61 | $file = array_map(array($this, 'convertFileInformation'), $file); |
| 62 | if (array_keys($keys) === $keys) { |
| 63 | $file = array_filter($file); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return $file; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @taken from \Symfony\Component\HttpFoundation\FileBag |
| 73 | * |
| 74 | * Fixes a malformed PHP $_FILES array. |
| 75 | * |
| 76 | * PHP has a bug that the format of the $_FILES array differs, depending on |
| 77 | * whether the uploaded file fields had normal field names or array-like |
| 78 | * field names ("normal" vs. "parent[child]"). |
| 79 | * |
| 80 | * This method fixes the array to look like the "normal" $_FILES array. |
| 81 | * |
| 82 | * It's safe to pass an already converted array, in which case this method |
| 83 | * just returns the original array unmodified. |
| 84 | * |
| 85 | * @return array |
| 86 | */ |
| 87 | protected function fixPhpFilesArray($data) |
| 88 | { |
| 89 | $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type'); |
| 90 | |
| 91 | if (!is_array($data)) { |
| 92 | return $data; |
| 93 | } |
| 94 | |
| 95 | // Remove extra key added by PHP 8.1. |
| 96 | unset($data['full_path']); |
| 97 | $keys = array_keys($data); |
| 98 | sort($keys); |
| 99 | |
| 100 | if ( |
| 101 | $fileKeys != $keys || |
| 102 | !isset($data['name']) || |
| 103 | !is_array($data['name']) |
| 104 | ) { |
| 105 | return $data; |
| 106 | } |
| 107 | |
| 108 | $files = $data; |
| 109 | foreach ($fileKeys as $k) { |
| 110 | unset($files[$k]); |
| 111 | } |
| 112 | |
| 113 | foreach ($data['name'] as $key => $name) { |
| 114 | $files[$key] = $this->fixPhpFilesArray(array( |
| 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 | )); |
| 121 | } |
| 122 | |
| 123 | return $files; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Retrieve a file from the request. |
| 128 | * |
| 129 | * @param string|null $key |
| 130 | * @param mixed $default |
| 131 | * @return \FluentBoards\Framework\Request\File|array|null |
| 132 | */ |
| 133 | public function file($key = null, $default = null) |
| 134 | { |
| 135 | return Helper::dataGet($this->files(), $key, $default); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Get the files array from the request. |
| 140 | * |
| 141 | * @return array |
| 142 | */ |
| 143 | public function files($asCollection = false) |
| 144 | { |
| 145 | return $asCollection ? Helper::collect( |
| 146 | $this->files |
| 147 | ) : $this->files; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Get the files as collection from the request. |
| 152 | * |
| 153 | * @return array |
| 154 | */ |
| 155 | public function fileCollection() |
| 156 | { |
| 157 | return $this->files(true); |
| 158 | } |
| 159 | } |
| 160 |