/** * Execute the middleware * * @param ServerRequestInterface $request * @param ResponseInterface $response * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $negotiator = new Negotiator(); $language = $negotiator->getBest($request->getHeaderLine('Accept-Language'), $this->languages); if ($language) { $language = strtolower(substr($language->getValue(), 0, 2)); } else { $language = isset($this->languages[0]) ? $this->languages[0] : null; } return $next($request->withAttribute('LANGUAGE', $language), $response); }
/** * @dataProvider dataProviderForTestGetBest */ public function testGetBest($accept, $priorities, $expected) { try { $accept = $this->negotiator->getBest($accept, $priorities); if (null === $accept) { $this->assertNull($expected); } else { $this->assertInstanceOf('Negotiation\\AcceptLanguage', $accept); $this->assertEquals($expected, $accept->getValue()); } } catch (\Exception $e) { $this->assertEquals($expected, $e); } }
/** * Handles a request to convert it to a response * * @param Request $request A Request instance * @param int $type The type of the request * @param bool $catch Whether to catch exceptions or not * * @return Response * * @throws Exception When an Exception occurs during processing */ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) { $acceptHeader = $request->headers->get('Accept'); if ($acceptHeader !== null && !empty($this->formatPriorities)) { $accept = $this->formatNegotiator->getBest($acceptHeader, $this->formatPriorities); $request->attributes->set('_accept', $accept); if ($accept !== null) { /** @var Accept $accept */ if (false === strpos($accept->getType(), '*')) { $mimeType = $accept->getType(); $format = $this->getFormat($mimeType); $request->attributes->set('_mime_type', $mimeType); $request->attributes->set('_format', $format); } } } $acceptLanguageHeader = $request->headers->get('Accept-Language'); if ($acceptLanguageHeader !== null && !empty($this->languagePriorities)) { $acceptLanguage = $this->languageNegotiator->getBest($acceptLanguageHeader, $this->languagePriorities); $request->attributes->set('_accept_language', $acceptLanguage); if ($acceptLanguage !== null) { /** @var AcceptLanguage $acceptLanguage */ $language = $acceptLanguage->getType(); $request->attributes->set('_language', $language); } } $acceptEncodingHeader = $request->headers->get('Accept-Encoding'); if ($acceptEncodingHeader !== null && !empty($this->encodingPriorities)) { $acceptEncoding = $this->encodingNegotiator->getBest($acceptEncodingHeader, $this->encodingPriorities); $request->attributes->set('_accept_encoding', $acceptEncoding); if ($acceptEncoding !== null) { /** @var AcceptEncoding $acceptEncoding */ $encoding = $acceptEncoding->getType(); $request->attributes->set('_encoding', $encoding); } } $acceptCharsetHeader = $request->headers->get('Accept-Charset'); if ($acceptCharsetHeader !== null && !empty($this->charsetPriorities)) { $acceptCharset = $this->charsetNegotiator->getBest($acceptCharsetHeader, $this->charsetPriorities); $request->attributes->set('_accept_charset', $acceptCharset); if ($acceptCharset !== null) { /** @var AcceptCharset $acceptCharset */ $charset = $acceptCharset->getType(); $request->attributes->set('_charset', $charset); } } try { $this->decodeBody($request); } catch (BadRequestHttpException $exception) { if ($catch === true) { return $this->handleException($exception, $request); } throw $exception; } return $this->kernel->handle($request, $type, $catch); }
public function testGetBestDoesNotMatchPriorities() { $acceptLanguageHeader = 'en, de'; $priorities = array('fr'); $this->assertNull($this->negotiator->getBest($acceptLanguageHeader, $priorities)); }
/** * * @param type $str */ public function setLocale($str = "") { $lang = strtolower($str == "" ? $this->getParameter("lang") : $str); $langAvail = array_keys($this->LanguagesAvailable()); // Get the languages to check if (!empty($str)) { $checkLang = array($str); } else { if (!empty($lang)) { $checkLang = array($lang); } else { $checkLang = $langAvail; } } // Language Negotiator $langNeg = new LanguageNegotiator(); $langAccepted = $langNeg->getBest(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : "en-us", $checkLang); if (is_null($langAccepted)) { $currentLanguage = !empty($str) ? $str : (!empty($lang) ? $lang : $langAvail[0]); } else { $currentLanguage = strtolower($langAccepted->getValue()); } // Get the correct language $selected = array_merge(preg_grep("/^" . $currentLanguage . "*/", $langAvail), preg_grep("/^" . substr($currentLanguage, 0, 2) . "*/", $langAvail)); if (count($selected) == 0) { $currentLanguage = $langAvail[0]; } else { $currentLanguage = array_shift($selected); } $this->_lang = LocaleFactory::GetLocale($currentLanguage); $this->addPairToConfig("LANGUAGE", $this->_lang->getName()); $this->addPairToConfig("LANGUAGENAME", $this->_lang->getLanguage()); }
public function testGetBestRespectsQualityOfSource() { $accept = $this->negotiator->getBest('en;q=0.5,de', array('de;q=0.3', 'en;q=0.9')); $this->assertInstanceOf('Negotiation\\AcceptLanguage', $accept); $this->assertEquals('en', $accept->getType()); }