This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace Esun3d\LanguageUrlStoreResolver\Model;
class RequestPathProcessor
{
/**
* @var LanguageResolver
*/
private $languageResolver;
public function __construct(LanguageResolver $languageResolver)
{
$this->languageResolver = $languageResolver;
}
public function stripLanguagePrefix(string $pathInfo): string
{
if ($this->languageResolver->resolveFromPath($pathInfo) === null) {
return $this->normalizePath($pathInfo);
}
$hasTrailingSlash = substr($pathInfo, -1) === '/';
$pathInfo = '/' . ltrim($pathInfo, '/');
$segments = explode('/', trim($pathInfo, '/'), 2);
$remainingPath = $segments[1] ?? '';
if ($remainingPath === '') {
return '/';
}
$path = '/' . ltrim($remainingPath, '/');
return $hasTrailingSlash && substr($path, -1) !== '/' ? $path . '/' : $path;
}
public function replaceLanguagePrefix(string $pathInfo, string $language): string
{
$remainingPath = $this->stripLanguagePrefix($pathInfo);
$language = strtolower(trim($language));
if ($remainingPath === '/') {
return '/' . $language . '/';
}
return '/' . $language . $remainingPath;
}
private function normalizePath(string $pathInfo): string
{
$pathInfo = '/' . ltrim($pathInfo, '/');
return $pathInfo === '' ? '/' : $pathInfo;
}
}