Skip to content

Commit

Permalink
fix: Fixed fetching non-ISO locale from database and querying result …
Browse files Browse the repository at this point in the history
…cache
  • Loading branch information
ambroisemaupate committed Feb 21, 2024
1 parent 02f0457 commit 6a1ec32
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/RoadizCoreBundle/src/Repository/TranslationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public function __construct(
parent::__construct($registry, Translation::class, $dispatcher);
}

public function isLocaleValid(?string $locale): bool
{
if (null === $locale) {
return false;
}

return preg_match('/^[A-Za-z]{2,4}([_-][A-Za-z]{4})?([_-]([A-Za-z]{2}|[0-9]{3}))?$/', $locale) === 1;
}

/**
* Get single default translation.
*
Expand Down Expand Up @@ -82,6 +91,10 @@ public function findAllAvailable(): array
*/
public function exists(string $locale): bool
{
if (!$this->isLocaleValid($locale)) {
return false;
}

$qb = $this->createQueryBuilder('t');
$qb->select($qb->expr()->countDistinct('t.locale'))
->andWhere($qb->expr()->eq('t.locale', ':locale'))
Expand Down Expand Up @@ -189,6 +202,9 @@ public function getAllOverrideLocales(): array
*/
public function findByLocaleAndAvailable(string $locale): array
{
if (!$this->isLocaleValid($locale)) {
return [];
}
$qb = $this->createQueryBuilder(self::TRANSLATION_ALIAS);
$qb->andWhere($qb->expr()->eq(self::TRANSLATION_ALIAS . '.available', ':available'))
->andWhere($qb->expr()->eq(self::TRANSLATION_ALIAS . '.locale', ':locale'))
Expand All @@ -213,6 +229,9 @@ public function findByLocaleAndAvailable(string $locale): array
*/
public function findByOverrideLocaleAndAvailable(string $overrideLocale): array
{
if (!$this->isLocaleValid($overrideLocale)) {
return [];
}
$qb = $this->createQueryBuilder(self::TRANSLATION_ALIAS);
$qb->andWhere($qb->expr()->eq(self::TRANSLATION_ALIAS . '.available', ':available'))
->andWhere($qb->expr()->eq(self::TRANSLATION_ALIAS . '.overrideLocale', ':overrideLocale'))
Expand Down Expand Up @@ -242,6 +261,9 @@ public function findOneByLocaleOrOverrideLocale(
string $locale,
string $alias = TranslationRepository::TRANSLATION_ALIAS
): ?TranslationInterface {
if (!$this->isLocaleValid($locale)) {
return null;
}
$qb = $this->createQueryBuilder($alias);
$qb->andWhere($qb->expr()->orX(
$qb->expr()->eq($alias . '.locale', ':locale'),
Expand All @@ -267,6 +289,9 @@ public function findOneByLocaleOrOverrideLocale(
*/
public function findOneAvailableByLocaleOrOverrideLocale(string $locale): ?TranslationInterface
{
if (!$this->isLocaleValid($locale)) {
return null;
}
$qb = $this->createQueryBuilder(self::TRANSLATION_ALIAS);
$qb->andWhere($qb->expr()->orX(
$qb->expr()->eq(self::TRANSLATION_ALIAS . '.locale', ':locale'),
Expand Down Expand Up @@ -294,6 +319,9 @@ public function findOneAvailableByLocaleOrOverrideLocale(string $locale): ?Trans
*/
public function findOneByLocaleAndAvailable(string $locale): ?TranslationInterface
{
if (!$this->isLocaleValid($locale)) {
return null;
}
$qb = $this->createQueryBuilder(self::TRANSLATION_ALIAS);
$qb->andWhere($qb->expr()->eq(self::TRANSLATION_ALIAS . '.available', ':available'))
->andWhere($qb->expr()->eq(self::TRANSLATION_ALIAS . '.locale', ':locale'))
Expand All @@ -318,6 +346,9 @@ public function findOneByLocaleAndAvailable(string $locale): ?TranslationInterfa
*/
public function findOneByOverrideLocaleAndAvailable(string $overrideLocale): ?TranslationInterface
{
if (!$this->isLocaleValid($overrideLocale)) {
return null;
}
$qb = $this->createQueryBuilder(self::TRANSLATION_ALIAS);
$qb->andWhere($qb->expr()->eq(self::TRANSLATION_ALIAS . '.available', ':available'))
->andWhere($qb->expr()->eq(self::TRANSLATION_ALIAS . '.overrideLocale', ':overrideLocale'))
Expand Down
2 changes: 2 additions & 0 deletions lib/RoadizCoreBundle/src/Routing/NodesSourcesPathResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,11 @@ private function parseFromIdentifier(
]);
return $nodeSource;
} else {
$this->stopwatch->stop('parseFromIdentifier');
throw new ResourceNotFoundException(sprintf('"%s" was not found.', $identifier));
}
} else {
$this->stopwatch->stop('parseFromIdentifier');
throw new ResourceNotFoundException();
}
}
Expand Down

0 comments on commit 6a1ec32

Please sign in to comment.