Files
kupshop/bundles/KupShop/I18nBundle/Context/CountryContext.php
2025-08-02 16:30:27 +02:00

62 lines
1.7 KiB
PHP

<?php
namespace KupShop\I18nBundle\Context;
use Doctrine\ORM\EntityManagerInterface;
use KupShop\I18nBundle\Entity\Country;
use KupShop\KupShopBundle\Util\Functional\Mapping;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class CountryContext extends \KupShop\KupShopBundle\Context\CountryContext
{
/** @var EntityManagerInterface */
protected $entityManager;
/** @var Country */
protected $session;
public function __construct(EntityManagerInterface $entityManager, SessionInterface $session)
{
parent::__construct();
$this->entityManager = $entityManager;
$this->session = $session;
}
public function remember(string $id)
{
if ($this->validate($id)) {
$this->session->set('country', $id);
}
}
public function getAll()
{
if (!isset($this->countries)) {
$countries = $this->entityManager->getRepository(Country::class)->findBy([], ['name' => 'ASC']);
if ($collator = \Collator::create('cs_CZ')) {
// uvnitr metody getName provadime translate nazvu zemi a pokud to chceme mit serazeny abecedne, tak musim udelat sort tady
usort($countries, function (Country $a, Country $b) use ($collator) {
return $collator->compare($a->getName(), $b->getName());
});
}
$this->countries = Mapping::mapKeys($countries, function ($key, Country $value) {
return [$value->getId(), $value];
});
}
return $this->countries;
}
protected function loadActive()
{
if ($result = $this->session->get('country')) {
return $result;
}
return $this->getDefaultId();
}
}