1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DEMV\File; |
4
|
|
|
|
5
|
|
|
use function Dgame\Ensurance\enforce; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Filename |
9
|
|
|
* @package DEMV\File |
10
|
|
|
*/ |
11
|
|
|
final class Filename |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $basename; |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $extension; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Filename constructor. |
24
|
|
|
* |
25
|
|
|
* @param string $filename |
26
|
|
|
* @param array $replace |
27
|
|
|
*/ |
28
|
|
|
public function __construct(string $filename, array $replace = []) |
29
|
|
|
{ |
30
|
|
|
$info = pathinfo($filename); |
31
|
|
|
if (array_key_exists('extension', $info)) { |
|
|
|
|
32
|
|
|
$this->setExtension($info['extension']); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
unset($info['extension']); |
36
|
|
|
unset($info['basename']); |
37
|
|
|
|
38
|
|
|
if (empty($replace)) { |
39
|
|
|
$replace = [ |
40
|
|
|
'Ä' => 'Ae', |
41
|
|
|
'Ö' => 'Oe', |
42
|
|
|
'Ü' => 'Ue', |
43
|
|
|
'ä' => 'ae', |
44
|
|
|
'ö' => 'oe', |
45
|
|
|
'ü' => 'ue', |
46
|
|
|
'ß' => 'ss' |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
foreach ($info as $key => $value) { |
51
|
|
|
$info[$key] = self::clean($info[$key], $replace); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$info = array_filter($info); |
|
|
|
|
55
|
|
|
$info = array_map('strtolower', $info); |
56
|
|
|
|
57
|
|
|
$this->basename = implode('_', $info); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param int $length |
62
|
|
|
* |
63
|
|
|
* @throws \Exception |
64
|
|
|
*/ |
65
|
|
|
public function limitLength(int $length): void |
66
|
|
|
{ |
67
|
|
|
enforce($length > 0)->orThrow('Invalide Länge: %d', $length); |
68
|
|
|
|
69
|
|
|
if (strlen($this->basename) > $length) { |
70
|
|
|
$this->basename = substr($this->basename, 0, $length); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $filename |
76
|
|
|
* @param array $replace |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public static function clean(string $filename, array $replace = []): string |
81
|
|
|
{ |
82
|
|
|
$filename = trim($filename); |
83
|
|
|
$filename = trim($filename, '_-'); |
84
|
|
|
|
85
|
|
|
if (!empty($replace)) { |
86
|
|
|
$filename = str_replace(array_keys($replace), array_values($replace), $filename); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Leerzeichen durch Unterstrich |
90
|
|
|
$filename = preg_replace('/\s+/', '_', $filename); |
91
|
|
|
// Alle Nicht-Alphanummerischen Zeichen, Binde- oder Unterstriche und Klammern zu Unterstrich |
92
|
|
|
$filename = preg_replace('/[^\w\-_\(\)]+/i', '_', $filename); |
93
|
|
|
// Mehrere aufeinanderfolgende Unterstriche zu einem Unterstrich |
94
|
|
|
$filename = preg_replace('/_+/', '_', $filename); |
95
|
|
|
// Mehrere aufeinanderfolgende Bindestriche zu einem Bindestrich |
96
|
|
|
$filename = preg_replace('/-+/', '-', $filename); |
97
|
|
|
// Aufeinanderfolgende Unterstrich/Bindestrich Sequenzen zu einem Bindestrich |
98
|
|
|
$filename = preg_replace('/(?:_-_|-_-|-_|_-)/', '-', $filename); |
99
|
|
|
|
100
|
|
|
return trim($filename, '_-'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return null|string |
105
|
|
|
*/ |
106
|
|
|
public function getExtension() |
107
|
|
|
{ |
108
|
|
|
return $this->extension; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function hasExtension(): bool |
115
|
|
|
{ |
116
|
|
|
return $this->extension !== null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $extension |
121
|
|
|
*/ |
122
|
|
|
public function setExtension(string $extension): void |
123
|
|
|
{ |
124
|
|
|
$extension = strtolower($extension); |
125
|
|
|
if (MimeTypesFactory::create()->getMimeType($extension) !== null) { |
126
|
|
|
$this->extension = $extension; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function getBasename(): string |
134
|
|
|
{ |
135
|
|
|
return $this->basename; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function isValid(): bool |
142
|
|
|
{ |
143
|
|
|
return strlen($this->getBasename()) !== 0 && $this->hasExtension(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function assemble(): string |
150
|
|
|
{ |
151
|
|
|
if ($this->hasExtension()) { |
152
|
|
|
return sprintf('%s.%s', $this->getBasename(), $this->getExtension()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this->getBasename(); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|