|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FileEye\MimeMap; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class for mapping file extensions to MIME types. |
|
7
|
|
|
*/ |
|
8
|
|
|
class MapHandler |
|
9
|
|
|
{ |
|
10
|
|
|
protected $map; |
|
11
|
|
|
|
|
12
|
12 |
|
public function __construct(array $map = null) |
|
13
|
|
|
{ |
|
14
|
12 |
|
if (is_null($map)) { |
|
15
|
12 |
|
$this->map = &TypeExtensionMap::$map; |
|
16
|
|
|
} else { |
|
17
|
|
|
$this->map = $map; |
|
18
|
|
|
} |
|
19
|
12 |
|
} |
|
20
|
|
|
|
|
21
|
12 |
|
public function get() |
|
22
|
|
|
{ |
|
23
|
12 |
|
return $this->map; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function sort() |
|
27
|
|
|
{ |
|
28
|
|
|
ksort($this->map['types']); |
|
29
|
|
|
ksort($this->map['extensions']); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
1 |
|
public function addMapping($type, $extension) |
|
33
|
|
|
{ |
|
34
|
1 |
|
$type = strtolower($type); |
|
35
|
1 |
|
$extension = strtolower($extension); |
|
36
|
|
|
|
|
37
|
|
|
// Add entry to 'types'. |
|
38
|
1 |
View Code Duplication |
if (!isset($this->map['types'][$type])) { |
|
|
|
|
|
|
39
|
1 |
|
$this->map['types'][$type] = [$extension]; |
|
40
|
|
|
} else { |
|
41
|
1 |
|
if (array_search($extension, $this->map['types'][$type]) === false) { |
|
42
|
|
|
$this->map['types'][$type][] = $extension; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// Add entry to 'extensions'. |
|
47
|
1 |
View Code Duplication |
if (!isset($this->map['extensions'][$extension])) { |
|
|
|
|
|
|
48
|
1 |
|
$this->map['extensions'][$extension] = [$type]; |
|
49
|
|
|
} else { |
|
50
|
1 |
|
if (array_search($type, $this->map['extensions'][$extension]) === false) { |
|
51
|
|
|
$this->map['extensions'][$extension][] = $type; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
public function removeMapping($type, $extension) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$type = strtolower($type); |
|
61
|
1 |
|
$extension = strtolower($extension); |
|
62
|
|
|
|
|
63
|
1 |
|
$ret = false; |
|
64
|
|
|
|
|
65
|
|
|
// Remove entry from 'types'. |
|
66
|
1 |
View Code Duplication |
if (isset($this->map['types'][$type])) { |
|
|
|
|
|
|
67
|
1 |
|
$key = array_search($extension, $this->map['types'][$type]); |
|
68
|
1 |
|
if ($key !== false) { |
|
69
|
1 |
|
$ret = true; |
|
70
|
1 |
|
unset($this->map['types'][$type][$key]); |
|
71
|
1 |
|
$tmp = []; |
|
72
|
1 |
|
foreach ($this->map['types'][$type] as $v) { |
|
73
|
1 |
|
$tmp[] = $v; |
|
74
|
|
|
} |
|
75
|
1 |
|
$this->map['types'][$type] = $tmp; |
|
76
|
1 |
|
if (empty($this->map['types'][$type])) { |
|
77
|
1 |
|
unset($this->map['types'][$type]); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// Remove entry from 'extensions'. |
|
83
|
1 |
View Code Duplication |
if (isset($this->map['extensions'][$extension])) { |
|
|
|
|
|
|
84
|
1 |
|
$key = array_search($type, $this->map['extensions'][$extension]); |
|
85
|
1 |
|
if ($key !== false) { |
|
86
|
1 |
|
$ret = true; |
|
87
|
1 |
|
unset($this->map['extensions'][$extension][$key]); |
|
88
|
1 |
|
$tmp = []; |
|
89
|
1 |
|
foreach ($this->map['extensions'][$extension] as $v) { |
|
90
|
|
|
$tmp[] = $v; |
|
91
|
|
|
} |
|
92
|
1 |
|
$this->map['extensions'][$extension] = $tmp; |
|
93
|
1 |
|
if (empty($this->map['extensions'][$extension])) { |
|
94
|
1 |
|
unset($this->map['extensions'][$extension]); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
return $ret; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
public function removeType($type) |
|
103
|
|
|
{ |
|
104
|
1 |
|
$type = strtolower($type); |
|
105
|
|
|
|
|
106
|
1 |
|
if (!isset($this->map['types'][$type])) { |
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
1 |
|
foreach ($this->map['types'][$type] as $extension) { |
|
110
|
1 |
|
$this->removeMapping($type, $extension); |
|
111
|
|
|
} |
|
112
|
1 |
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
1 |
View Code Duplication |
public function setTypeDefaultExtension($type, $extension) |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
1 |
|
$type = strtolower($type); |
|
118
|
1 |
|
$extension = strtolower($extension); |
|
119
|
|
|
|
|
120
|
1 |
|
if (!isset($this->map['types'][$type])) { |
|
121
|
|
|
throw new MappingException('Cannot set ' . $extension . ' as default extension for type ' . $type . ', type not defined'); |
|
122
|
|
|
} |
|
123
|
1 |
|
$key = array_search($extension, $this->map['types'][$type]); |
|
124
|
1 |
|
if ($key === false) { |
|
125
|
|
|
throw new MappingException('Cannot set ' . $extension . ' as default extension for type ' . $type . ', extension not associated to this type'); |
|
126
|
|
|
} |
|
127
|
1 |
|
$tmp = [$extension]; |
|
128
|
1 |
|
foreach ($this->map['types'][$type] as $k => $v) { |
|
129
|
1 |
|
if ($k === $key) { |
|
130
|
1 |
|
continue; |
|
131
|
|
|
} |
|
132
|
1 |
|
$tmp[] = $v; |
|
133
|
|
|
} |
|
134
|
1 |
|
$this->map['types'][$type] = $tmp; |
|
135
|
1 |
|
} |
|
136
|
|
|
|
|
137
|
1 |
View Code Duplication |
public function setExtensionDefaultType($extension, $type) |
|
|
|
|
|
|
138
|
|
|
{ |
|
139
|
1 |
|
$type = strtolower($type); |
|
140
|
1 |
|
$extension = strtolower($extension); |
|
141
|
|
|
|
|
142
|
1 |
|
if (!isset($this->map['extensions'][$extension])) { |
|
143
|
|
|
throw new MappingException('Cannot set ' . $type . ' as default type for extension ' . $extension . ', extension not defined'); |
|
144
|
|
|
} |
|
145
|
1 |
|
$key = array_search($type, $this->map['extensions'][$extension]); |
|
146
|
1 |
|
if ($key === false) { |
|
147
|
|
|
throw new MappingException('Cannot set ' . $type . ' as default type for extension ' . $extension . ', type not associated to this extension'); |
|
148
|
|
|
} |
|
149
|
1 |
|
$tmp = [$type]; |
|
150
|
1 |
|
foreach ($this->map['extensions'][$extension] as $k => $v) { |
|
151
|
1 |
|
if ($k === $key) { |
|
152
|
1 |
|
continue; |
|
153
|
|
|
} |
|
154
|
1 |
|
$tmp[] = $v; |
|
155
|
|
|
} |
|
156
|
1 |
|
$this->map['extensions'][$extension] = $tmp; |
|
157
|
1 |
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.