Files
esun3d/app/code/Esun3d/LanguageUrlStoreResolver/Model/LanguageResolver.php
T
2026-09-15 10:35:56 +08:00

55 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun3d\LanguageUrlStoreResolver\Model;
use Magento\Store\Model\StoreManagerInterface;
class LanguageResolver
{
/**
* @var StoreViewRegistry
*/
private $storeViewRegistry;
/** @var StoreManagerInterface */
private $storeManager;
public function __construct(StoreViewRegistry $storeViewRegistry, StoreManagerInterface $storeManager)
{
$this->storeViewRegistry = $storeViewRegistry;
$this->storeManager = $storeManager;
}
public function resolveFromPath(string $pathInfo): ?string
{
$firstSegment = $this->getFirstSegment($pathInfo);
if ($firstSegment === null) {
return null;
}
$language = strtolower($firstSegment);
$allowed = $this->storeViewRegistry->isLanguageInWebsite(
$language,
(int)$this->storeManager->getWebsite()->getId()
);
return $allowed ? $language : null;
}
private function getFirstSegment(string $pathInfo): ?string
{
$pathInfo = trim($pathInfo, '/');
if ($pathInfo === '') {
return null;
}
$segments = explode('/', $pathInfo);
return $segments[0] !== '' ? $segments[0] : null;
}
}