Skip to content

Instantly share code, notes, and snippets.

@Nightbr
Last active October 2, 2018 08:43
Show Gist options
  • Save Nightbr/4f622e5c5a1ef1991b28759c267bd61f to your computer and use it in GitHub Desktop.
Save Nightbr/4f622e5c5a1ef1991b28759c267bd61f to your computer and use it in GitHub Desktop.
<?php
namespace App\Repository;
use App\Entity\Locale;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
final class LocaleRepository
{
/**
* @var Repository
*/
private $repository;
/**
* @var string
*/
private $parametersDefaultLocale;
/**
* @var array
*/
private $parametersAvailableLocales;
public function __construct(string $parametersDefaultLocale, array $parametersAvailableLocales, EntityManager $entityManager)
{
$this->repository = $entityManager->getRepository(Locale::class);
$this->parametersDefaultLocale = $parametersDefaultLocale;
$this->parametersAvailableLocales = $parametersAvailableLocales;
}
/**
* Return defaultLocale code
* @return string
*/
public function getDefaultLocale()
{
$defaultLocale = $this->parametersDefaultLocale;
$dbDefaultLocale = $this->repository->findOneBy(array('isDefault'=>true));
if($dbDefaultLocale){
$defaultLocale = $dbDefaultLocale->getCode();
}
return $defaultLocale;
}
/**
* Return array of availableLocale code
* @return array
*/
public function getAvailableLocales()
{
$qb = $this->repository->createQueryBuilder('l');
$qb->select('l.code AS locales');
$result = $qb->getQuery()->getResult();
$availableLocales = array_map(function($el){ return $el['locales']; }, $result);
if(empty($availableLocales)){
$availableLocales = $this->parametersAvailableLocales;
}
return $availableLocales;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment