From 78dfa176f09b87d9c7e0894964cc349fb940cbcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Danyi?= Date: Mon, 14 May 2018 15:22:38 +0200 Subject: [PATCH] * locale based sorting implemented --- src/App/Service/AwardeeManager.php | 23 +++++++++++++++++++++-- src/App/Service/JudgeManager.php | 17 +++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/App/Service/AwardeeManager.php b/src/App/Service/AwardeeManager.php index 21685f5..15ac10b 100644 --- a/src/App/Service/AwardeeManager.php +++ b/src/App/Service/AwardeeManager.php @@ -10,6 +10,7 @@ use UtilityModule\Hydrator\DoctrineObject; class AwardeeManager { + const SORT_LOCALE = 'hu_HU.UTF8'; const IMAGE_DIRECTORY = 'data/user-data/images/awardee'; /** @var EntityManager */ @@ -31,9 +32,11 @@ class AwardeeManager */ public function getAwardees(): ?array { - return $this->entityManager->getRepository(Awardee::class)->findBy([], [ + $result = $this->entityManager->getRepository(Awardee::class)->findBy([], [ 'year' => 'DESC', + 'name' => 'ASC', ]); + return $this->sortAwardeesResult($result); } /** @@ -42,9 +45,25 @@ class AwardeeManager */ public function getAwardeesByYear(int $year): ?array { - return $this->entityManager->getRepository(Awardee::class)->findBy([ + $result = $this->entityManager->getRepository(Awardee::class)->findBy([ 'year' => $year, + ], [ + 'name' => 'ASC', ]); + return $this->sortAwardeesResult($result); + } + + /** + * @param Awardee[] $awardees + * @return Awardee[] + */ + private function sortAwardeesResult(array $awardees): array + { + setlocale(LC_COLLATE, self::SORT_LOCALE); + usort($awardees, function (Awardee $a, Awardee $b) { + return strcoll($a->getName(), $b->getName()); + }); + return $awardees; } /** diff --git a/src/App/Service/JudgeManager.php b/src/App/Service/JudgeManager.php index 11d68d7..d034c73 100644 --- a/src/App/Service/JudgeManager.php +++ b/src/App/Service/JudgeManager.php @@ -10,6 +10,7 @@ use UtilityModule\Hydrator\DoctrineObject; class JudgeManager { + const SORT_LOCALE = 'hu_HU.UTF8'; const IMAGE_DIRECTORY = 'data/user-data/images/judge'; /** @var EntityManager */ @@ -32,12 +33,14 @@ class JudgeManager public function getJudges(): ?array { $qb = $this->entityManager->createQueryBuilder(); - return $qb->select('j, t') + $result = $qb->select('j, t') ->from(Judge::class, 'j') ->leftJoin('j.titles', 't') ->orderBy('j.name', 'ASC') + ->addOrderBy('t.year', 'ASC') ->getQuery() ->getResult(); + return $this->sortJudgesResult($result); } /** @@ -47,7 +50,7 @@ class JudgeManager public function getJudgesByYear(int $year): ?array { $qb = $this->entityManager->createQueryBuilder(); - return $qb->select('j,t') + $result = $qb->select('j,t') ->from(Judge::class, 'j') ->innerJoin('j.titles', 't') ->where('t.year = :year') @@ -55,6 +58,16 @@ class JudgeManager ->setParameter('year', $year) ->getQuery() ->getResult(); + return $this->sortJudgesResult($result); + } + + private function sortJudgesResult(array $judges): array + { + setlocale(LC_COLLATE, self::SORT_LOCALE); + usort($judges, function (Judge $a, Judge $b) { + return strcoll($a->getName(), $b->getName()); + }); + return $judges; } /**