-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBaseMap.php
More file actions
273 lines (245 loc) · 7.52 KB
/
Copy pathBaseMap.php
File metadata and controls
273 lines (245 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php declare(strict_types=1);
namespace FileEye\MimeMap\Map;
use FileEye\MimeMap\MappingException;
/**
* Abstract base class for managing MimeMap maps.
*
* This class cannot be instantiated.
*
* @template TMap of GenericMap
* @implements MapInterface<TMap>
*/
abstract class BaseMap implements MapInterface
{
/**
* Singleton instance.
*
* @var MapInterface<TMap>|null
*/
protected static $instance;
/**
* Mapping between file extensions and MIME types.
*
* @var TMap
*/
protected static $map = [];
/**
* A backup of the mapping between file extensions and MIME types.
*
* Used during the map update process.
*
* @var TMap|null
*/
protected static ?array $backupMap;
public function __construct()
{
}
public function backup(): void
{
static::$backupMap = static::$map;
}
public function reset(): void
{
if (isset(static::$backupMap)) {
static::$map = static::$backupMap;
}
static::$backupMap = null;
}
public static function getInstance(): MapInterface
{
if (!isset(static::$instance)) {
static::$instance = new static();
}
return static::$instance;
}
public function getFileName(): string
{
throw new \LogicException(__METHOD__ . ' is not implemented in ' . get_called_class());
}
public function getMapArray(): array
{
return static::$map;
}
public function sort(): MapInterface
{
foreach (array_keys(static::$map) as $k) {
// @phpstan-ignore assign.propertyType
ksort(static::$map[$k]);
foreach (static::$map[$k] as &$sub) {
ksort($sub);
}
}
return $this;
}
/**
* Gets a list of entries of the map.
*
* @param string $entry
* The main array entry.
* @param string|null $match
* (Optional) a match wildcard to limit the list.
*
* @return list<int|string>
* The list of the entries.
*/
protected function listEntries(string $entry, ?string $match = null): array
{
if (!isset(static::$map[$entry])) {
return [];
}
$list = array_keys(static::$map[$entry]);
if (is_null($match)) {
return $list;
} else {
$re = strtr($match, ['/' => '\\/', '*' => '.*']);
return array_values(array_filter($list, function (int|string $v) use ($re): bool {
return preg_match("/$re/", (string) $v) === 1;
}));
}
}
/**
* Gets the content of an entry of the map.
*
* @param string $entry
* The main array entry.
* @param string $entryKey
* The main entry value.
*
* @return array<string,array<string>>
* The values of the entry, or empty array if missing.
*/
protected function getMapEntry(string $entry, string $entryKey): array
{
return static::$map[$entry][$entryKey] ?? [];
}
/**
* Gets the content of a subentry of the map.
*
* @param string $entry
* The main array entry.
* @param string $entryKey
* The main entry value.
* @param string $subEntry
* The sub entry.
*
* @return array<int<0,max>,string>
* The values of the subentry, or empty array if missing.
*/
protected function getMapSubEntry(string $entry, string $entryKey, string $subEntry): array
{
return static::$map[$entry][$entryKey][$subEntry] ?? [];
}
/**
* Adds an entry to the map.
*
* Checks that no duplicate entries are made.
*
* @param string $entry
* The main array entry.
* @param string $entryKey
* The main entry value.
* @param string $subEntry
* The sub entry.
* @param string $value
* The value to add.
*
* @return MapInterface<TMap>
*/
protected function addMapSubEntry(string $entry, string $entryKey, string $subEntry, string $value): MapInterface
{
if (!isset(static::$map[$entry][$entryKey][$subEntry])) {
static::$map[$entry][$entryKey][$subEntry] = [$value];
} else {
if (array_search($value, static::$map[$entry][$entryKey][$subEntry]) === false) {
static::$map[$entry][$entryKey][$subEntry][] = $value;
}
}
return $this;
}
/**
* Removes an entry from the map.
*
* @param string $entry
* The main array entry.
* @param string $entryKey
* The main entry value.
* @param string $subEntry
* The sub entry.
* @param string $value
* The value to remove.
*
* @return bool
* true if the entry was removed, false if the entry was not present.
*/
protected function removeMapSubEntry(string $entry, string $entryKey, string $subEntry, string $value): bool
{
// Return false if no entry.
if (!isset(static::$map[$entry][$entryKey][$subEntry])) {
return false;
}
// Return false if no value.
$k = array_search($value, static::$map[$entry][$entryKey][$subEntry]);
if ($k === false) {
return false;
}
// Remove the map sub entry key.
// @phpstan-ignore assign.propertyType
unset(static::$map[$entry][$entryKey][$subEntry][$k]);
// Remove the sub entry if no more values.
if (empty(static::$map[$entry][$entryKey][$subEntry])) {
// @phpstan-ignore assign.propertyType
unset(static::$map[$entry][$entryKey][$subEntry]);
} else {
// Resequence the remaining values.
$tmp = [];
foreach (static::$map[$entry][$entryKey][$subEntry] as $v) {
$tmp[] = $v;
}
static::$map[$entry][$entryKey][$subEntry] = $tmp;
}
// Remove the entry if no more values.
if (empty(static::$map[$entry][$entryKey])) {
// @phpstan-ignore assign.propertyType
unset(static::$map[$entry][$entryKey]);
}
return true;
}
/**
* Sets a value as the default for an entry.
*
* @param string $entry
* The main array entry.
* @param string $entryKey
* The main entry value.
* @param string $subEntry
* The sub entry.
* @param string $value
* The value to add.
*
* @throws MappingException if no mapping found.
*
* @return MapInterface<TMap>
*/
protected function setValueAsDefault(string $entry, string $entryKey, string $subEntry, string $value): MapInterface
{
// Throw exception if no entry.
if (!isset(static::$map[$entry][$entryKey][$subEntry])) {
throw new MappingException("Cannot set '{$value}' as default for '{$entryKey}', '{$entryKey}' not defined");
}
// Throw exception if no entry-value pair.
$k = array_search($value, static::$map[$entry][$entryKey][$subEntry]);
if ($k === false) {
throw new MappingException("Cannot set '{$value}' as default for '{$entryKey}', '{$value}' not associated to '{$entryKey}'");
}
// Move value to top of array and resequence the rest.
$tmp = [$value];
foreach (static::$map[$entry][$entryKey][$subEntry] as $kk => $v) {
if ($kk === $k) {
continue;
}
$tmp[] = $v;
}
static::$map[$entry][$entryKey][$subEntry] = $tmp;
return $this;
}
}