PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.10.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.10.0
2.11.0 2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 All 78 releases
fluent-community / vendor / wpfluent / framework / src / WPFluent / Http / Request / InteractsWithFilesTrait.php

InteractsWithFilesTrait.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.10.0, at vendor/wpfluent/framework/src/WPFluent/Http/Request/InteractsWithFilesTrait.php

241 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\Framework\Http\Request;
4
5 use FluentCommunity\Framework\Http\Request\File;
6 use FluentCommunity\Framework\Support\Helper;
7 use FluentCommunity\Framework\Support\Collection;
8
9 trait InteractsWithFilesTrait
10 {
11 /**
12 * Processed $_FILES array
13 * @var array
14 */
15 protected $files = [];
16
17 /**
18 * Prepares HTTP files for Request
19 *
20 * @param array $files
21 *
22 * @return array
23 */
24 public function prepareFiles($files = [])
25 {
26 foreach ($files as $key => &$file) {
27 $file = $this->convertFileInformation($file);
28 }
29
30 return $files;
31 }
32
33 /**
34 * @taken from \Symfony\Component\HttpFoundation\FileBag
35 *
36 * Converts uploaded files to UploadedFile instances.
37 *
38 * @param array|File $file A (multi-dimensional) array of uploaded file information
39 *
40 * @return File[]|File|null A (multi-dimensional) array of File instances
41 */
42 protected function convertFileInformation($file)
43 {
44 $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type');
45
46 if ($file instanceof File) {
47 return $file;
48 }
49
50 $file = $this->fixPhpFilesArray($file);
51
52 if (is_array($file)) {
53 $keys = array_keys($file);
54 sort($keys);
55
56 if ($keys == $fileKeys) {
57 if (UPLOAD_ERR_NO_FILE == $file['error']) {
58 $file = null;
59 } else {
60 $file = new File(
61 $file['tmp_name'],
62 $file['name'],
63 $file['type'],
64 $file['size'],
65 $file['error']
66 );
67 }
68 } else {
69 $file = array_map(array($this, 'convertFileInformation'), $file);
70 if (array_keys($keys) === $keys) {
71 $file = array_filter($file);
72 }
73 }
74 }
75
76 return $file;
77 }
78
79 /**
80 * @taken from \Symfony\Component\HttpFoundation\FileBag
81 *
82 * Fixes a malformed PHP $_FILES array.
83 *
84 * PHP has a bug that the format of the $_FILES array differs, depending on
85 * whether the uploaded file fields had normal field names or array-like
86 * field names ("normal" vs. "parent[child]").
87 *
88 * This method fixes the array to look like the "normal" $_FILES array.
89 *
90 * It's safe to pass an already converted array, in which case this method
91 * just returns the original array unmodified.
92 *
93 * @return array
94 */
95 protected function fixPhpFilesArray($data)
96 {
97 $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type');
98
99 if (!is_array($data)) {
100 return $data;
101 }
102
103 // Remove extra key added by PHP 8.1.
104 unset($data['full_path']);
105 $keys = array_keys($data);
106 sort($keys);
107
108 if (
109 $fileKeys != $keys ||
110 !isset($data['name']) ||
111 !is_array($data['name'])
112 ) {
113 return $data;
114 }
115
116 $files = $data;
117 foreach ($fileKeys as $k) {
118 unset($files[$k]);
119 }
120
121 foreach ($data['name'] as $key => $name) {
122 $files[$key] = $this->fixPhpFilesArray(array(
123 'error' => $data['error'][$key],
124 'name' => $name,
125 'type' => $data['type'][$key],
126 'tmp_name' => $data['tmp_name'][$key],
127 'size' => $data['size'][$key],
128 ));
129 }
130
131 return $files;
132 }
133
134 /**
135 * Retrieve a file from the request.
136 *
137 * @param string|null $key
138 * @param mixed $default
139 * @return \FluentCommunity\Framework\Http\Request\File|array|null
140 */
141 public function file($key = null, $default = null)
142 {
143 return Helper::dataGet($this->files(), $key, $default);
144 }
145
146 /**
147 * Get the files array from the request.
148 *
149 * @return array
150 */
151 public function files($key = null)
152 {
153 if (empty($this->files)) {
154 $this->files = $this->prepareFiles($_FILES);
155 }
156
157 return Helper::dataGet($this->files, $key, $this->files);
158 }
159
160 /**
161 * Get the files as collection from the request.
162 *
163 * @return array
164 */
165 public function fileCollection($key = null)
166 {
167 $collection = Helper::collect(
168 Helper::dataGet($this->files, $key, $this->files)
169 );
170
171 if (!method_exists($collection, 'save')) {
172 $this->addSaveMethod($collection);
173 }
174
175 return $collection;
176 }
177
178 /**
179 * Add a save method on the runtime.
180 *
181 * @param Collection &$files
182 * @return void
183 */
184 protected function addSaveMethod(Collection &$files)
185 {
186 Collection::macro('save', function ($path = null) use ($files) {
187 return $files->map(function ($file) use ($path) {
188 // When developer will use the method without key name:
189 // i.e: $request->fileCollection(), an array of arrays
190 // ['images' => [0 => File, 1 => File]] will be
191 // returned, so $file will contain an array
192 // of File objects, [0 => File, 1 => File].
193 if (is_array($file)) {
194 $savedFiles = [];
195 foreach ($file as $fileObject) {
196 $savedFiles[] = $fileObject->save($path);
197 }
198 return $savedFiles;
199 }
200
201 // For $request->fileCollection('images')
202 // [0 => File, 1 => File] will be returned.
203 return $file->save($path);
204 });
205 });
206 }
207
208 /**
209 * Determine if the request contains a valid uploaded file for the given key.
210 *
211 * @param string $key
212 * @return bool
213 */
214 public function hasFile($key)
215 {
216 $file = $this->file($key);
217
218 if (is_array($file)) {
219 foreach ($file as $f) {
220 if ($f instanceof File && $f->isValid()) {
221 return true;
222 }
223 }
224 return false;
225 }
226
227 return $file instanceof File && $file->isValid();
228 }
229
230 /**
231 * Alias for hasFile().
232 *
233 * @param string $key
234 * @return bool
235 */
236 public function isValidFile($key)
237 {
238 return $this->hasFile($key);
239 }
240 }
241