1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FileEye\MimeMap\Map; |
4
|
|
|
|
5
|
|
|
use FileEye\MimeMap\MappingException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Abstract class for mapping file extensions to MIME types. |
9
|
|
|
* |
10
|
|
|
* This class cannot be instantiated; extend from it to implement a map. |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractMap extends BaseMap |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Determines if a MIME type exists. |
16
|
|
|
* |
17
|
|
|
* @param string $type The type to be found. |
18
|
|
|
* |
19
|
|
|
* @return bool |
20
|
|
|
*/ |
21
|
13 |
|
public function hasType($type) |
22
|
|
|
{ |
23
|
13 |
|
return (bool) $this->getMapEntry('t', $type); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Determines if a MIME type alias exists. |
28
|
|
|
* |
29
|
|
|
* @param string $alias The alias to be found. |
30
|
|
|
* |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
19 |
|
public function hasAlias($alias) |
34
|
|
|
{ |
35
|
19 |
|
return (bool) $this->getMapEntry('a', $alias); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Determines if an entry exists from the 'extensions' array. |
40
|
|
|
* |
41
|
|
|
* @param string $extension The extension to be found. |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
1 |
|
public function hasExtension($extension) |
46
|
|
|
{ |
47
|
1 |
|
return (bool) $this->getMapEntry('e', $extension); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Lists all the MIME types defined in the map. |
52
|
|
|
* |
53
|
|
|
* @param string $match (Optional) a match wildcard to limit the list. |
54
|
|
|
* |
55
|
|
|
* @return string[] |
56
|
|
|
*/ |
57
|
7 |
|
public function listTypes($match = null) |
58
|
|
|
{ |
59
|
7 |
|
return $this->listEntries('t', $match); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Lists all the MIME types aliases defined in the map. |
64
|
|
|
* |
65
|
|
|
* @param string $match (Optional) a match wildcard to limit the list. |
66
|
|
|
* |
67
|
|
|
* @return string[] |
68
|
|
|
*/ |
69
|
2 |
|
public function listAliases($match = null) |
70
|
|
|
{ |
71
|
2 |
|
return $this->listEntries('a', $match); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Lists all the extensions defined in the map. |
76
|
|
|
* |
77
|
|
|
* @param string $match (Optional) a match wildcard to limit the list. |
78
|
|
|
* |
79
|
|
|
* @return string[] |
80
|
|
|
*/ |
81
|
3 |
|
public function listExtensions($match = null) |
82
|
|
|
{ |
83
|
3 |
|
return $this->listEntries('e', $match); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Adds a description of a MIME type. |
88
|
|
|
* |
89
|
|
|
* @param string $type |
90
|
|
|
* A MIME type. |
91
|
|
|
* @param string $description |
92
|
|
|
* The description of the MIME type. |
93
|
|
|
* |
94
|
|
|
* @throws MappingException if $type is an alias. |
95
|
|
|
* |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
2 |
|
public function addTypeDescription($type, $description) |
99
|
|
|
{ |
100
|
|
|
// Consistency checks. |
101
|
2 |
|
if ($this->hasAlias($type)) { |
102
|
1 |
|
throw new MappingException("Cannot add description for '{$type}', '{$type}' is an alias"); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
$this->addMapSubEntry('t', $type, 'desc', $description); |
106
|
1 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Adds an alias of a MIME type. |
111
|
|
|
* |
112
|
|
|
* @param string $type |
113
|
|
|
* A MIME type. |
114
|
|
|
* @param string $alias |
115
|
|
|
* An alias of $type. |
116
|
|
|
* |
117
|
|
|
* @throws MappingException if no $type is found. |
118
|
|
|
* |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
3 |
|
public function addTypeAlias($type, $alias) |
122
|
|
|
{ |
123
|
3 |
|
$type = strtolower($type); |
124
|
3 |
|
$alias = strtolower($alias); |
125
|
|
|
|
126
|
|
|
// Consistency checks. |
127
|
3 |
|
if (!$this->hasType($type)) { |
128
|
1 |
|
throw new MappingException("Cannot set '{$alias}' as alias for '{$type}', '{$type}' not defined"); |
129
|
|
|
} |
130
|
2 |
|
if ($this->hasType($alias)) { |
131
|
2 |
|
throw new MappingException("Cannot set '{$alias}' as alias for '{$type}', '{$alias}' is already defined as a type"); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
$this->addMapSubEntry('t', $type, 'a', $alias); |
135
|
1 |
|
$this->addMapSubEntry('a', $alias, 't', $type); |
136
|
1 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Adds a type-to-extension mapping. |
141
|
|
|
* |
142
|
|
|
* @param string $type |
143
|
|
|
* A MIME type. |
144
|
|
|
* @param string $extension |
145
|
|
|
* A file extension. |
146
|
|
|
* |
147
|
|
|
* @throws MappingException if $type is an alias. |
148
|
|
|
* |
149
|
|
|
* @return $this |
150
|
|
|
*/ |
151
|
6 |
|
public function addTypeExtensionMapping($type, $extension) |
152
|
|
|
{ |
153
|
6 |
|
$type = strtolower($type); |
154
|
6 |
|
$extension = strtolower($extension); |
155
|
|
|
|
156
|
|
|
// Consistency checks. |
157
|
6 |
|
if ($this->hasAlias($type)) { |
158
|
1 |
|
throw new MappingException("Cannot map '{$extension}' to '{$type}', '{$type}' is an alias"); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// Add entry to 't'. |
162
|
5 |
|
$this->addMapSubEntry('t', $type, 'e', $extension); |
163
|
|
|
|
164
|
|
|
// Add entry to 'e'. |
165
|
5 |
|
$this->addMapSubEntry('e', $extension, 't', $type); |
166
|
|
|
|
167
|
5 |
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Gets the descriptions of a MIME type. |
172
|
|
|
* |
173
|
|
|
* @param string $type The type to be found. |
174
|
|
|
* |
175
|
|
|
* @return string[] The mapped descriptions. |
176
|
|
|
*/ |
177
|
1 |
|
public function getTypeDescriptions($type) |
178
|
|
|
{ |
179
|
1 |
|
$res = $this->getMapSubEntry('t', $type, 'desc'); |
180
|
1 |
|
return $res ?: []; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Gets the aliases of a MIME type. |
185
|
|
|
* |
186
|
|
|
* @param string $type The type to be found. |
187
|
|
|
* |
188
|
|
|
* @return string[] The mapped aliases. |
189
|
|
|
*/ |
190
|
2 |
|
public function getTypeAliases($type) |
191
|
|
|
{ |
192
|
2 |
|
$res = $this->getMapSubEntry('t', $type, 'a'); |
193
|
2 |
|
return $res ?: []; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Gets the content of an entry from the 't' array. |
198
|
|
|
* |
199
|
|
|
* @param string $type The type to be found. |
200
|
|
|
* |
201
|
|
|
* @return string[] The mapped file extensions. |
202
|
|
|
*/ |
203
|
5 |
|
public function getTypeExtensions($type) |
204
|
|
|
{ |
205
|
5 |
|
$res = $this->getMapSubEntry('t', $type, 'e'); |
206
|
5 |
|
return $res ?: []; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Changes the default extension for a MIME type. |
211
|
|
|
* |
212
|
|
|
* @param string $type |
213
|
|
|
* A MIME type. |
214
|
|
|
* @param string $extension |
215
|
|
|
* A file extension. |
216
|
|
|
* |
217
|
|
|
* @throws MappingException if no mapping found. |
218
|
|
|
* |
219
|
|
|
* @return $this |
220
|
|
|
*/ |
221
|
3 |
|
public function setTypeDefaultExtension($type, $extension) |
222
|
|
|
{ |
223
|
3 |
|
return $this->setValueAsDefault('t', $type, 'e', $extension); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Removes the entire mapping of a type. |
228
|
|
|
* |
229
|
|
|
* @param string $type |
230
|
|
|
* A MIME type. |
231
|
|
|
* |
232
|
|
|
* @return bool |
233
|
|
|
* true if the mapping was removed, false if the type was not present. |
234
|
|
|
*/ |
235
|
1 |
|
public function removeType($type) |
236
|
|
|
{ |
237
|
1 |
|
$type = strtolower($type); |
238
|
|
|
|
239
|
|
|
// Return false if type is not found. |
240
|
1 |
|
if (!$this->hasType($type)) { |
241
|
1 |
|
return false; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
// Loop through all the associated extensions and remove them. |
245
|
1 |
|
foreach ($this->getTypeExtensions($type) as $extension) { |
246
|
1 |
|
$this->removeTypeExtensionMapping($type, $extension); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
// Loop through all the associated aliases and remove them. |
250
|
1 |
|
foreach ($this->getTypeAliases($type) as $alias) { |
251
|
1 |
|
$this->removeTypeAlias($type, $alias); |
252
|
|
|
} |
253
|
|
|
|
254
|
1 |
|
unset(static::$map['t'][$type]); |
255
|
|
|
|
256
|
1 |
|
return true; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Removes a MIME type alias. |
261
|
|
|
* |
262
|
|
|
* @param string $type |
263
|
|
|
* A MIME type. |
264
|
|
|
* @param string $alias |
265
|
|
|
* The alias to be removed. |
266
|
|
|
* |
267
|
|
|
* @return bool |
268
|
|
|
* true if the alias was removed, false if the alias was not present. |
269
|
|
|
*/ |
270
|
1 |
|
public function removeTypeAlias($type, $alias) |
271
|
|
|
{ |
272
|
1 |
|
$type = strtolower($type); |
273
|
1 |
|
$alias = strtolower($alias); |
274
|
|
|
|
275
|
1 |
|
$type_ret = $this->removeMapSubEntry('t', $type, 'a', $alias); |
276
|
1 |
|
$alias_ret = $this->removeMapSubEntry('a', $alias, 't', $type); |
277
|
|
|
|
278
|
1 |
|
return $type_ret && $alias_ret; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Removes a type-to-extension mapping. |
283
|
|
|
* |
284
|
|
|
* @param string $type |
285
|
|
|
* A MIME type. |
286
|
|
|
* @param string $extension |
287
|
|
|
* The file extension to be removed. |
288
|
|
|
* |
289
|
|
|
* @return bool |
290
|
|
|
* true if the mapping was removed, false if the mapping was not present. |
291
|
|
|
*/ |
292
|
1 |
|
public function removeTypeExtensionMapping($type, $extension) |
293
|
|
|
{ |
294
|
1 |
|
$type = strtolower($type); |
295
|
1 |
|
$extension = strtolower($extension); |
296
|
|
|
|
297
|
1 |
|
$type_ret = $this->removeMapSubEntry('t', $type, 'e', $extension); |
298
|
1 |
|
$extension_ret = $this->removeMapSubEntry('e', $extension, 't', $type); |
299
|
|
|
|
300
|
1 |
|
return $type_ret && $extension_ret; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Gets the parent types of an alias. |
305
|
|
|
* |
306
|
|
|
* @param string $alias The alias to be found. |
307
|
|
|
* |
308
|
|
|
* @return string[] |
309
|
|
|
*/ |
310
|
3 |
|
public function getAliasTypes($alias) |
311
|
|
|
{ |
312
|
3 |
|
$res = $this->getMapSubEntry('a', $alias, 't'); |
313
|
3 |
|
return $res ?: []; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Gets the content of an entry from the 'extensions' array. |
318
|
|
|
* |
319
|
|
|
* @param string $extension The extension to be found. |
320
|
|
|
* |
321
|
|
|
* @return string[] The mapped MIME types. |
322
|
|
|
*/ |
323
|
9 |
|
public function getExtensionTypes($extension) |
324
|
|
|
{ |
325
|
9 |
|
$res = $this->getMapSubEntry('e', $extension, 't'); |
326
|
9 |
|
return $res ?: []; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Changes the default MIME type for a file extension. |
331
|
|
|
* |
332
|
|
|
* @param string $extension |
333
|
|
|
* A file extension. |
334
|
|
|
* @param string $type |
335
|
|
|
* A MIME type. |
336
|
|
|
* |
337
|
|
|
* @throws MappingException if no mapping found. |
338
|
|
|
* |
339
|
|
|
* @return $this |
340
|
|
|
*/ |
341
|
3 |
|
public function setExtensionDefaultType($extension, $type) |
342
|
|
|
{ |
343
|
3 |
|
return $this->setValueAsDefault('e', $extension, 't', $type); |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|