public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
$language = null;
$uri = $request->getUri();
//Use path
if ($this->usePath) {
$path = ltrim($uri->getPath(), '/');
$dirs = explode('/', $path, 2);
$first = strtolower(array_shift($dirs));
if (!empty($first) && in_array($first, $this->languages, true)) {
$language = $first;
//remove the language in the path
$request = $request->withUri($uri->withPath('/' . array_shift($dirs)));
}
}
//Use http headers
if ($language === null) {
$language = $this->negotiateHeader($request->getHeaderLine('Accept-Language'), new Negotiator(), $this->languages);
if (empty($language)) {
$language = isset($this->languages[0]) ? $this->languages[0] : '';
}
//Redirect to a path with the language
if ($this->redirectStatus !== false && $this->usePath) {
$path = Utils\Helpers::joinPath($language, $uri->getPath());
return $this->getRedirectResponse($request, $uri->withPath($path), $response);
}
}
$response = $next(self::setAttribute($request, self::KEY, $language), $response->withHeader('Content-Language', $language));
if (!$response->hasHeader('Content-Language')) {
$response = $response->withHeader('Content-Language', $language);
}
return $response;
}