public function testSortByPriorityWithChromeAcceptLanguageString() { $str = 'de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4'; $result = Utilities::sortByPriority($str); $this->assertInternalType('array', $result); $this->assertArrayHasKey('de-de', $result); $this->assertArrayHasKey('de', $result); $this->assertArrayHasKey('en-us', $result); $this->assertArrayHasKey('en', $result); $this->assertEquals(1, $result['de-de']); $this->assertEquals(0.8, $result['de']); $this->assertEquals(0.6, $result['en-us']); $this->assertEquals(0.4, $result['en']); }
/** * 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; }