MimeDetector::getExtension()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace IQParts\Content;
4
5
/**
6
 * Class MimeDetector
7
 * @package IQParts\Framework\Content
8
 */
9
final class MimeDetector
10
{
11
    /**
12
     * @var
13
     */
14
    private $mimeTypes;
15
16
    /**
17
     * Factory constructor.
18
     * @param array $extraMimes
19
     */
20
    public function __construct(array $extraMimes = [])
21
    {
22
        $this->mimeTypes = array_merge(
23
            [
24
                'doc' => 'application/msword',
25
                'dot' => 'application/msword',
26
                'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
27
                'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
28
                'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
29
                'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
30
                'xls' => 'application/vnd.ms-excel',
31
                'xlt' => 'application/vnd.ms-excel',
32
                'xla' => 'application/vnd.ms-excel',
33
                'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
34
                'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
35
                'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
36
                'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
37
                'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
38
                'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
39
                'ppt' => 'application/vnd.ms-powerpoint',
40
                'pot' => 'application/vnd.ms-powerpoint',
41
                'pps' => 'application/vnd.ms-powerpoint',
42
                'ppa' => 'application/vnd.ms-powerpoint',
43
                'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
44
                'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
45
                'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
46
                'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
47
                'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
48
                'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
49
                'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
50
                'mdb' => 'application/vnd.ms-access',
51
                'aac' => 'audio/aac',
52
                'abw' => 'application/x-abiword',
53
                'arc' => 'application/octet-stream',
54
                'avi' => 'video/x-msvideo',
55
                'azw' => 'application/vnd.amazon.ebook',
56
                'bin' => 'application/octet-stream',
57
                'bz' => 'application/x-bzip',
58
                'bz2' => 'application/x-bzip2',
59
                'csh' => 'application/x-csh',
60
                'css' => 'text/css',
61
                'csv' => 'text/csv',
62
                'eot' => 'application/vnd.ms-fontobject',
63
                'epub' => 'application/epub+zip',
64
                'gif' => 'image/gif',
65
                'htm' => 'text/html',
66
                'html' => 'text/html',
67
                'twig' => 'text/html',
68
                'ico' => 'image/x-icon',
69
                'ics' => 'text/calendar',
70
                'jar' => 'application/java-archive',
71
                'jpeg' => 'image/jpeg',
72
                'jpg' => 'image/jpg',
73
                'js' => 'application/javascript',
74
                'json' => 'application/json',
75
                'mid' => 'audio/midi',
76
                'midi' => 'audio/midi',
77
                'mpeg' => 'video/mpeg',
78
                'mpkg' => 'application/vnd.apple.installer+xml',
79
                'odp' => 'application/vnd.oasis.opendocument.presentation',
80
                'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
81
                'odt' => 'application/vnd.oasis.opendocument.text',
82
                'oga' => 'audio/ogg',
83
                'ogv' => 'video/ogg',
84
                'ogx' => 'application/ogg',
85
                'otf' => 'font/otf',
86
                'png' => 'image/png',
87
                'pdf' => 'application/pdf',
88
                'rar' => 'application/x-rar-compressed',
89
                'rtf' => 'application/rtf',
90
                'sh' => 'application/x-sh',
91
                'svg' => 'image/svg+xml',
92
                'swf' => 'application/x-shockwave-flash',
93
                'tar' => 'application/x-tar',
94
                'tif' => 'image/tiff',
95
                'tiff' => 'image/tiff',
96
                'ts' => 'application/typescript',
97
                'txt' => 'text/plain',
98
                'ttf' => 'font/ttf',
99
                'vsd' => 'application/vnd.visio',
100
                'wav' => 'audio/x-wav',
101
                'weba' => 'audio/webm',
102
                'webm' => 'video/webm',
103
                'webp' => 'image/webp',
104
                'woff' => 'font/woff',
105
                'woff2' => 'font/woff2',
106
                'xhtml' => 'application/xhtml+xml',
107
                'xml' => 'application/xml',
108
                'xul' => 'application/vnd.mozilla.xul+xml',
109
                'zip' => 'application/zip',
110
                '3gp' => 'video/3gpp',
111
                '3g2' => 'video/3gpp2',
112
                '7z' => 'application/x-7z-compressed'
113
            ],
114
            $extraMimes
115
        );
116
    }
117
118
    /**
119
     * @param string $filename
120
     * @return string
121
     */
122
    public function detectFromExtension(string $filename): string
123
    {
124
        return $this->mimeTypes[$this->getExtension($filename)] ?? 'text/plain';
125
    }
126
127
    /**
128
     * @param string $filename
129
     * @return bool
130
     */
131
    public function isText(string $filename): bool
132
    {
133
        $mime = $this->detectFromExtension($filename);
134
        if (fnmatch('text/*', $mime)) {
135
            return true;
136
        }
137
138
        $text = [
139
            "application/javascript" => true,
140
            "application/rtf" => true,
141
            "application/json" => true,
142
            "application/x-sh" => true,
143
            "application/xml" => true
144
        ];
145
        return isset($text[$mime]);
146
    }
147
148
    /**
149
     * @param $filename
150
     * @return string
151
     */
152
    private function getExtension($filename): string
153
    {
154
        $file = basename($filename);
155
        $dotPlace = strrpos($file, '.');
156
        if ($dotPlace === false) {
157
            return $file;
158
        }
159
        return substr($file, $dotPlace + 1);
160
    }
161
162
}