src/Entity/FaqCategories.php line 13

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use JMS\Serializer\Annotation as Serializer;
  8. /**
  9.  * @ORM\Entity(repositoryClass=FaqCategoriesRepository::class)
  10.  */
  11. class FaqCategories
  12. {
  13.     
  14.     const RESOURCE_KEY 'faqcategories';
  15.     /**
  16.      * @ORM\Id
  17.      * @ORM\GeneratedValue
  18.      * @ORM\Column(type="integer")
  19.      */
  20.     private $id;
  21.     /**
  22.      * @var Collection<string, FaqCategoriesTranslation>
  23.      *
  24.      * @ORM\OneToMany(targetEntity="App\Entity\FaqCategoriesTranslation", mappedBy="FaqCategories", cascade={"ALL"}, indexBy="locale")
  25.      *
  26.      * @Serializer\Exclude
  27.      */
  28.     private $translations;
  29.     /**
  30.      * @var string
  31.      */
  32.     private $locale="en";
  33.     public function __construct()
  34.     {
  35.         $this->translations = new ArrayCollection();    
  36.     }
  37.     public function getId(): ?int
  38.     {
  39.         return $this->id;
  40.     }
  41.     /**
  42.      * @Serializer\VirtualProperty(name="title")
  43.      */
  44.     public function getTitle(): ?string
  45.     {
  46.         $translation $this->getTranslation($this->locale);
  47.         if (!$translation) {
  48.             return null;
  49.         }
  50.         return $translation->getTitle();
  51.     }
  52.     public function setTitle(?string $title): self
  53.     {
  54.         $translation $this->getTranslation($this->locale);
  55.         if (!$translation) {
  56.             $translation $this->createTranslation($this->locale);
  57.         }
  58.         $translation->setTitle($title);
  59.         return $this;
  60.     }
  61.     /**
  62.      * @Serializer\VirtualProperty(name="description")
  63.      */
  64.     public function getDescription(): ?string
  65.     {
  66.         $translation $this->getTranslation($this->locale);
  67.         if (!$translation) {
  68.             return null;
  69.         }
  70.         return $translation->getDescription();
  71.     }
  72.     public function setDescription(?string $description): self
  73.     {
  74.         $translation $this->getTranslation($this->locale);
  75.         if (!$translation) {
  76.             $translation $this->createTranslation($this->locale);
  77.         }
  78.         $translation->setDescription($description);
  79.         return $this;
  80.     }
  81.     public function getLocale(): string
  82.     {
  83.         return $this->locale;
  84.     }
  85.     public function setLocale(?string $locale): self
  86.     {
  87.         $this->locale $locale;
  88.         return $this;
  89.     }
  90.     /**
  91.      * @return FaqCategoriesTranslation[]
  92.      */
  93.     public function getTranslations(): array
  94.     {
  95.         return $this->translations->toArray();
  96.     }
  97.     protected function getTranslation(string $locale): ?FaqCategoriesTranslation
  98.     {
  99.         if (!$this->translations->containsKey($locale)) {
  100.             return null;
  101.         }
  102.         return $this->translations->get($locale);
  103.     }
  104.     protected function createTranslation(string $locale): FaqCategoriesTranslation
  105.     {
  106.         $translation = new FaqCategoriesTranslation($this$locale);
  107.         $this->translations->set($locale$translation);
  108.         return $translation;
  109.     }
  110.     public function __toString()
  111.     {
  112.         $translation $this->getTranslation($this->locale);
  113.         if (!$translation) {
  114.             return null;
  115.         }
  116.         return $translation->getTitle();
  117.     }
  118. }