public function setUp() { $this->strategy = new QueryStrategy(); $this->event = new LocaleEvent(); $request = new HttpRequest(); $response = new HttpResponse(); $this->event->setRequest($request); $this->event->setResponse($response); }
public function detect(RequestInterface $request, ResponseInterface $response = null) { $event = new LocaleEvent(LocaleEvent::EVENT_DETECT, $this); $event->setRequest($request); $event->setResponse($response); if ($this->hasSupported()) { $event->setSupported($this->getSupported()); } $events = $this->getEventManager(); $results = $events->trigger($event, function ($r) { return is_string($r); }); if ($results->stopped()) { $locale = $results->last(); } else { $locale = $this->getFromIP(); if ($locale == null) { $locale = $this->getDefault(); } } //manage all local format from navigator $locale = substr($locale, 0, 2); if ($this->hasSupported() && !in_array($locale, $this->getSupported())) { $locale = $this->getFromIP(); if ($locale == null) { $locale = $this->getDefault(); } } // Trigger FOUND event only when a response is given if ($response instanceof ResponseInterface) { $event->setName(LocaleEvent::EVENT_FOUND); $event->setLocale($locale); $return = false; /** * The response will be returned instead of the found locale * only in case a strategy returned the response. This is an * indication the strategy has updated the response (e.g. with * a Location header) and as such, the response must be returned * instead of the locale. */ $events->trigger($event, function ($r) use(&$return) { if ($r instanceof ResponseInterface) { $return = true; } }); if ($return) { return $response; } } return $locale; }