Passed
Push — master ( 98feaf...80c9d2 )
by mon
02:27
created

AbstractMap::getTypeDescriptions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 2
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 17
    public function hasType($type)
22
    {
23 17
        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 30
    public function hasAlias($alias)
34
    {
35 30
        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 3
    public function addTypeDescription($type, $description)
99
    {
100
        // Consistency checks.
101 3
        if ($this->hasAlias($type)) {
102 1
            throw new MappingException("Cannot add description for '{$type}', '{$type}' is an alias");
103
        }
104
105 2
        $this->addMapSubEntry('t', $type, 'desc', $description);
106 2
        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 7
    public function addTypeAlias($type, $alias)
122
    {
123 7
        $type = strtolower($type);
124 7
        $alias = strtolower($alias);
125
126
        // Consistency checks.
127 7
        if (!$this->hasType($type)) {
128 1
            throw new MappingException("Cannot set '{$alias}' as alias for '{$type}', '{$type}' not defined");
129
        }
130 6
        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 5
        $this->addMapSubEntry('t', $type, 'a', $alias);
135 5
        $this->addMapSubEntry('a', $alias, 't', $type);
136 5
        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 10
    public function addTypeExtensionMapping($type, $extension)
152
    {
153 10
        $type = strtolower($type);
154 10
        $extension = strtolower($extension);
155
156
        // Consistency checks.
157 10
        if ($this->hasAlias($type)) {
158 2
            throw new MappingException("Cannot map '{$extension}' to '{$type}', '{$type}' is an alias");
159
        }
160
161
        // Add entry to 't'.
162 9
        $this->addMapSubEntry('t', $type, 'e', $extension);
163
164
        // Add entry to 'e'.
165 9
        $this->addMapSubEntry('e', $extension, 't', $type);
166
167 9
        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
        return $this->getMapSubEntry('t', $type, 'desc') ?: [];
180
    }
181
182
    /**
183
     * Gets the aliases of a MIME type.
184
     *
185
     * @param string $type The type to be found.
186
     *
187
     * @return string[] The mapped aliases.
188
     */
189 3
    public function getTypeAliases($type)
190
    {
191 3
        return $this->getMapSubEntry('t', $type, 'a') ?: [];
192
    }
193
194
    /**
195
     * Gets the content of an entry from the 't' array.
196
     *
197
     * @param string $type The type to be found.
198
     *
199
     * @return string[] The mapped file extensions.
200
     */
201 6
    public function getTypeExtensions($type)
202
    {
203 6
        return $this->getMapSubEntry('t', $type, 'e') ?: [];
204
    }
205
206
    /**
207
     * Changes the default extension for a MIME type.
208
     *
209
     * @param string $type
210
     *   A MIME type.
211
     * @param string $extension
212
     *   A file extension.
213
     *
214
     * @throws MappingException if no mapping found.
215
     *
216
     * @return $this
217
     */
218 3
    public function setTypeDefaultExtension($type, $extension)
219
    {
220 3
        return $this->setValueAsDefault('t', $type, 'e', $extension);
221
    }
222
223
    /**
224
     * Removes the entire mapping of a type.
225
     *
226
     * @param string $type
227
     *   A MIME type.
228
     *
229
     * @return bool
230
     *   true if the mapping was removed, false if the type was not present.
231
     */
232 2
    public function removeType($type)
233
    {
234 2
        $type = strtolower($type);
235
236
        // Return false if type is not found.
237 2
        if (!$this->hasType($type)) {
238 1
            return false;
239
        }
240
241
        // Loop through all the associated extensions and remove them.
242 2
        foreach ($this->getTypeExtensions($type) as $extension) {
243 2
            $this->removeTypeExtensionMapping($type, $extension);
244
        }
245
246
        // Loop through all the associated aliases and remove them.
247 2
        foreach ($this->getTypeAliases($type) as $alias) {
248 2
            $this->removeTypeAlias($type, $alias);
249
        }
250
251 2
        unset(static::$map['t'][$type]);
252
253 2
        return true;
254
    }
255
256
    /**
257
     * Removes a MIME type alias.
258
     *
259
     * @param string $type
260
     *   A MIME type.
261
     * @param string $alias
262
     *   The alias to be removed.
263
     *
264
     * @return bool
265
     *   true if the alias was removed, false if the alias was not present.
266
     */
267 2
    public function removeTypeAlias($type, $alias)
268
    {
269 2
        $type = strtolower($type);
270 2
        $alias = strtolower($alias);
271
272
        // Remove any extension mapped to the alias.
273 2
        if ($extensions = $this->getMapSubEntry('a', $alias, 'e')) {
274 1
            foreach ($extensions as $extension) {
275 1
                $this->removeMapSubEntry('a', $alias, 'e', $extension);
276 1
                $this->removeMapSubEntry('e', $extension, 't', $alias);
277
            }
278
        }
279
280 2
        $type_ret = $this->removeMapSubEntry('t', $type, 'a', $alias);
281 2
        $alias_ret = $this->removeMapSubEntry('a', $alias, 't', $type);
282
283 2
        return $type_ret && $alias_ret;
284
    }
285
286
    /**
287
     * Removes a type-to-extension mapping.
288
     *
289
     * @param string $type
290
     *   A MIME type.
291
     * @param string $extension
292
     *   The file extension to be removed.
293
     *
294
     * @return bool
295
     *   true if the mapping was removed, false if the mapping was not present.
296
     */
297 4
    public function removeTypeExtensionMapping($type, $extension)
298
    {
299 4
        $type = strtolower($type);
300 4
        $extension = strtolower($extension);
301
302 4
        if ($this->hasAlias($type)) {
303 1
            $alias = $type;
304 1
            $type_ret = $this->removeMapSubEntry('a', $alias, 'e', $extension);
305 1
            $extension_ret = $this->removeMapSubEntry('e', $extension, 't', $alias);
306
        } else {
307 3
            $this->removeAliasedTypesExtensionMapping($type, $extension);
308 3
            $type_ret = $this->removeMapSubEntry('t', $type, 'e', $extension);
309 3
            $extension_ret = $this->removeMapSubEntry('e', $extension, 't', $type);
310
        }
311
312 4
        return $type_ret && $extension_ret;
313
    }
314
315
    /**
316
     * Removes aliased types extension mapping.
317
     *
318
     * @param string $type
319
     *   A MIME type.
320
     * @param string $extension
321
     *   The file extension to be removed.
322
     */
323 3
    protected function removeAliasedTypesExtensionMapping($type, $extension)
324
    {
325 3
        foreach ($this->getExtensionTypes($extension) as $associated_type) {
326 3
            if ($this->hasAlias($associated_type) && $type === $this->getAliasTypes($associated_type)[0]) {
327 2
                $this->removeMapSubEntry('a', $associated_type, 'e', $extension);
328 3
                $this->removeMapSubEntry('e', $extension, 't', $associated_type);
329
            }
330
        }
331 3
    }
332
333
    /**
334
     * Gets the parent types of an alias.
335
     *
336
     * @param string $alias The alias to be found.
337
     *
338
     * @return string[]
339
     */
340 7
    public function getAliasTypes($alias)
341
    {
342 7
        return $this->getMapSubEntry('a', $alias, 't') ?: [];
343
    }
344
345
    /**
346
     * Gets the content of an entry from the 'extensions' array.
347
     *
348
     * @param string $extension The extension to be found.
349
     *
350
     * @return string[] The mapped MIME types.
351
     */
352 12
    public function getExtensionTypes($extension)
353
    {
354 12
        return $this->getMapSubEntry('e', $extension, 't') ?: [];
355
    }
356
357
    /**
358
     * Changes the default MIME type for a file extension.
359
     *
360
     * Allows a MIME type alias to be set as default for the extension.
361
     *
362
     * @param string $extension
363
     *   A file extension.
364
     * @param string $type
365
     *   A MIME type.
366
     *
367
     * @throws MappingException if no mapping found.
368
     *
369
     * @return $this
370
     */
371 7
    public function setExtensionDefaultType($extension, $type)
372
    {
373 7
        $type = strtolower($type);
374 7
        $extension = strtolower($extension);
375
376 7
        if ($this->hasAlias($type)) {
377 4
            $alias = $type;
378 4
            $type = $this->getAliasTypes($alias)[0];
379
            // Check that the alias is referring to a type associated with that
380
            // extension.
381 4
            $associated_types = $this->getMapSubEntry('e', $extension, 't') ?: [];
382 4
            if (!in_array($type, $associated_types)) {
383 1
                throw new MappingException("Cannot set '{$alias}' as default type for extension '{$extension}', its unaliased type '{$type}' is not associated to '{$extension}'");
384
            }
385 3
            $this->addMapSubEntry('a', $alias, 'e', $extension);
386 3
            $this->addMapSubEntry('e', $extension, 't', $alias);
387 3
            return $this->setValueAsDefault('e', $extension, 't', $alias);
388
        } else {
389 3
            return $this->setValueAsDefault('e', $extension, 't', $type);
390
        }
391
    }
392
}
393