src/Entity/Menu/Item.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Menu;
  3. use App\Entity\Menu;
  4. use App\Entity\Page;
  5. use App\Entity\Utils\Traits\PositionTrait;
  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 Item.
  14.  */
  15. #[ORM\Table(name'menu_item')]
  16. #[ORM\Entity(repositoryClass\App\Repository\Menu\ItemRepository::class)]
  17. class Item
  18. {
  19.     use PositionTrait;
  20.     final public const TYPE_PAGE   'page';
  21.     final public const TYPE_CUSTOM 'custom';
  22.     final public const TARGET_SELF  '_self';
  23.     final public const TARGET_BLANK '_blank';
  24.     /**
  25.      * Id.
  26.      */
  27.     #[ORM\Id]
  28.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::INTEGER)]
  29.     #[ORM\GeneratedValue]
  30.     protected ?int $id null;
  31.     /**
  32.      * Type.
  33.      */
  34.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::STRINGlength255)]
  35.     #[Assert\NotBlank(message'form.error.required')]
  36.     protected string $label;
  37.     /**
  38.      * Type.
  39.      */
  40.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::STRINGlength255)]
  41.     #[Assert\NotBlank(message'form.error.required')]
  42.     protected string $type;
  43.     /**
  44.      * Target.
  45.      */
  46.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::STRINGlength255)]
  47.     #[Assert\NotBlank(message'form.error.required')]
  48.     protected string $target;
  49.     /**
  50.      * Link.
  51.      */
  52.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::STRINGlength255nullabletrue)]
  53.     #[Assert\Length(max255maxMessage'form.error.long')]
  54.     #[Assert\Regex(pattern'/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/', match: truemessage'form.error.link')]
  55.     protected ?string $link null;
  56.     /**
  57.      * Class.
  58.      */
  59.     #[ORM\Column(type\Doctrine\DBAL\Types\Types::STRINGlength255nullabletrue)]
  60.     #[Assert\Length(max255maxMessage'form.error.long')]
  61.     protected ?string $class null;
  62.     #[ORM\ManyToOne(targetEntity\App\Entity\Page::class, inversedBy'menuItems'cascade: ['persist'])]
  63.     #[ORM\JoinColumn(name'page_id')]
  64.     protected ?Page $page null;
  65.     #[ORM\ManyToOne(targetEntity\App\Entity\Menu::class, inversedBy'items'cascade: ['all'])]
  66.     #[ORM\JoinColumn(name'menu_id')]
  67.     protected ?Menu $menu null;
  68.     /**
  69.      * Children.
  70.      */
  71.     #[ORM\OneToMany(targetEntity'Item'mappedBy'parent'cascade: ['all'], orphanRemovaltrue)]
  72.     protected Collection $children;
  73.     /**
  74.      * Master.
  75.      */
  76.     #[ORM\ManyToOne(targetEntity'Item'inversedBy'children'cascade: ['all'])]
  77.     #[ORM\JoinColumn(name'parent_id'onDelete'CASCADE')]
  78.     protected ?Item $parent null;
  79.     protected array $tmpData;
  80.     /**
  81.      * Item constructor.
  82.      */
  83.     public function __construct()
  84.     {
  85.         $this->children = new ArrayCollection();
  86.     }
  87.     public function setId(int $id): void
  88.     {
  89.         $this->id $id;
  90.     }
  91.     public function getId(): int
  92.     {
  93.         return $this->id;
  94.     }
  95.     public function getLabel(): string
  96.     {
  97.         return $this->label;
  98.     }
  99.     public function setLabel(string $label): void
  100.     {
  101.         $this->label $label;
  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 getTarget(): string
  112.     {
  113.         return $this->target;
  114.     }
  115.     public function setTarget(string $target): void
  116.     {
  117.         $this->target $target;
  118.     }
  119.     public function getLink(): ?string
  120.     {
  121.         return $this->link;
  122.     }
  123.     public function setLink(?string $link): void
  124.     {
  125.         $this->link $link;
  126.     }
  127.     public function getClass(): ?string
  128.     {
  129.         return $this->class;
  130.     }
  131.     public function setClass(?string $class): void
  132.     {
  133.         $this->class $class;
  134.     }
  135.     public function getPage(): ?Page
  136.     {
  137.         return $this->page;
  138.     }
  139.     public function setPage(Page $page null): void
  140.     {
  141.         $this->page $page;
  142.     }
  143.     public function getMenu(): Menu
  144.     {
  145.         return $this->menu;
  146.     }
  147.     public function setMenu(?Menu $menu null): void
  148.     {
  149.         $this->menu $menu;
  150.     }
  151.     public function getChildren(): Collection
  152.     {
  153.         return $this->children;
  154.     }
  155.     public function addChild(Item $child): void
  156.     {
  157.         $this->children[] = $child;
  158.         $child->setParent($this);
  159.     }
  160.     public function removeChild(Item $child): void
  161.     {
  162.         $this->children->removeElement($child);
  163.         $child->setParent(null);
  164.     }
  165.     public function removeAllChildren(): void
  166.     {
  167.         foreach ($this->children as $child) {
  168.             $this->removeChild($child);
  169.         }
  170.     }
  171.     public function hasChildren(): int
  172.     {
  173.         return count($this->children);
  174.     }
  175.     public function isRoot(): bool
  176.     {
  177.         return !$this->parent;
  178.     }
  179.     public function getParent(): Item
  180.     {
  181.         return $this->parent;
  182.     }
  183.     public function setParent(?Item $parent null): void
  184.     {
  185.         $this->parent $parent;
  186.     }
  187.     public function getArrayData(): array
  188.     {
  189.         $children = [];
  190.         if (count($this->children)) {
  191.             /** @var Item $child */
  192.             foreach ($this->children as $child) {
  193.                 $children[] = $child->getArrayData();
  194.             }
  195.         }
  196.         return [
  197.             'id'       => $this->id,
  198.             'label'    => $this->label,
  199.             'type'     => $this->type,
  200.             'target'   => $this->target,
  201.             'page'     => $this->page?->getId(),
  202.             'link'     => $this->link,
  203.             'position' => $this->position,
  204.             'class'    => $this->class,
  205.             'children' => $children,
  206.         ];
  207.     }
  208.     public function setArrayData(array $data): void
  209.     {
  210.         $this->removeAllChildren();
  211.         $this->children->clear();
  212.         $cpt 0;
  213.         if (isset($data['children'])) {
  214.             foreach ($data['children'] as $childData) {
  215.                 $child = new Item();
  216.                 $child->setArrayData($childData);
  217.                 $child->setPosition($cpt);
  218.                 $this->addChild($child);
  219.                 ++$cpt;
  220.             }
  221.         }
  222.         $this->setLabel($data['label']);
  223.         $this->setType($data['type']);
  224.         $this->setTarget($data['target']);
  225.         $this->setLink($data['link'] ?? null);
  226.         $this->setPosition($data['position'] ?? 0);
  227.         $this->setClass($data['class'] ?? null);
  228.         $this->setTmpData([
  229.             'page' => $data['page'] ?? null,
  230.         ]);
  231.     }
  232.     public function reorderChildren(string $field): void
  233.     {
  234.         /** @var PersistentCollection $children */
  235.         $children $this->children;
  236.         if (!$children->isInitialized()) {
  237.             $children->initialize();
  238.         }
  239.         $criteria Criteria::create()
  240.             ->orderBy([$field => Criteria::ASC]);
  241.         $children $children->matching($criteria);
  242.         if (!$this->children->isInitialized()) {
  243.             $this->children->initialize();
  244.         }
  245.         $this->children->clear();
  246.         /** @var Item $child */
  247.         foreach ($children as $key => $child) {
  248.             $this->children->set($key$child);
  249.         }
  250.     }
  251.     public function getTmpData(): array
  252.     {
  253.         return $this->tmpData;
  254.     }
  255.     public function setTmpData(array $tmpData): void
  256.     {
  257.         $this->tmpData $tmpData;
  258.     }
  259. }