/** * @param MvcEvent $event * * @return MvcEvent */ public function __invoke(MvcEvent $event) { if (!($result = $this->langRecognizer->recognize($event))) { return false; } list($lang, $newLocale, $routeMatchLang) = $result; if ($result = $this->langRedirector->checkRedirect($event, $lang, $routeMatchLang)) { return $event; } \Locale::setDefault($newLocale); /** @var Translator $translator */ $translator = $this->mvcTranslator->getTranslator(); $translator->setLocale($newLocale); AbstractValidator::setDefaultTranslator($this->mvcTranslator); return $event; }
/** * @dataProvider dataProviderForTestCheckRedirect_WhenShouldRedirectToRecognizedLanguageIsEnabled * * @param string $recognizedLang * @param string $routeMatchLang * @param string $routeMatchLangAfterRedirect * @param array $expectedResult */ public function testCheckRedirect_WhenShouldRedirectToRecognizedLanguageIsEnabled($recognizedLang, $routeMatchLang, $routeMatchLangAfterRedirect, $expectedResult) { $this->prepareTestedObject($this->prepareConfig(true)); $paramsBefore = ['__NAMESPACE__' => 'Module\\Some', 'controller' => 'TestController', 'action' => 'index', 'lang' => $routeMatchLang ? $routeMatchLang : '', '__CONTROLLER__' => 'test']; $paramsAfter = ['__NAMESPACE__' => 'Module\\Some', 'controller' => 'test', 'action' => null, 'lang' => $routeMatchLangAfterRedirect, '__CONTROLLER__' => 'test']; $routeMatch = new RouteMatch($paramsBefore); $matchedRouteName = 'some\\test'; $routeMatch->setMatchedRouteName($matchedRouteName); $event = new MvcEvent(); $event->setResponse(new Response()); $event->setRouteMatch($routeMatch); $uriLangPrefix = $routeMatchLangAfterRedirect ? '/' . $routeMatchLangAfterRedirect : ''; $expectedUri = $uriLangPrefix . '/some/test'; if ($expectedResult) { $this->routeMock->expects($this->once())->method('assemble')->with($paramsAfter, ['name' => $matchedRouteName])->willReturn($expectedUri); } $event->setRouter($this->routeMock); $result = $this->testedObject->checkRedirect($event, $recognizedLang, $routeMatchLang); $this->assertSame($expectedResult, $result); }