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