1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2019 Amin Yazdanpanah<http://www.aminyazdanpanah.com>. |
5
|
|
|
* |
6
|
|
|
* Licensed under the MIT License; |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* https://opensource.org/licenses/MIT |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace AYazdanpanah\SaveUploadedFiles; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
use AYazdanpanah\SaveUploadedFiles\Exception\Exception; |
23
|
|
|
|
24
|
|
|
class Upload |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param $files |
28
|
|
|
* @return Filter |
29
|
|
|
* @throws Exception |
30
|
|
|
*/ |
31
|
|
|
public static function files($files) |
32
|
|
|
{ |
33
|
|
|
if (!is_array($files) && count($files) > 0) { |
34
|
|
|
throw new Exception("File must be an array"); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$extractions = []; |
38
|
|
|
|
39
|
|
|
foreach ($files as $file) { |
40
|
|
|
if (!isset($file['name'], $file['save_to'])) { |
41
|
|
|
throw new Exception("Filename or path is not specified"); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$validator = static::validator(static::mockValidator()); |
45
|
|
|
$override = false; |
46
|
|
|
$save_as = null; |
47
|
|
|
$export = static::mockCallback(); |
48
|
|
|
|
49
|
|
|
if(isset($file['export'])){ |
50
|
|
|
$export = $file['export']; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (isset($file['validator'])) { |
54
|
|
|
$validator = static::validator($file['validator']); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (isset($file['override'])) { |
58
|
|
|
$override = $file['override']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (isset($file['save_as'])) { |
62
|
|
|
$save_as = $file['save_as']; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$extractions[$file['name']] = (new File()) |
66
|
|
|
->file($file['name']) |
67
|
|
|
->setValidator($validator) |
68
|
|
|
->setOverride($override) |
69
|
|
|
->setSaveAs($save_as) |
70
|
|
|
->save($file['save_to'], $export); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return new Filter($extractions); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $validator |
78
|
|
|
* @return Validator |
79
|
|
|
* @throws Exception |
80
|
|
|
*/ |
81
|
|
|
private static function validator($validator): Validator |
82
|
|
|
{ |
83
|
|
|
if (!isset($validator['min_size'], $validator['max_size'], $validator['allowed_extensions']) || !is_array($validator['allowed_extensions'])) { |
84
|
|
|
throw new Exception("Invalid validator inputs: check min_size, max_size, and types again"); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return (new Validator()) |
88
|
|
|
->setMinSize($validator['min_size']) |
89
|
|
|
->setMaxSize($validator['max_size']) |
90
|
|
|
->setType($validator['allowed_extensions']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
private static function mockValidator():array |
97
|
|
|
{ |
98
|
|
|
return [ |
99
|
|
|
'min_size' => 1, |
100
|
|
|
'max_size' => 999999, |
101
|
|
|
'allowed_extensions' => ['*'] |
102
|
|
|
|
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private static function mockCallback(): callable |
107
|
|
|
{ |
108
|
|
|
return function ($filename){ |
109
|
|
|
return $filename; |
110
|
|
|
}; |
111
|
|
|
} |
112
|
|
|
} |