init
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user