Last active
October 2, 2018 08:43
-
-
Save Nightbr/4f622e5c5a1ef1991b28759c267bd61f to your computer and use it in GitHub Desktop.
Part of https://medium.com/@titouanbenoit/internationalization-with-api-platform-the-other-way-5ce9c446737f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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