1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
namespace Enjoys\Upload; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
10
|
|
|
|
11
|
|
|
final class FileInfo |
12
|
|
|
{ |
13
|
|
|
private string $filename; |
14
|
|
|
private string $originalFilename; |
15
|
|
|
private string $mediaType; |
16
|
|
|
private string $extension; |
17
|
|
|
private int $size; |
18
|
|
|
|
19
|
20 |
|
public function __construct(UploadedFileInterface $file) |
20
|
|
|
{ |
21
|
20 |
|
$this->filename = $this->originalFilename = $file->getClientFilename() ?? ''; |
22
|
|
|
|
23
|
20 |
|
$this->mediaType = $file->getClientMediaType() ?? ''; |
24
|
|
|
|
25
|
20 |
|
$this->extension = pathinfo( |
26
|
20 |
|
$file->getClientFilename() ?? '', |
27
|
20 |
|
PATHINFO_EXTENSION |
28
|
20 |
|
); |
29
|
|
|
|
30
|
20 |
|
$this->size = $file->getSize() ?? 0; |
31
|
|
|
} |
32
|
|
|
|
33
|
3 |
|
public function getFilenameWithoutExtension(): string |
34
|
|
|
{ |
35
|
3 |
|
return $this->removeExtension($this->filename); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
7 |
|
public function getExtensionWithDot(): string |
40
|
|
|
{ |
41
|
7 |
|
return (empty($this->extension)) ? '' : '.' . $this->extension; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Automatically adds an extension if it is not specified |
46
|
|
|
* @param string $filename |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
5 |
|
public function setFilename(string $filename): void |
50
|
|
|
{ |
51
|
5 |
|
$this->filename = $this->removeExtension($filename) . $this->getExtensionWithDot(); |
52
|
|
|
} |
53
|
|
|
|
54
|
8 |
|
public function getFilename(): string |
55
|
|
|
{ |
56
|
8 |
|
return $this->filename; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function getMediaType(): string |
60
|
|
|
{ |
61
|
1 |
|
return $this->mediaType; |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
public function getOriginalFilename(): string |
65
|
|
|
{ |
66
|
4 |
|
return $this->originalFilename; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
1 |
|
public function getExtension(): string |
71
|
|
|
{ |
72
|
1 |
|
return $this->extension; |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
public function getSize(): int |
76
|
|
|
{ |
77
|
2 |
|
return $this->size; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $filename |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
5 |
|
private function removeExtension(string $filename): string |
85
|
|
|
{ |
86
|
5 |
|
return preg_replace( |
87
|
5 |
|
sprintf('/%s$/', preg_quote($this->getExtensionWithDot())), |
88
|
5 |
|
'', |
89
|
5 |
|
$filename |
90
|
5 |
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|