Skip to content

Commit

Permalink
Sync static labels with tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Remg committed Oct 10, 2024
1 parent 2520322 commit 2040253
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/Adherent/Tag/TagGenerator/AdherentStaticLabelTagGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Adherent\Tag\TagGenerator;

use App\Entity\Adherent;

class AdherentStaticLabelTagGenerator extends AbstractTagGenerator
{
public function generate(Adherent $adherent, array $previousTags): array
{
$tags = [];

foreach ($adherent->getStaticLabels() as $staticLabel) {
$category = $staticLabel->category;

if (!$category->sync) {
continue;
}

$tags[] = $category->code.':'.$staticLabel->code;
}

return $tags;
}
}
45 changes: 44 additions & 1 deletion src/Adherent/Tag/TagTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,50 @@
namespace App\Adherent\Tag;

use App\Adherent\Tag\StaticTag\EventTagBuilder;
use App\Entity\AdherentStaticLabel;
use App\Entity\AdherentStaticLabelCategory;
use App\Repository\AdherentStaticLabelCategoryRepository;
use App\Repository\AdherentStaticLabelRepository;
use Symfony\Contracts\Translation\TranslatorInterface;

class TagTranslator
{
private array $staticLabelCategories = [];
private array $staticLabels = [];

public function __construct(
private readonly TranslatorInterface $translator,
private readonly EventTagBuilder $eventTagBuilder,
private readonly AdherentStaticLabelCategoryRepository $staticLabelCategoryRepository,
private readonly AdherentStaticLabelRepository $staticLabelRepository,
) {
}

public function trans(string $tag, bool $fullTag = true): string
{
if (substr_count($tag, ':')) {
$this->loadStaticLabels();

$parts = explode(':', $tag);

foreach ($parts as $index => $part) {
$matches = [];
if (0 === $index && \array_key_exists($part, $this->staticLabelCategories)) {
$parts[$index] = $this->staticLabelCategories[$part];

continue;
}

if (1 === $index && \array_key_exists($part, $this->staticLabels)) {
$parts[$index] = $this->staticLabels[$part];

continue;
}

if ($negation = str_ends_with($part, '--')) {
$part = substr($part, 0, -2);
}

$matches = [];
// Matches a year in the format _YYYY or _YYYY:
if (preg_match('/_(\d{4}):?/', $part, $matches)) {
$year = $matches[1];
Expand Down Expand Up @@ -58,4 +80,25 @@ private function translate(string $fullKey, string $part): string

return $trans;
}

private function loadStaticLabels(): void
{
if (empty($this->staticLabelCategories)) {
/** @var AdherentStaticLabelCategory[] $staticLabelCategories */
$staticLabelCategories = $this->staticLabelCategoryRepository->findAll();

foreach ($staticLabelCategories as $staticLabelCategory) {
$this->staticLabelCategories[$staticLabelCategory->code] = $staticLabelCategory->label;
}
}

if (empty($this->staticLabels)) {
/** @var AdherentStaticLabel[] $staticLabels */
$staticLabels = $this->staticLabelRepository->findAll();

foreach ($staticLabels as $staticLabel) {
$this->staticLabels[$staticLabel->code] = $staticLabel->label;
}
}
}
}
3 changes: 3 additions & 0 deletions src/Entity/Adherent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2591,6 +2591,9 @@ public function getImagePath(): string
return $this->imageName ? \sprintf('images/profile/%s', $this->getImageName()) : '';
}

/**
* @return AdherentStaticLabel[]|Collection
*/
public function getStaticLabels(): Collection
{
return $this->staticLabels;
Expand Down
15 changes: 15 additions & 0 deletions src/Repository/AdherentStaticLabelCategoryRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Repository;

use App\Entity\AdherentStaticLabelCategory;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class AdherentStaticLabelCategoryRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, AdherentStaticLabelCategory::class);
}
}
15 changes: 15 additions & 0 deletions src/Repository/AdherentStaticLabelRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Repository;

use App\Entity\AdherentStaticLabel;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class AdherentStaticLabelRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, AdherentStaticLabel::class);
}
}

0 comments on commit 2040253

Please sign in to comment.