init
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun3d\LanguageUrlStoreResolver\Model;
|
||||
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class StoreCodeMapper
|
||||
{
|
||||
/**
|
||||
* @var StoreViewRegistry
|
||||
*/
|
||||
private $storeViewRegistry;
|
||||
|
||||
private $storeManager;
|
||||
|
||||
public function __construct(StoreViewRegistry $storeViewRegistry, StoreManagerInterface $storeManager)
|
||||
{
|
||||
$this->storeViewRegistry = $storeViewRegistry;
|
||||
$this->storeManager = $storeManager;
|
||||
}
|
||||
|
||||
public function map(string $language, ?string $market = null): ?string
|
||||
{
|
||||
$language = strtolower(trim($language));
|
||||
|
||||
if ($market !== null
|
||||
&& $this->isRealStoreCodeInScope($market)
|
||||
&& $this->languageFromStoreCode($market) === $language
|
||||
) {
|
||||
return $market;
|
||||
}
|
||||
|
||||
return $this->getDefaultStoreCodeForLanguage($language);
|
||||
}
|
||||
|
||||
public function languageFromStoreCode(string $storeCode): ?string
|
||||
{
|
||||
return $this->storeViewRegistry->getLanguageByStoreCode($storeCode);
|
||||
}
|
||||
|
||||
public function marketFromStoreCode(string $storeCode): ?string
|
||||
{
|
||||
return $this->isRealStoreCodeInScope($storeCode) ? $storeCode : null;
|
||||
}
|
||||
|
||||
public function isRealStoreCodeInScope(string $storeCode): bool
|
||||
{
|
||||
return $this->storeViewRegistry->isStoreCodeInWebsite($storeCode, $this->getWebsiteId());
|
||||
}
|
||||
|
||||
public function isLanguageAllowed(string $language): bool
|
||||
{
|
||||
return $this->storeViewRegistry->isLanguageInWebsite($language, $this->getWebsiteId());
|
||||
}
|
||||
|
||||
public function getDefaultStoreCodeForLanguage(string $language): ?string
|
||||
{
|
||||
return $this->storeViewRegistry->getStoreCodeForLanguageInWebsite($language, $this->getWebsiteId());
|
||||
}
|
||||
|
||||
public function getDefaultLanguage(): ?string
|
||||
{
|
||||
try {
|
||||
$defaultStore = $this->storeManager->getWebsite()->getDefaultStore();
|
||||
return $defaultStore ? $this->languageFromStoreCode((string)$defaultStore->getCode()) : null;
|
||||
} catch (\Throwable $exception) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function isDefaultLanguage(string $language): bool
|
||||
{
|
||||
return strtolower(trim($language)) === $this->getDefaultLanguage();
|
||||
}
|
||||
|
||||
private function getWebsiteId(): int
|
||||
{
|
||||
return (int)$this->storeManager->getWebsite()->getId();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user