|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Enjoys\Upload; |
|
6
|
|
|
|
|
7
|
|
|
use League\Flysystem\Filesystem; |
|
8
|
|
|
use League\Flysystem\FilesystemException; |
|
9
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
|
10
|
|
|
|
|
11
|
|
|
final class UploadProcessing |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
private ?string $targetPath = null; |
|
15
|
|
|
private FileInfo $fileInfo; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var RuleInterface[] |
|
19
|
|
|
*/ |
|
20
|
|
|
private array $rules = []; |
|
21
|
|
|
|
|
22
|
9 |
|
public function __construct(private UploadedFileInterface $uploadedFile, private Filesystem $filesystem) |
|
23
|
|
|
{ |
|
24
|
9 |
|
$this->fileInfo = new FileInfo($uploadedFile); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @throws FilesystemException |
|
29
|
|
|
*/ |
|
30
|
4 |
|
public function upload(string $targetPath = '/'): void |
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
4 |
|
$this->validate(); |
|
34
|
|
|
|
|
35
|
3 |
|
$this->targetPath = rtrim($targetPath, '/') . '/' . $this->fileInfo->getFilename(); |
|
36
|
3 |
|
$this->filesystem->writeStream($this->targetPath, $this->uploadedFile->getStream()->detach()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
4 |
|
private function validate(): void |
|
40
|
|
|
{ |
|
41
|
4 |
|
foreach ($this->rules as $rule) { |
|
42
|
2 |
|
$rule->check($this->getUploadedFile()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Automatically adds an extension if it is not specified |
|
49
|
|
|
* @param string $filename |
|
50
|
|
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function setFilename(string $filename): void |
|
53
|
|
|
{ |
|
54
|
1 |
|
$this->fileInfo->setFilename($filename); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
3 |
|
public function getUploadedFile(): UploadedFileInterface |
|
59
|
|
|
{ |
|
60
|
3 |
|
return $this->uploadedFile; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
2 |
|
public function getTargetPath(): ?string |
|
65
|
|
|
{ |
|
66
|
2 |
|
return $this->targetPath; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
1 |
|
public function getFilesystem(): Filesystem |
|
71
|
|
|
{ |
|
72
|
1 |
|
return $this->filesystem; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return FileInfo |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function getFileInfo(): FileInfo |
|
79
|
|
|
{ |
|
80
|
1 |
|
return $this->fileInfo; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
4 |
|
public function addRule(RuleInterface $rule): void |
|
85
|
|
|
{ |
|
86
|
4 |
|
$this->rules[] = $rule; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param RuleInterface[] $rules |
|
91
|
|
|
* @return void |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function addRules(array $rules): void |
|
94
|
|
|
{ |
|
95
|
1 |
|
$this->rules = array_merge($this->rules, $rules); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return RuleInterface[] |
|
100
|
|
|
*/ |
|
101
|
2 |
|
public function getRules(): array |
|
102
|
|
|
{ |
|
103
|
2 |
|
return $this->rules; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|