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