src/Entity/Menu.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Menu\Item;
  4. use App\Entity\Utils\IntlInterface;
  5. use App\Entity\Utils\Traits\TimestampTrait;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\Common\Collections\Criteria;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Doctrine\ORM\PersistentCollection;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. /**
  13.  * Class Menu.
  14.  */
  15. #[ORM\Table(name'menu')]
  16. #[ORM\Entity(repositoryClass\App\Repository\MenuRepository::class)]
  17. class Menu implements IntlInterface
  18. {
  19.     use TimestampTrait;
  20.     /**
  21.      * Id.
  22.      */
  23.     #[ORM\Id]
  24.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::INTEGER)]
  25.     #[ORM\GeneratedValue]
  26.     protected int $id;
  27.     /**
  28.      * Name.
  29.      */
  30.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::STRINGlength255)]
  31.     #[Assert\NotBlank(message'form.error.required')]
  32.     protected string $name;
  33.     /**
  34.      * Reference.
  35.      */
  36.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::STRINGlength255)]
  37.     #[Assert\Length(max255maxMessage'form.error.long')]
  38.     #[Assert\Regex(pattern'/^[a-zA-Z0-9-]+$/i', match: truemessage'form.error.reference')]
  39.     protected string $reference;
  40.     /**
  41.      * Locale.
  42.      */
  43.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::STRINGlength255)]
  44.     #[Assert\NotBlank(message'form.error.required')]
  45.     protected string $locale 'en';
  46.     /**
  47.      * Translations.
  48.      */
  49.     #[ORM\OneToMany(targetEntity'Menu'mappedBy'master'cascade: ['detach'])]
  50.     protected Collection $translations;
  51.     /**
  52.      * Master.
  53.      */
  54.     #[ORM\ManyToOne(targetEntity'Menu'inversedBy'translations'cascade: ['detach'])]
  55.     #[ORM\JoinColumn(name'master_id')]
  56.     protected ?Menu $master null;
  57.     /**
  58.      * Items.
  59.      *
  60.      * @var \Doctrine\Common\Collections\Collection<int, \App\Entity\Menu\Item>|\App\Entity\Menu\Item[]
  61.      */
  62.     #[Assert\Valid]
  63.     #[ORM\OrderBy(['id' => 'asc'])]
  64.     #[ORM\OneToMany(targetEntity\App\Entity\Menu\Item::class, mappedBy'menu'cascade: ['all'], orphanRemovaltrue)]
  65.     protected Collection $items;
  66.     /**
  67.      * Menu constructor.
  68.      */
  69.     public function __construct()
  70.     {
  71.         $this->items        = new ArrayCollection();
  72.         $this->translations = new ArrayCollection();
  73.     }
  74.     public function setId(int $id): void
  75.     {
  76.         $this->id $id;
  77.     }
  78.     public function getId(): int
  79.     {
  80.         return $this->id;
  81.     }
  82.     public function getName(): string
  83.     {
  84.         return $this->name;
  85.     }
  86.     public function setName(string $name): void
  87.     {
  88.         $this->name $name;
  89.     }
  90.     public function getReference(): string
  91.     {
  92.         return $this->reference;
  93.     }
  94.     public function setReference(string $reference): void
  95.     {
  96.         $this->reference $reference;
  97.     }
  98.     public function getLocale(): string
  99.     {
  100.         return $this->locale;
  101.     }
  102.     public function setLocale(string $locale): void
  103.     {
  104.         $this->locale $locale;
  105.     }
  106.     public function getExistingLocales(): array
  107.     {
  108.         $locales = [];
  109.         $master    $this->getMaster();
  110.         $locales[] = $master->getLocale();
  111.         /** @var Menu $translation */
  112.         foreach ($this->getTranslations() as $translation) {
  113.             $locales[] = $translation->getLocale();
  114.         }
  115.         return $locales;
  116.     }
  117.     public function getTranslations(): Collection
  118.     {
  119.         $master $this->getMaster();
  120.         return $master->translations;
  121.     }
  122.     /**
  123.      * @return mixed|null
  124.      */
  125.     public function getTranslation(string $locale)
  126.     {
  127.         $master $this->getMaster();
  128.         if ($master->getLocale() == $locale) {
  129.             return $master;
  130.         }
  131.         /** @var Menu $translation */
  132.         foreach ($master->getTranslations() as $translation) {
  133.             if ($translation->getLocale() == $locale) {
  134.                 return $translation;
  135.             }
  136.         }
  137.         return null;
  138.     }
  139.     public function addTranslation(object $translation): void
  140.     {
  141.         $this->translations[] = $translation;
  142.         $translation->setMaster($this);
  143.     }
  144.     public function removeTranslation(object $translation): void
  145.     {
  146.         $this->translations->removeElement($translation);
  147.         $translation->setMaster(null);
  148.     }
  149.     public function removeAllTranslation(): void
  150.     {
  151.         foreach ($this->translations as $translation) {
  152.             $this->removeTranslation($translation);
  153.         }
  154.     }
  155.     public function isMaster(): bool
  156.     {
  157.         return !$this->master;
  158.     }
  159.     public function getMaster(): ?Menu
  160.     {
  161.         if (!$this->master) {
  162.             return $this;
  163.         }
  164.         return $this->master;
  165.     }
  166.     public function setMaster(?object $master): void
  167.     {
  168.         $this->master $master;
  169.     }
  170.     public function addItem(Item $item): Menu
  171.     {
  172.         $this->items[] = $item;
  173.         $item->setMenu($this);
  174.         return $this;
  175.     }
  176.     public function removeItem(Item $item): void
  177.     {
  178.         $this->items->removeElement($item);
  179.         $item->setMenu(null);
  180.     }
  181.     /**
  182.      * @return Collection
  183.      */
  184.     public function getItems()
  185.     {
  186.         return $this->items;
  187.     }
  188.     public function setItems(Collection $items): void
  189.     {
  190.         foreach ($items as $b) {
  191.             $this->addItem($b);
  192.         }
  193.     }
  194.     public function removeAllItems(): void
  195.     {
  196.         if (count($this->items)) {
  197.             foreach ($this->items as $b) {
  198.                 $this->removeItem($b);
  199.             }
  200.         }
  201.     }
  202.     public function reorderItems(string $field): void
  203.     {
  204.         /** @var PersistentCollection $items */
  205.         $items $this->items;
  206.         if (!$items->isInitialized()) {
  207.             $items->initialize();
  208.         }
  209.         $criteria Criteria::create()
  210.             ->orderBy([$field => Criteria::ASC]);
  211.         $items $items->matching($criteria);
  212.         if (!$this->items->isInitialized()) {
  213.             $this->items->initialize();
  214.         }
  215.         $this->items->clear();
  216.         /** @var Item $item */
  217.         foreach ($items as $key => $item) {
  218.             $item->reorderChildren($field);
  219.             $this->items->set($key$item);
  220.         }
  221.     }
  222.     public function import(array $databool $partial false): void
  223.     {
  224.         $fields $this->getArrayModel();
  225.         foreach ($fields as $field) {
  226.             if (isset($data[$field])) {
  227.                 $setter         'set'.ucfirst((string) $field);
  228.                 $fields[$field] = $this->{$setter}($data[$field]);
  229.             }
  230.         }
  231.         if (!$partial) {
  232.             $this->removeAllItems();
  233.             $this->items->clear();
  234.             $cpt 0;
  235.             foreach ($data['items'] as $itemData) {
  236.                 $item = new Item();
  237.                 $item->setArrayData($itemData);
  238.                 $item->setPosition($cpt);
  239.                 $this->addItem($item);
  240.                 ++$cpt;
  241.             }
  242.         }
  243.     }
  244.     public function export(?string $locale null): array
  245.     {
  246.         $items  = [];
  247.         $fields = [];
  248.         /** @var Item $item */
  249.         foreach ($this->items as $item) {
  250.             $items[] = $item->getArrayData();
  251.         }
  252.         foreach ($this->getArrayModel() as $field) {
  253.             $getter 'get'.ucfirst((string) $field);
  254.             $fields[$field] = $this->{$getter}();
  255.         }
  256.         if ($locale) {
  257.             $fields['locale'] = $locale;
  258.         }
  259.         return array_merge(
  260.             $fields,
  261.             ['items' => $items]
  262.         );
  263.     }
  264.     public function getArrayModel(): array
  265.     {
  266.         return [
  267.             'name',
  268.             'reference',
  269.             'locale',
  270.         ];
  271.     }
  272. }