|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.4.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Http\Request; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Exceptions\FileUploadException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Trait File |
|
21
|
|
|
* @package Quantum\Http\Request |
|
22
|
|
|
*/ |
|
23
|
|
|
trait File |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Files |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $__files = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Checks to see if request contains file |
|
34
|
|
|
* @param string $key |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function hasFile(string $key): bool |
|
38
|
|
|
{ |
|
39
|
|
|
return isset(self::$__files[$key]); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Gets the file info by given key |
|
44
|
|
|
* @param string $key |
|
45
|
|
|
* @return array|object |
|
46
|
|
|
* @throws \InvalidArgumentException |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function getFile(string $key) |
|
49
|
|
|
{ |
|
50
|
|
|
if (!self::hasFile($key)) { |
|
51
|
|
|
throw new \InvalidArgumentException(_message(FileUploadException::UPLOADED_FILE_NOT_FOUND, $key)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return self::$__files[$key]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param array $_files |
|
59
|
|
|
* @return array|object[]|null |
|
60
|
|
|
*/ |
|
61
|
|
|
private static function handleFiles(array $_files): ?array |
|
62
|
|
|
{ |
|
63
|
|
|
if (!count($_files)) { |
|
64
|
|
|
return []; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$key = key($_files); |
|
68
|
|
|
|
|
69
|
|
|
if ($key) { |
|
70
|
|
|
if (!is_array($_files[$key]['name'])) { |
|
71
|
|
|
return [$key => (object)$_files[$key]]; |
|
72
|
|
|
} else { |
|
73
|
|
|
$formattedFiles = []; |
|
74
|
|
|
|
|
75
|
|
|
foreach ($_files[$key]['name'] as $index => $name) { |
|
76
|
|
|
$formattedFiles[$key][$index] = (object)[ |
|
77
|
|
|
'name' => $name, |
|
78
|
|
|
'type' => $_files[$key]['type'][$index], |
|
79
|
|
|
'tmp_name' => $_files[$key]['tmp_name'][$index], |
|
80
|
|
|
'error' => $_files[$key]['error'][$index], |
|
81
|
|
|
'size' => $_files[$key]['size'][$index], |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $formattedFiles; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |