1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Knp\FriendlyContexts\Http; |
4
|
|
|
|
5
|
|
|
class HttpContentTypeGuesser |
6
|
|
|
{ |
7
|
|
|
private static $contentTypeTable = [ |
8
|
|
|
// application and text types |
9
|
|
|
'json' => 'application/json', |
10
|
|
|
'javascript' => ['application/javascript', 'text/javascript'], |
11
|
|
|
'xml' => ['application/xml', 'text/xml'], |
12
|
|
|
'rss' => 'application/rss', |
13
|
|
|
'pdf' => 'application/pdf', |
14
|
|
|
'soap' => 'application/soap+xml', |
15
|
|
|
'atom' => 'application/atom+xml', |
16
|
|
|
'stream' => 'application/octet-stream', |
17
|
|
|
'xhtml' => ['application/xhtml+xml', 'text/html'], |
18
|
|
|
'html' => ['application/xhtml+xml', 'text/html'], |
19
|
|
|
'zip' => 'application/zip', |
20
|
|
|
'gzip' => 'application/gzip', |
21
|
|
|
'font' => 'application/font-woff', |
22
|
|
|
'dtd' => 'applciation/xml-dtd', |
23
|
|
|
'ecmascript' => 'application/ecmascript', |
24
|
|
|
'postscript' => 'application/postscript', |
25
|
|
|
'cmd' => 'text/cmd', |
26
|
|
|
'css' => 'text/css', |
27
|
|
|
'csv' => 'text/csv', |
28
|
|
|
'plaintext' => 'text/plain', |
29
|
|
|
'text' => 'text/plain', |
30
|
|
|
'rtf' => 'text/rtf', |
31
|
|
|
'vcard' => 'text/vcard', |
32
|
|
|
'abc' => 'text/vnd.abc', |
33
|
|
|
// audio and video types |
34
|
|
|
'ogg' => ['application/ogg', 'audio/ogg', 'video/ogg'], |
35
|
|
|
'l24' => 'audio/L24', |
36
|
|
|
'mp4' => ['audio/mp4', 'video/mp4'], |
37
|
|
|
'mpeg' => ['audio/mpeg', 'video/mpeg'], |
38
|
|
|
'opus' => 'audio/opus', |
39
|
|
|
'vorbis' => 'audio/vorbis', |
40
|
|
|
'realaudio' => 'audio/vnd.rn-realaudio', |
41
|
|
|
'wave' => 'audio/vnd.wave', |
42
|
|
|
'webm' => ['audio/webm', 'video/webm'], |
43
|
|
|
'avi' => 'video/avi', |
44
|
|
|
'quicktime' => 'video/quicktime', |
45
|
|
|
'wmv' => 'video/x-ms-wmv', |
46
|
|
|
'matroska' => 'video/x-matroska', |
47
|
|
|
'flv' => 'video/x-flv', |
48
|
|
|
// image types |
49
|
|
|
'gif' => 'image/gif', |
50
|
|
|
'jpeg' => 'image/jpeg', |
51
|
|
|
'pjpeg' => 'image/pjpeg', |
52
|
|
|
'png' => 'image/png', |
53
|
|
|
'svg' => 'image/svg+xml', |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
public function guess($shortType) |
57
|
|
|
{ |
58
|
|
|
$shortType = strtolower($shortType); |
59
|
|
|
|
60
|
|
|
if (!isset(self::$contentTypeTable[$shortType])) { |
61
|
|
|
throw new \InvalidArgumentException(sprintf( |
62
|
|
|
'No short content type has been found for "%s"', |
63
|
|
|
$shortType |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (!is_array(self::$contentTypeTable[$shortType])) { |
68
|
|
|
return [self::$contentTypeTable[$shortType]]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return self::$contentTypeTable[$shortType]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function exists($shortType) |
75
|
|
|
{ |
76
|
|
|
$shortType = strtolower($shortType); |
77
|
|
|
|
78
|
|
|
return isset(self::$contentTypeTable[$shortType]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|