murph-core/src/core/Entity/Site/Navigation.php

256 lines
5.8 KiB
PHP
Raw Normal View History

2022-03-13 19:32:32 +01:00
<?php
namespace App\Core\Entity\Site;
use App\Core\Doctrine\Timestampable;
use App\Core\Entity\EntityInterface;
use App\Core\Entity\NavigationSetting;
use App\Core\Repository\Site\NavigationRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
2022-11-19 19:35:28 +01:00
#[ORM\Entity(repositoryClass: NavigationRepository::class)]
#[ORM\HasLifecycleCallbacks]
2022-03-13 19:32:32 +01:00
class Navigation implements EntityInterface
{
use Timestampable;
2022-11-19 19:35:28 +01:00
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
2022-03-13 19:32:32 +01:00
protected $id;
2022-11-19 19:35:28 +01:00
#[ORM\Column(type: 'string', length: 255)]
2022-03-13 19:32:32 +01:00
protected $label;
2022-11-19 19:35:28 +01:00
#[ORM\Column(type: 'string', length: 255)]
2022-03-13 19:32:32 +01:00
protected $code;
2022-11-19 19:35:28 +01:00
#[ORM\Column(type: 'string', length: 255)]
2022-03-13 19:32:32 +01:00
protected $domain;
2022-11-19 19:35:28 +01:00
#[ORM\Column(type: 'boolean', options: ['default' => 0])]
2022-03-13 19:32:32 +01:00
protected $forceDomain = false;
2022-11-19 19:35:28 +01:00
#[ORM\Column(type: 'text', nullable: true)]
2022-03-13 19:32:32 +01:00
protected $additionalDomains = '[]';
2022-11-19 19:35:28 +01:00
#[ORM\OneToMany(targetEntity: Menu::class, mappedBy: 'navigation')]
2022-03-13 19:32:32 +01:00
protected $menus;
2022-11-19 19:35:28 +01:00
#[ORM\Column(type: 'string', length: 10)]
2022-03-13 19:32:32 +01:00
protected $locale = 'en';
#[ORM\Column(type: 'string', length: 7, nullable: true)]
2023-10-12 16:15:07 +02:00
protected $color;
2022-11-19 19:35:28 +01:00
#[ORM\Column(type: 'integer', nullable: true)]
2022-03-13 19:32:32 +01:00
protected $sortOrder;
2022-11-19 19:35:28 +01:00
#[ORM\OneToMany(targetEntity: NavigationSetting::class, mappedBy: 'navigation', orphanRemoval: true)]
2022-03-13 19:32:32 +01:00
protected $navigationSettings;
public function __construct()
{
$this->menus = new ArrayCollection();
$this->navigationSettings = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getLabel(): ?string
{
return $this->label;
}
2023-10-25 19:59:28 +02:00
public function setLabel(?string $label): self
2022-03-13 19:32:32 +01:00
{
$this->label = $label;
return $this;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
public function getDomain(): ?string
{
return $this->domain;
}
public function setDomain(string $domain): self
{
$this->domain = $domain;
return $this;
}
public function getForceDomain(): ?bool
{
return $this->forceDomain;
}
public function setForceDomain(bool $forceDomain): self
{
$this->forceDomain = $forceDomain;
return $this;
}
public function getAdditionalDomains(): array
{
return (array) json_decode($this->additionalDomains, true);
}
public function setAdditionalDomains(array $additionalDomains): self
{
$this->additionalDomains = json_encode($additionalDomains);
return $this;
}
/**
* @return Collection|Menu[]
*/
public function getMenus(): Collection
{
return $this->menus;
}
public function addMenu(Menu $menu): self
{
if (!$this->menus->contains($menu)) {
$this->menus[] = $menu;
$menu->setNavigation($this);
}
return $this;
}
public function removeMenu(Menu $menu): self
{
if ($this->menus->removeElement($menu)) {
// set the owning side to null (unless already changed)
if ($menu->getNavigation() === $this) {
$menu->setNavigation(null);
}
}
return $this;
}
public function getMenu(string $code): ?Menu
{
foreach ($this->menus as $menu) {
if ($menu->getCode() === $code) {
return $menu;
}
}
return $menu;
}
public function getRouteName(): string
{
return $this->getCode() ? $this->getCode() : 'navigation_'.$this->getId();
}
public function getLocale(): ?string
{
return $this->locale;
}
public function setLocale(string $locale): self
{
$this->locale = $locale;
return $this;
}
public function getSortOrder(): ?int
{
return $this->sortOrder;
}
public function setSortOrder(?int $sortOrder): self
{
$this->sortOrder = $sortOrder;
return $this;
}
/**
* @return Collection|NavigationSetting[]
*/
public function getNavigationSettings(): Collection
{
return $this->navigationSettings;
}
public function addNavigationSetting(NavigationSetting $navigationSetting): self
{
if (!$this->navigationSettings->contains($navigationSetting)) {
$this->navigationSettings[] = $navigationSetting;
$navigationSetting->setNavigation($this);
}
return $this;
}
public function removeNavigationSetting(NavigationSetting $navigationSetting): self
{
if ($this->navigationSettings->removeElement($navigationSetting)) {
// set the owning side to null (unless already changed)
if ($navigationSetting->getNavigation() === $this) {
$navigationSetting->setNavigation(null);
}
}
return $this;
}
public function matchDomain(string $domain): bool
{
if ($domain === $this->getDomain()) {
return true;
}
foreach ($this->getAdditionalDomains() as $additionalDomain) {
2022-05-08 16:44:24 +02:00
if ('domain' === $additionalDomain['type'] && $additionalDomain['domain'] === $domain) {
return true;
}
2022-05-08 16:44:24 +02:00
if ('regexp' === $additionalDomain['type'] && preg_match('#'.$additionalDomain['domain'].'#', $domain) > 0) {
return true;
}
}
return false;
}
public function setColor(string $color): self
{
$this->color = $color;
return $this;
}
2023-08-07 19:28:51 +02:00
public function getColor(): ?string
{
return $this->color;
}
2022-03-13 19:32:32 +01:00
}