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 | 'ogg' => ['audio/ogg'], |
||
46 | 'ogv' => ['video/ogg'], |
||
47 | 'webm' => ['video/webm'], |
||
48 | 'webp' => ['image/webp'], |
||
49 | 'svg' => ['image/svg+xml'], |
||
50 | 'zip' => ['application/zip', 'application/x-zip', 'application/x-zip-compressed'], |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * Returns the format. |
||
55 | * |
||
56 | * @param ServerRequestInterface $request |
||
57 | * |
||
58 | * @return string|null |
||
59 | */ |
||
60 | public static function getFormat(ServerRequestInterface $request) |
||
64 | |||
65 | /** |
||
66 | * Add a new format. |
||
67 | * |
||
68 | * @param string $format |
||
69 | * @param array $mimeTypes |
||
70 | * |
||
71 | * @return self |
||
72 | */ |
||
73 | public function addFormat($format, array $mimeTypes) |
||
79 | |||
80 | /** |
||
81 | * Set the default format. |
||
82 | * |
||
83 | * @param string $format |
||
84 | * |
||
85 | * @return self |
||
86 | */ |
||
87 | public function defaultFormat($format) |
||
93 | |||
94 | /** |
||
95 | * Execute the middleware. |
||
96 | * |
||
97 | * @param ServerRequestInterface $request |
||
98 | * @param ResponseInterface $response |
||
99 | * @param callable $next |
||
100 | * |
||
101 | * @return ResponseInterface |
||
102 | */ |
||
103 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
114 | |||
115 | /** |
||
116 | * Returns the format using the file extension. |
||
117 | * |
||
118 | * @return null|string |
||
119 | */ |
||
120 | private function getFromExtension(ServerRequestInterface $request) |
||
126 | |||
127 | /** |
||
128 | * Returns the format using the Accept header. |
||
129 | * |
||
130 | * @return null|string |
||
131 | */ |
||
132 | private function getFromHeader(ServerRequestInterface $request) |
||
144 | } |
||
145 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: