Skip to content

Commit

Permalink
Simplify the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Apr 18, 2023
1 parent 4b3602a commit ff3ca76
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
16 changes: 5 additions & 11 deletions src/Locale.php
Expand Up @@ -47,17 +47,6 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$this->setLocale($language);
}

if ($path === '/') {
$request = $request->withUri($uri->withPath('/'));
}

if (
($path === "/$this->defaultLanguage" || $path === "/$this->defaultLanguage/") &&
$request->getMethod() === Method::GET
) {
return $handler->handle($request->withUri($uri->withPath('/')));
}

if ($language === $this->defaultLanguage) {
$this->urlGenerator->setDefaultArgument($this->localeArgument, '');
$request = $request->withUri($uri->withPath($this->getUrlPathWithLanguage($path, $language)));
Expand Down Expand Up @@ -117,6 +106,11 @@ private function getLanguage(string $path): string
*/
private function getUrlPathWithLanguage(string $path, string $language): string
{

if ($path === "/$language" || $path === '') {
return "/$language/";
}

if (str_contains($path, "/$language")) {
return $path;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Locale/MiddlewareTest.php
Expand Up @@ -106,7 +106,7 @@ public function testProcessRedirectWithPathRootLanguage(): void

$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertSame(302, $response->getStatusCode());
$this->assertSame('/', $response->getHeaderLine('Location'));
$this->assertSame('/en/', $response->getHeaderLine('Location'));
$this->assertSame('en', $this->translator->getLocale());
}

Expand Down Expand Up @@ -135,7 +135,7 @@ public function testProcessRedirectWithPathRootLanguageWithMethodPost(): void

$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertSame(302, $response->getStatusCode());
$this->assertSame('/en', $response->getHeaderLine('Location'));
$this->assertSame('/en/', $response->getHeaderLine('Location'));
$this->assertSame('en', $this->translator->getLocale());
}

Expand All @@ -147,7 +147,7 @@ public function testProcessReturnsResponseInterface(): void
$response = $locale->process($this->request, $this->handler);

$this->assertSame(302, $response->getStatusCode());
$this->assertSame('/en', $response->getHeaderLine('Location'));
$this->assertSame('/en/', $response->getHeaderLine('Location'));
$this->assertSame('en', $this->translator->getLocale());
}

Expand Down Expand Up @@ -257,7 +257,7 @@ public function testWithDefaultLocale(): void
$response = $locale->process($this->request, $this->handler);

$this->assertSame(302, $response->getStatusCode());
$this->assertSame('/ru', $response->getHeaderLine('Location'));
$this->assertSame('/ru/', $response->getHeaderLine('Location'));
$this->assertSame('ru', $this->translator->getLocale());
}

Expand Down

0 comments on commit ff3ca76

Please sign in to comment.