Files
2026-09-15 10:35:56 +08:00

77 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun3d\LanguageUrlStoreResolver\Model;
use Magento\Framework\App\RequestInterface;
class MarketResolver
{
/**
* @var MarketCookie
*/
private $marketCookie;
/**
* @var StoreCodeMapper
*/
private $storeCodeMapper;
public function __construct(MarketCookie $marketCookie, StoreCodeMapper $storeCodeMapper)
{
$this->marketCookie = $marketCookie;
$this->storeCodeMapper = $storeCodeMapper;
}
public function resolve(RequestInterface $request): ?string
{
$requestMarket = $this->normalizeMarket($request->getParam('market'));
if ($requestMarket !== null) {
return $requestMarket;
}
$cookieMarket = $this->normalizeMarket($this->marketCookie->get());
if ($cookieMarket !== null) {
return $cookieMarket;
}
return null;
}
public function resolveForLanguage(RequestInterface $request, string $language): ?string
{
$requestMarket = $this->normalizeMarketForLanguage($request->getParam('market'), $language);
if ($requestMarket !== null) {
return $requestMarket;
}
$cookieMarket = $this->normalizeMarketForLanguage($this->marketCookie->get(), $language);
if ($cookieMarket !== null) {
return $cookieMarket;
}
return $this->storeCodeMapper->getDefaultStoreCodeForLanguage($language);
}
public function normalizeMarket($market): ?string
{
if (!is_scalar($market)) {
return null;
}
$market = trim((string)$market);
return $this->storeCodeMapper->isRealStoreCodeInScope($market) ? $market : null;
}
public function normalizeMarketForLanguage($market, string $language): ?string
{
$market = $this->normalizeMarket($market);
if ($market === null) {
return null;
}
return $this->storeCodeMapper->languageFromStoreCode($market) === strtolower($language) ? $market : null;
}
}