1 | <?php |
||
14 | class FormatNegotiator |
||
15 | { |
||
16 | use Utils\NegotiateTrait; |
||
17 | |||
18 | const KEY = 'FORMAT'; |
||
19 | |||
20 | /** |
||
21 | * @var string Default format |
||
22 | */ |
||
23 | private $default = 'html'; |
||
24 | |||
25 | /** |
||
26 | * @var array Available formats with the mime types |
||
27 | */ |
||
28 | private $formats = [ |
||
29 | 'html' => ['text/html', 'application/xhtml+xml'], |
||
30 | 'css' => ['text/css'], |
||
31 | 'gif' => ['image/gif'], |
||
32 | 'png' => ['image/png', 'image/x-png'], |
||
33 | 'jpg' => ['image/jpeg', 'image/jpg'], |
||
34 | 'jpeg' => ['image/jpeg', 'image/jpg'], |
||
35 | 'json' => ['application/json', 'text/json', 'application/x-json'], |
||
36 | 'jsonp' => ['text/javascript', 'application/javascript', 'application/x-javascript'], |
||
37 | 'js' => ['text/javascript', 'application/javascript', 'application/x-javascript'], |
||
38 | 'pdf' => ['application/pdf', 'application/x-download'], |
||
39 | 'rdf' => ['application/rdf+xml'], |
||
40 | 'rss' => ['application/rss+xml'], |
||
41 | 'atom' => ['application/atom+xml'], |
||
42 | 'xml' => ['text/xml', 'application/xml', 'application/x-xml'], |
||
43 | 'txt' => ['text/plain'], |
||
44 | 'mp4' => ['video/mp4'], |
||
45 | 'otf' => ['font/opentype'], |
||
46 | 'ttf' => ['font/ttf', 'application/font-ttf', 'application/x-font-ttf'], |
||
47 | 'ogg' => ['audio/ogg'], |
||
48 | 'ogv' => ['video/ogg'], |
||
49 | 'webm' => ['video/webm'], |
||
50 | 'woff' => ['font/woff', 'application/font-woff', 'application/x-font-woff'], |
||
51 | 'webp' => ['image/webp'], |
||
52 | 'svg' => ['image/svg+xml'], |
||
53 | 'zip' => ['application/zip', 'application/x-zip', 'application/x-zip-compressed'], |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * Returns the format. |
||
58 | * |
||
59 | * @param ServerRequestInterface $request |
||
60 | * |
||
61 | * @return string|null |
||
62 | */ |
||
63 | public static function getFormat(ServerRequestInterface $request) |
||
67 | |||
68 | /** |
||
69 | * Add a new format. |
||
70 | * |
||
71 | * @param string $format |
||
72 | * @param array $mimeTypes |
||
73 | * |
||
74 | * @return self |
||
75 | */ |
||
76 | public function addFormat($format, array $mimeTypes) |
||
82 | |||
83 | /** |
||
84 | * Set the default format. |
||
85 | * |
||
86 | * @param string $format |
||
87 | * |
||
88 | * @return self |
||
89 | */ |
||
90 | public function defaultFormat($format) |
||
96 | |||
97 | /** |
||
98 | * Execute the middleware. |
||
99 | * |
||
100 | * @param ServerRequestInterface $request |
||
101 | * @param ResponseInterface $response |
||
102 | * @param callable $next |
||
103 | * |
||
104 | * @return ResponseInterface |
||
105 | */ |
||
106 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
122 | |||
123 | /** |
||
124 | * Returns the format using the file extension. |
||
125 | * |
||
126 | * @return null|string |
||
127 | */ |
||
128 | private function getFromExtension(ServerRequestInterface $request) |
||
134 | |||
135 | /** |
||
136 | * Returns the format using the Accept header. |
||
137 | * |
||
138 | * @return null|string |
||
139 | */ |
||
140 | private function getFromHeader(ServerRequestInterface $request) |
||
152 | } |
||
153 |