<?php
namespace WebBundle\Repository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\DBAL\DBALException;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Doctrine\Persistence\ManagerRegistry;
use Exception;
use FlexApp\Constant\TimeConstant;
use WebBundle\Entity\ListCountry;
use WebBundle\Helper\App;
use WebBundle\Helper\StrHelper;
use WebBundle\Interfaces\ListEntityRepositoryInterface;
use WebBundle\Traits\RepoTrait;
class ListCountryRepository extends ServiceEntityRepository implements ListEntityRepositoryInterface
{
use RepoTrait;
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, ListCountry::class);
}
/**
* @param string $code
* @return ListCountry|null
* @throws NonUniqueResultException
*/
public function getCountry(string $code): ?ListCountry
{
$q = $this->createQueryBuilder('l')
->andWhere('l.code=:code OR l.id=:code')
->setParameter('code', $code)
->setMaxResults(1);
$r = $q->getQuery()
->useQueryCache(true)
->useResultCache(true, 3600);
try {
return $r->getSingleResult();
} catch (NoResultException $e) {
return null;
}
}
/**
* @param string $name
* @return ListCountry|null
* @throws NonUniqueResultException
*/
public function getByNameEn($name)
{
$q = $this->createQueryBuilder('l')
->andWhere('l.nameEn=:nameEn')
->setParameter('nameEn', StrHelper::toLower($name))
->setMaxResults(1);
$r = $q->getQuery()
->useQueryCache(true)
->useResultCache(true, 3600);
try {
$item = $r->getSingleResult();
} catch (NoResultException $e) {
return null;
}
return $item;
}
/**
* @param bool $full
* @return array|null
*/
public function getList(bool $full = false): ?array
{
$q = $this
->createQueryBuilder('l');
if (!$full) {
$q->where('l.hide != 1 AND l.count > 0');
}
$r = $q->orderBy('l.sort DESC, l.name')
->getQuery()
->useQueryCache(true)
->useResultCache(true, (int) TimeConstant::DAY);
try {
return $r->getArrayResult();
} catch (NoResultException $e) {
return null;
}
}
/**
* @return array|null
*/
public function getListForLocalize(): ?array
{
$q = $this
->createQueryBuilder('l')
->where('l.hide != 1')
->orderBy('l.code', 'ASC')
->getQuery()
->setResultCacheLifetime((int) TimeConstant::DAY);
try {
$items = $q->getArrayResult();
} catch (NoResultException $e) {
return null;
}
return $items;
}
/**
* @return array|null
*/
public function getListForByOrder()
{
$q = $this
->createQueryBuilder('lc')
->select('lc, s')
->leftJoin('lc.states', 's')
->orderBy('lc.sort DESC, lc.name')
->andWhere('lc.hide != 1')
->getQuery()
->useQueryCache(true);
//->useResultCache(true, (3600 * 24 * 30));
try {
$items = $q->getArrayResult();
} catch (NoResultException $e) {
return null;
}
return $items;
}
/**
* получить массив данных для профиля
* @return array|null
*/
public function getListForProfile()
{
$q = $this
->createQueryBuilder('l');
$r = $q->orderBy('l.name')
->andWhere('l.alias is not null')
->getQuery();
// ->useQueryCache(true)
// ->useResultCache(true, 24 * 3600);
try {
$items = $r->getArrayResult();
} catch (NoResultException $e) {
return null;
}
return $items;
}
/**
* @param $sql
* @return bool
* @throws DBALException
*/
public function sql($sql)
{
$conn = $this->getEntityManager()->getConnection();
$conn = $conn->prepare($sql);
return $conn->execute();
}
public function getListForHeader(string $locale)
{
return $this->getListForReviewsAndProfile($locale);
}
/**
* список стран для виджета отзывов
* @param string $locale
* @return array
* @throws Exception
*/
public function getListForReviewsAndProfile(string $locale, $withHide = false)
{
$translator = App::getTranslator();
$result = [];
$q = $this
->createQueryBuilder('c')
->select('c.code, c.alias')
->orderBy('c.sort DESC, c.name');
if (!$withHide) {
$q->andWhere('c.hide != 1');
}
$r = $q->getQuery()
->useQueryCache(true)
->enableResultCache((int) TimeConstant::DAY);
$items = $r->getArrayResult();
// Получаем переведённый список
foreach ($items as $item) {
$result[$item['code']] = $translator->trans($item['alias'], [], 'messages', $locale);
}
return $result;
}
public function getReference(int $id): ListCountry
{
return $this->getEntityManager()->getReference(ListCountry::class, $id);
}
/**
* Получает массив сущностей ListCountry по массиву кодов.
*
* @param array $codes Массив кодов стран.
* @return ListCountry[] Массив сущностей ListCountry.
*/
public function getCountriesByCodes(array $codes): array
{
if (empty($codes)) {
return [];
}
$qb = $this->createQueryBuilder('c')
->where('c.code IN (:codes) OR c.id IN (:codes)')
->setParameter('codes', $codes)
->getQuery();
try {
return $qb->getResult();
} catch (Exception $e) {
return [];
}
}
}