Skip to content

Commit 6aecd69

Browse files
committed
Util::getExtension
1 parent 188ce0b commit 6aecd69

File tree

1 file changed

+132
-60
lines changed

1 file changed

+132
-60
lines changed

src/Util.php

Lines changed: 132 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,128 @@
3131
*/
3232

3333
class Util {
34+
35+
private static $_http_codes = [
36+
100 => 'Continue',
37+
101 => 'Switching Protocols',
38+
102 => 'Processing',
39+
200 => 'OK',
40+
201 => 'Created',
41+
202 => 'Accepted',
42+
203 => 'Non-Authoritative Information',
43+
204 => 'No Content',
44+
205 => 'Reset Content',
45+
206 => 'Partial Content',
46+
207 => 'Multi-Status',
47+
300 => 'Multiple Choices',
48+
301 => 'Moved Permanently',
49+
302 => 'Found',
50+
303 => 'See Other',
51+
304 => 'Not Modified',
52+
305 => 'Use Proxy',
53+
306 => 'Switch Proxy',
54+
307 => 'Temporary Redirect',
55+
400 => 'Bad Request',
56+
401 => 'Unauthorized',
57+
402 => 'Payment Required',
58+
403 => 'Forbidden',
59+
404 => 'Not Found',
60+
405 => 'Method Not Allowed',
61+
406 => 'Not Acceptable',
62+
407 => 'Proxy Authentication Required',
63+
408 => 'Request Timeout',
64+
409 => 'Conflict',
65+
410 => 'Gone',
66+
411 => 'Length Required',
67+
412 => 'Precondition Failed',
68+
413 => 'Request Entity Too Large',
69+
414 => 'Request-URI Too Long',
70+
415 => 'Unsupported Media Type',
71+
416 => 'Requested Range Not Satisfiable',
72+
417 => 'Expectation Failed',
73+
418 => 'I\'m a teapot',
74+
422 => 'Unprocessable Entity',
75+
423 => 'Locked',
76+
424 => 'Failed Dependency',
77+
425 => 'Unordered Collection',
78+
426 => 'Upgrade Required',
79+
429 => 'Too Many Requests',
80+
449 => 'Retry With',
81+
450 => 'Blocked by Windows Parental Controls',
82+
500 => 'Internal Server Error',
83+
501 => 'Not Implemented',
84+
502 => 'Bad Gateway',
85+
503 => 'Service Unavailable',
86+
504 => 'Gateway Timeout',
87+
505 => 'HTTP Version Not Supported',
88+
506 => 'Variant Also Negotiates',
89+
507 => 'Insufficient Storage',
90+
509 => 'Bandwidth Limit Exceeded',
91+
510 => 'Not Extended'
92+
];
93+
94+
private static $_mime_types = [
95+
'txt' => 'text/plain',
96+
'htm' => 'text/html',
97+
'html' => 'text/html',
98+
'php' => 'text/html',
99+
'css' => 'text/css',
100+
'js' => 'application/javascript',
101+
'json' => 'application/json',
102+
'xml' => 'application/xml',
103+
'swf' => 'application/x-shockwave-flash',
104+
'flv' => 'video/x-flv',
105+
106+
// images
107+
'png' => 'image/png',
108+
'jpe' => 'image/jpeg',
109+
'jpeg' => 'image/jpeg',
110+
'jpg' => 'image/jpeg',
111+
'gif' => 'image/gif',
112+
'bmp' => 'image/bmp',
113+
'ico' => 'image/vnd.microsoft.icon',
114+
'tiff' => 'image/tiff',
115+
'tif' => 'image/tiff',
116+
'svg' => 'image/svg+xml',
117+
'svgz' => 'image/svg+xml',
118+
119+
// archives
120+
'zip' => 'application/zip',
121+
'rar' => 'application/x-rar-compressed',
122+
'exe' => 'application/x-msdownload',
123+
'msi' => 'application/x-msdownload',
124+
'cab' => 'application/vnd.ms-cab-compressed',
125+
126+
// audio/video
127+
'mp3' => 'audio/mpeg',
128+
'qt' => 'video/quicktime',
129+
'mov' => 'video/quicktime',
130+
'wmv' => 'video/x-ms-wmv',
131+
'mp4' => 'video/mp4',
132+
'mp4a' => 'audio/mp4',
133+
'mpeg' => 'video/mpeg',
134+
135+
// adobe
136+
'pdf' => 'application/pdf',
137+
'psd' => 'image/vnd.adobe.photoshop',
138+
'ai' => 'application/postscript',
139+
'eps' => 'application/postscript',
140+
'ps' => 'application/postscript',
141+
'tiff' => 'image/tiff',
142+
143+
// ms office
144+
'doc' => 'application/msword',
145+
'rtf' => 'application/rtf',
146+
'xls' => 'application/vnd.ms-excel',
147+
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
148+
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
149+
'ppt' => 'application/vnd.ms-powerpoint',
150+
151+
// open office
152+
'odt' => 'application/vnd.oasis.opendocument.text',
153+
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
154+
];
155+
34156
/**
35157
* get HTTP code name
36158
* @param $code HTTP code
@@ -39,66 +161,16 @@ class Util {
39161
* code name
40162
*/
41163
public static function getHttpStatus($code) {
42-
$http_codes = [
43-
100 => 'Continue',
44-
101 => 'Switching Protocols',
45-
102 => 'Processing',
46-
200 => 'OK',
47-
201 => 'Created',
48-
202 => 'Accepted',
49-
203 => 'Non-Authoritative Information',
50-
204 => 'No Content',
51-
205 => 'Reset Content',
52-
206 => 'Partial Content',
53-
207 => 'Multi-Status',
54-
300 => 'Multiple Choices',
55-
301 => 'Moved Permanently',
56-
302 => 'Found',
57-
303 => 'See Other',
58-
304 => 'Not Modified',
59-
305 => 'Use Proxy',
60-
306 => 'Switch Proxy',
61-
307 => 'Temporary Redirect',
62-
400 => 'Bad Request',
63-
401 => 'Unauthorized',
64-
402 => 'Payment Required',
65-
403 => 'Forbidden',
66-
404 => 'Not Found',
67-
405 => 'Method Not Allowed',
68-
406 => 'Not Acceptable',
69-
407 => 'Proxy Authentication Required',
70-
408 => 'Request Timeout',
71-
409 => 'Conflict',
72-
410 => 'Gone',
73-
411 => 'Length Required',
74-
412 => 'Precondition Failed',
75-
413 => 'Request Entity Too Large',
76-
414 => 'Request-URI Too Long',
77-
415 => 'Unsupported Media Type',
78-
416 => 'Requested Range Not Satisfiable',
79-
417 => 'Expectation Failed',
80-
418 => 'I\'m a teapot',
81-
422 => 'Unprocessable Entity',
82-
423 => 'Locked',
83-
424 => 'Failed Dependency',
84-
425 => 'Unordered Collection',
85-
426 => 'Upgrade Required',
86-
429 => 'Too Many Requests',
87-
449 => 'Retry With',
88-
450 => 'Blocked by Windows Parental Controls',
89-
500 => 'Internal Server Error',
90-
501 => 'Not Implemented',
91-
502 => 'Bad Gateway',
92-
503 => 'Service Unavailable',
93-
504 => 'Gateway Timeout',
94-
505 => 'HTTP Version Not Supported',
95-
506 => 'Variant Also Negotiates',
96-
507 => 'Insufficient Storage',
97-
509 => 'Bandwidth Limit Exceeded',
98-
510 => 'Not Extended'
99-
];
100-
101-
return isset($http_codes[$code]) ? $http_codes[$code] : 'Unknown code';
164+
return self::get($code, self::$_http_codes, 'Unknown HTTP status code');
165+
}
166+
167+
/**
168+
* get file extension by mime type
169+
* @param string $mime_type Supported mime type
170+
* @return string The extension
171+
*/
172+
public static function getExtension($mime_type) {
173+
return array_search(strtolower($mime_type), self::$_mime_types);
102174
}
103175

104176
public static function get($field, $source = null, $default = null, $possible_values = []) {

0 commit comments

Comments
 (0)