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\Column(typeTypes::STRINGlength255nullabletrue)]
  19.     private ?string $image null;
  20.     #[ORM\OneToMany(mappedBy'useCaseTopic'targetEntityUseCase::class)]
  21.     private ?Collection $useCases;
  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 getImage(): ?string
  70.     {
  71.         return $this->image;
  72.     }
  73.     public function setImage(string $image): void
  74.     {
  75.         $this->image $image;
  76.     }
  77.     public function addUseCase(UseCase $useCase): static
  78.     {
  79.         if(!$this->useCases->contains($useCase)) {
  80.             $this->useCases[] = $useCase;
  81.         }
  82.         return $this;
  83.     }
  84.     public function removeUseCase(UseCase $useCase): static
  85.     {
  86.         if($this->useCases->contains($useCase)) {
  87.             $this->useCases->removeElement($useCase);
  88.         }
  89.         return $this;
  90.     }
  91.     public function getUseCases(): Collection
  92.     {
  93.         return $this->useCases;
  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. }