55 lines
1.2 KiB
PHP
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;
|
|
}
|
|
}
|