src/Entity/UseCaseTopic.php line 15

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use App\Repository\NewsRepository;
  5. use App\Repository\UseCaseTopicRepository;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\DBAL\Types\Types;
  9. use Doctrine\ORM\Mapping as ORM;
  10. #[ORM\Entity(repositoryClassUseCaseTopicRepository::class)]
  11. class UseCaseTopic
  12. {
  13.     final public const RESOURCE_KEY 'use_case_topics';
  14.     #[ORM\Id]
  15.     #[ORM\GeneratedValue]
  16.     #[ORM\Column(typeTypes::INTEGER)]
  17.     private ?int $id null;
  18.     #[ORM\OneToMany(mappedBy'useCaseTopic'targetEntityUseCase::class)]
  19.     private ?Collection $useCases;
  20.     #[ORM\Column(typeTypes::STRINGlength255nullablefalse)]
  21.     private ?string $color null;
  22.     /**
  23.      * @var Collection<string, UseCaseTopicTranslation>
  24.      */
  25.     #[ORM\OneToMany(mappedBy'useCaseTopic'targetEntityUseCaseTopicTranslation::class, cascade: ['persist'], indexBy'locale')]
  26.     private Collection $translations;
  27.     private string $locale;
  28.     public function __construct()
  29.     {
  30.         $this->useCases = new ArrayCollection();
  31.         $this->translations = new ArrayCollection();
  32.     }
  33.     public function getId(): ?int
  34.     {
  35.         return $this->id;
  36.     }
  37.     public function getTitle(): ?string
  38.     {
  39.         $translation $this->getTranslation($this->locale);
  40.         if (!$translation instanceof UseCaseTopicTranslation) {
  41.             return null;
  42.         }
  43.         return $translation->getTitle();
  44.     }
  45.     public function setTitle(string $title): void
  46.     {
  47.         $translation $this->getTranslation($this->locale);
  48.         if (!$translation instanceof UseCaseTopicTranslation) {
  49.             $translation $this->createTranslation($this->locale);
  50.         }
  51.         $translation->setTitle($title);
  52.     }
  53.     public function getDescription(): ?string
  54.     {
  55.         $translation $this->getTranslation($this->locale);
  56.         if (!$translation instanceof UseCaseTopicTranslation) {
  57.             return null;
  58.         }
  59.         return $translation->getDescription();
  60.     }
  61.     public function setDescription(string $description): void
  62.     {
  63.         $translation $this->getTranslation($this->locale);
  64.         if (!$translation instanceof UseCaseTopicTranslation) {
  65.             $translation $this->createTranslation($this->locale);
  66.         }
  67.         $translation->setDescription($description);
  68.     }
  69.     public function addUseCase(UseCase $useCase): static
  70.     {
  71.         if(!$this->useCases->contains($useCase)) {
  72.             $this->useCases[] = $useCase;
  73.         }
  74.         return $this;
  75.     }
  76.     public function removeUseCase(UseCase $useCase): static
  77.     {
  78.         if($this->useCases->contains($useCase)) {
  79.             $this->useCases->removeElement($useCase);
  80.         }
  81.         return $this;
  82.     }
  83.     public function getUseCases(): Collection
  84.     {
  85.         return $this->useCases;
  86.     }
  87.     public function getColor(): ?string
  88.     {
  89.         return $this->color;
  90.     }
  91.     public function setColor(?string $color): void
  92.     {
  93.         $this->color $color;
  94.     }
  95.     public function getLocale(): string
  96.     {
  97.         return $this->locale;
  98.     }
  99.     public function setLocale(string $locale): self
  100.     {
  101.         $this->locale $locale;
  102.         return $this;
  103.     }
  104.     /**
  105.      * @return UseCaseTopicTranslation[]
  106.      */
  107.     public function getTranslations(): array
  108.     {
  109.         return $this->translations->toArray();
  110.     }
  111.     protected function getTranslation(string $locale): ?UseCaseTopicTranslation
  112.     {
  113.         if (!$this->translations->containsKey($locale)) {
  114.             return null;
  115.         }
  116.         return $this->translations->get($locale);
  117.     }
  118.     protected function createTranslation(string $locale): UseCaseTopicTranslation
  119.     {
  120.         $translation = new UseCaseTopicTranslation($this$locale);
  121.         $this->translations->set($locale$translation);
  122.         return $translation;
  123.     }
  124. }