/** * Determine the requested format by the API client * * We have basically two ways requesting an output format * 1. The client tells us the requsted format within the URL like /controller/action.format * 2. The client send the Accept: header * * The order is important only if the client specifies both. If so, the 1. varient (the URL dot syntax) * has precedence * * @return string Client requested output format */ public function getFormat() { if ($this->format !== null) { return $this->format; } $format = $this->default_format; if (isset($_SERVER['HTTP_ACCEPT'])) { $accept_header = Utilities::sortByPriority($_SERVER['HTTP_ACCEPT']); foreach ($accept_header as $mime_type => $priority) { if (Format::isMimeTypeSupported($mime_type)) { $format = $mime_type; break; } } } // Check for trailing dot-format syntax like /controller/action.format -> action.json $override = ''; if (isset($_SERVER['REQUEST_URI']) && preg_match('/\\.(\\w+)($|\\?)/i', $_SERVER['REQUEST_URI'], $matches)) { $override = $matches[1]; } if (Format::getMimeTypeFromFormatAbbreviation($override)) { $format = Format::getMimeTypeFromFormatAbbreviation($override); } $this->format = $format; return $format; }
/** * @covers JK\RestServer\Format::getMimeTypeFromFormatAbbreviation */ public function testGetMimeTypeFromFormatNonExistentAbbreviation() { $result = Format::getMimeTypeFromFormatAbbreviation('non-existent'); $this->assertFalse($result); }