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.91.6, 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 | use FluentBoards\Framework\Support\Collection; |
| 7 | |
| 8 | trait InteractsWithFilesTrait |
| 9 | { |
| 10 | /** |
| 11 | * Prepares HTTP files for Request |
| 12 | * |
| 13 | * @param array $files |
| 14 | * |
| 15 | * @return array |
| 16 | */ |
| 17 | public function prepareFiles($files = []) |
| 18 | { |
| 19 | foreach ($files as $key => &$file) { |
| 20 | $file = $this->convertFileInformation($file); |
| 21 | } |
| 22 | |
| 23 | return $files; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @taken from \Symfony\Component\HttpFoundation\FileBag |
| 28 | * |
| 29 | * Converts uploaded files to UploadedFile instances. |
| 30 | * |
| 31 | * @param array|File $file A (multi-dimensional) array of uploaded file information |
| 32 | * |
| 33 | * @return File[]|File|null A (multi-dimensional) array of File instances |
| 34 | */ |
| 35 | protected function convertFileInformation($file) |
| 36 | { |
| 37 | $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type'); |
| 38 | |
| 39 | if ($file instanceof File) { |
| 40 | return $file; |
| 41 | } |
| 42 | |
| 43 | $file = $this->fixPhpFilesArray($file); |
| 44 | |
| 45 | if (is_array($file)) { |
| 46 | $keys = array_keys($file); |
| 47 | sort($keys); |
| 48 | |
| 49 | if ($keys == $fileKeys) { |
| 50 | if (UPLOAD_ERR_NO_FILE == $file['error']) { |
| 51 | $file = null; |
| 52 | } else { |
| 53 | $file = new File( |
| 54 | $file['tmp_name'], |
| 55 | $file['name'], |
| 56 | $file['type'], |
| 57 | $file['size'], |
| 58 | $file['error'] |
| 59 | ); |
| 60 | } |
| 61 | } else { |
| 62 | $file = array_map(array($this, 'convertFileInformation'), $file); |
| 63 | if (array_keys($keys) === $keys) { |
| 64 | $file = array_filter($file); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return $file; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @taken from \Symfony\Component\HttpFoundation\FileBag |
| 74 | * |
| 75 | * Fixes a malformed PHP $_FILES array. |
| 76 | * |
| 77 | * PHP has a bug that the format of the $_FILES array differs, depending on |
| 78 | * whether the uploaded file fields had normal field names or array-like |
| 79 | * field names ("normal" vs. "parent[child]"). |
| 80 | * |
| 81 | * This method fixes the array to look like the "normal" $_FILES array. |
| 82 | * |
| 83 | * It's safe to pass an already converted array, in which case this method |
| 84 | * just returns the original array unmodified. |
| 85 | * |
| 86 | * @return array |
| 87 | */ |
| 88 | protected function fixPhpFilesArray($data) |
| 89 | { |
| 90 | $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type'); |
| 91 | |
| 92 | if (!is_array($data)) { |
| 93 | return $data; |
| 94 | } |
| 95 | |
| 96 | // Remove extra key added by PHP 8.1. |
| 97 | unset($data['full_path']); |
| 98 | $keys = array_keys($data); |
| 99 | sort($keys); |
| 100 | |
| 101 | if ( |
| 102 | $fileKeys != $keys || |
| 103 | !isset($data['name']) || |
| 104 | !is_array($data['name']) |
| 105 | ) { |
| 106 | return $data; |
| 107 | } |
| 108 | |
| 109 | $files = $data; |
| 110 | foreach ($fileKeys as $k) { |
| 111 | unset($files[$k]); |
| 112 | } |
| 113 | |
| 114 | foreach ($data['name'] as $key => $name) { |
| 115 | $files[$key] = $this->fixPhpFilesArray(array( |
| 116 | 'error' => $data['error'][$key], |
| 117 | 'name' => $name, |
| 118 | 'type' => $data['type'][$key], |
| 119 | 'tmp_name' => $data['tmp_name'][$key], |
| 120 | 'size' => $data['size'][$key], |
| 121 | )); |
| 122 | } |
| 123 | |
| 124 | return $files; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Retrieve a file from the request. |
| 129 | * |
| 130 | * @param string|null $key |
| 131 | * @param mixed $default |
| 132 | * @return \FluentBoards\Framework\Request\File|array|null |
| 133 | */ |
| 134 | public function file($key = null, $default = null) |
| 135 | { |
| 136 | return Helper::dataGet($this->files(), $key, $default); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get the files array from the request. |
| 141 | * |
| 142 | * @return array |
| 143 | */ |
| 144 | public function files($key = null) |
| 145 | { |
| 146 | return Helper::dataGet($this->files, $key, $this->files); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Get the files as collection from the request. |
| 151 | * |
| 152 | * @return array |
| 153 | */ |
| 154 | public function fileCollection($key = null) |
| 155 | { |
| 156 | $collection = Helper::collect( |
| 157 | Helper::dataGet($this->files, $key, $this->files) |
| 158 | ); |
| 159 | |
| 160 | if (!method_exists($collection, 'save')) { |
| 161 | $this->addSaveMethod($collection); |
| 162 | } |
| 163 | |
| 164 | return $collection; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Ad a save method on the runtime. |
| 169 | * |
| 170 | * @param Collection &$files |
| 171 | * @return void |
| 172 | */ |
| 173 | protected function addSaveMethod(Collection &$files) |
| 174 | { |
| 175 | Collection::macro('save', function ($path = null) use ($files) { |
| 176 | return $files->map(function ($file) use ($path) { |
| 177 | // When developer will use the method without key name: |
| 178 | // i.e: $request->fileCollection(), an array of arrays |
| 179 | // ['images' => [0 => File, 1 => File]] will be |
| 180 | // returned, so $file will contain an array |
| 181 | // of File objects, [0 => File, 1 => File]. |
| 182 | if (is_array($file)) { |
| 183 | $savedFiles = []; |
| 184 | foreach ($file as $fileObject) { |
| 185 | $savedFiles[] = $fileObject->save($path); |
| 186 | } |
| 187 | return $savedFiles; |
| 188 | } |
| 189 | |
| 190 | // For $request->fileCollection('images') |
| 191 | // [0 => File, 1 => File] will be returned. |
| 192 | return $file->save($path); |
| 193 | })->all(); |
| 194 | }); |
| 195 | } |
| 196 | } |
| 197 |