src/Entity/Tag.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Utils\IntlInterface;
  4. use App\Entity\Utils\Traits\TimestampTrait;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. /**
  9.  * Class Tag.
  10.  */
  11. #[ORM\Table(name'tag')]
  12. #[ORM\Entity(repositoryClass\App\Repository\TagRepository::class)]
  13. class Tag implements IntlInterface\Stringable
  14. {
  15.     use TimestampTrait;
  16.     /**
  17.      * Id.
  18.      */
  19.     #[ORM\Id]
  20.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::INTEGER)]
  21.     #[ORM\GeneratedValue]
  22.     protected ?int $id null;
  23.     /**
  24.      * Type.
  25.      */
  26.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::STRINGlength255)]
  27.     #[Assert\NotBlank(message'form.error.required')]
  28.     #[Assert\Length(min2max255minMessage'form.error.short'maxMessage'form.error.long')]
  29.     protected string $type;
  30.     /**
  31.      * Global name.
  32.      */
  33.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::STRINGlength255nullabletrue)]
  34.     #[Assert\Length(min2max255minMessage'form.error.short'maxMessage'form.error.long')]
  35.     protected ?string $globalName null;
  36.     /**
  37.      * Global description.
  38.      */
  39.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::TEXTnullabletrue)]
  40.     #[Assert\Length(min2max10000minMessage'form.error.short'maxMessage'form.error.long')]
  41.     protected ?string $globalDescription null;
  42.     /**
  43.      * Name.
  44.      */
  45.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::STRINGlength255)]
  46.     #[Assert\NotBlank(message'form.error.required')]
  47.     #[Assert\Length(min2max255minMessage'form.error.short'maxMessage'form.error.long')]
  48.     protected string $name;
  49.     /**
  50.      * Description.
  51.      */
  52.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::TEXTnullabletrue)]
  53.     #[Assert\Length(min2max10000minMessage'form.error.short'maxMessage'form.error.long')]
  54.     protected ?string $description null;
  55.     /**
  56.      * Locale.
  57.      */
  58.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::STRINGlength255)]
  59.     #[Assert\NotBlank(message'form.error.required')]
  60.     protected string $locale 'en';
  61.     /**
  62.      * Translations.
  63.      */
  64.     #[ORM\OneToMany(targetEntity'Tag'mappedBy'master'cascade: ['detach'])]
  65.     protected ?Collection $translations null;
  66.     /**
  67.      * Master.
  68.      */
  69.     #[ORM\ManyToOne(targetEntity'Tag'inversedBy'translations'cascade: ['detach'])]
  70.     #[ORM\JoinColumn(name'master_id')]
  71.     protected ?Tag $master null;
  72.     /**
  73.      * Entities.
  74.      */
  75.     #[Assert\Valid]
  76.     #[ORM\ManyToMany(targetEntity'Page'mappedBy'tags'cascade: ['detach'])]
  77.     protected Collection $pages;
  78.     /**
  79.      * Tag constructor.
  80.      *
  81.      * @throws \Exception
  82.      */
  83.     public function __construct()
  84.     {
  85.         $this->translations = new \Doctrine\Common\Collections\ArrayCollection();
  86.         $this->pages        = new \Doctrine\Common\Collections\ArrayCollection();
  87.         $name               date_timestamp_get(new \DateTime());
  88.         $this->setGlobalName($name);
  89.         $this->setName($name);
  90.     }
  91.     public function __toString(): string
  92.     {
  93.         return $this->globalName;
  94.     }
  95.     public function setId(int $id): void
  96.     {
  97.         $this->id $id;
  98.     }
  99.     public function getId(): int
  100.     {
  101.         return $this->id;
  102.     }
  103.     public function getType(): string
  104.     {
  105.         return $this->type;
  106.     }
  107.     public function setType(string $type): void
  108.     {
  109.         $this->type $type;
  110.     }
  111.     public function getGlobalName(): ?string
  112.     {
  113.         return $this->globalName;
  114.     }
  115.     public function setGlobalName(?string $globalName): void
  116.     {
  117.         $this->globalName $globalName;
  118.         if ($this->isMaster()) {
  119.             if ($this->translations) {
  120.                 /** @var Tag $translation */
  121.                 foreach ($this->translations as $translation) {
  122.                     $translation->setGlobalName($globalName);
  123.                 }
  124.             }
  125.         }
  126.     }
  127.     /**
  128.      * @return mixed
  129.      */
  130.     public function getGlobalDescription()
  131.     {
  132.         return $this->globalDescription;
  133.     }
  134.     public function setGlobalDescription(?string $globalDescription): void
  135.     {
  136.         $this->globalDescription $globalDescription;
  137.     }
  138.     public function getName(): string
  139.     {
  140.         return $this->name;
  141.     }
  142.     public function setName(string $name): void
  143.     {
  144.         $this->name $name;
  145.     }
  146.     public function getDescription(): ?string
  147.     {
  148.         return $this->description;
  149.     }
  150.     public function setDescription(?string $description): void
  151.     {
  152.         $this->description $description;
  153.     }
  154.     public function getLocale(): string
  155.     {
  156.         return $this->locale;
  157.     }
  158.     public function setLocale(string $locale): void
  159.     {
  160.         $this->locale $locale;
  161.     }
  162.     public function getExistingLocales(): array
  163.     {
  164.         $locales = [];
  165.         $master    $this->getMaster();
  166.         $locales[] = $master->getLocale();
  167.         /** @var Page $translation */
  168.         foreach ($this->getTranslations() as $translation) {
  169.             $locales[] = $translation->getLocale();
  170.         }
  171.         return $locales;
  172.     }
  173.     public function getTranslations(): Collection
  174.     {
  175.         $master $this->getMaster();
  176.         return $master->translations;
  177.     }
  178.     /**
  179.      * @return mixed|null
  180.      */
  181.     public function getTranslation(string $locale)
  182.     {
  183.         $master $this->getMaster();
  184.         if ($master->getLocale() == $locale) {
  185.             return $master;
  186.         }
  187.         /** @var Page $translation */
  188.         foreach ($master->getTranslations() as $translation) {
  189.             if ($translation->getLocale() == $locale) {
  190.                 return $translation;
  191.             }
  192.         }
  193.         return null;
  194.     }
  195.     public function addTranslation(object $translation): void
  196.     {
  197.         $this->translations[] = $translation;
  198.         /* @var Tag $translation */
  199.         $translation->setMaster($this);
  200.     }
  201.     public function removeTranslation(object $translation): void
  202.     {
  203.         $this->translations->removeElement($translation);
  204.         /* @var Tag $translation */
  205.         $translation->setMaster(null);
  206.     }
  207.     public function removeAllTranslation(): void
  208.     {
  209.         foreach ($this->translations as $translation) {
  210.             $this->removeTranslation($translation);
  211.         }
  212.     }
  213.     public function isMaster(): bool
  214.     {
  215.         return !$this->master;
  216.     }
  217.     public function getMaster(): Tag
  218.     {
  219.         if (false == $this->master) {
  220.             return $this;
  221.         }
  222.         return $this->master;
  223.     }
  224.     /**
  225.      * @param mixed $master
  226.      */
  227.     public function setMaster($master): void
  228.     {
  229.         $this->master $master;
  230.     }
  231.     /**
  232.      * Add pages.
  233.      */
  234.     public function addPage(Page $page): void
  235.     {
  236.         $this->pages[] = $page;
  237.     }
  238.     /**
  239.      * Remove pages.
  240.      */
  241.     public function removePage(Page $page): void
  242.     {
  243.         $this->pages->removeElement($page);
  244.     }
  245.     public function getPages(): Collection
  246.     {
  247.         return $this->pages;
  248.     }
  249.     public function getArrayModel(bool $partial false): array
  250.     {
  251.         if ($partial) {
  252.             $data = [
  253.                 'globalName',
  254.                 'name',
  255.                 'locale',
  256.                 'type',
  257.             ];
  258.         } else {
  259.             $data = [
  260.                 'globalName',
  261.                 'globalDescription',
  262.                 'name',
  263.                 'description',
  264.                 'locale',
  265.                 'type',
  266.             ];
  267.         }
  268.         return $data;
  269.     }
  270.     public function import(array $databool $partial false): void
  271.     {
  272.         $fields $this->getArrayModel($partial);
  273.         foreach ($fields as $field) {
  274.             $setter 'set'.ucfirst((string) $field);
  275.             $fields[$field] = $this->{$setter}($data[$field]);
  276.         }
  277.     }
  278.     public function export(string $locale null): array
  279.     {
  280.         $fields = [];
  281.         foreach ($this->getArrayModel() as $field) {
  282.             $getter 'get'.ucfirst((string) $field);
  283.             $fields[$field] = $this->{$getter}();
  284.         }
  285.         if ($locale) {
  286.             $fields['locale'] = $locale;
  287.         }
  288.         return $fields;
  289.     }
  290. }