<?php
namespace App\CmsBundle\Entity;
use App\CmsBundle\Enum\LogActionEnum;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* Preset
*
* @ORM\Table(name="preset")
* @ORM\Entity(repositoryClass="App\CmsBundle\Repository\PresetRepository")
*/
class Preset
{
use LogTrait;
/**
* @ORM\Id
* @ORM\Column(type="integer", options={"unsigned":true})
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected ?int $id = null;
/**
* @ORM\Column(name="handler", type="string", length=1000)
*/
private string $handler;
/**
* @ORM\Column(name="title", type="string", length=1000)
*/
private string $title;
/**
* @ORM\OneToMany(targetEntity="App\CmsBundle\Entity\Widget", mappedBy="preset")
*/
protected Collection $widgets;
/**
* @ORM\OneToMany(targetEntity="App\CmsBundle\Entity\WidgetParameter", mappedBy="preset", cascade={"persist","remove"})
* @ORM\JoinColumn(nullable=true)
*/
private Collection $widgetParameters;
public function __construct()
{
$this->widgets = new ArrayCollection();
$this->widgetParameters = new ArrayCollection();
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @param int $id
* @return void
*/
public function setId(int $id): void
{
$this->id = $id;
}
/**
* @return Site
*/
public function getSite(): Site
{
return $this->site;
}
/**
* @param Site $site
* @return $this
*/
public function setSite(Site $site): self
{
$this->site = $site;
return $this;
}
/**
* @return string
*/
public function getHandler(): string
{
return $this->handler;
}
/**
* @param string $handler
* @return Preset
*/
public function setHandler(string $handler): self
{
$this->handler = $handler;
return $this;
}
/**
* @return string
*/
public function getTitle(): string
{
return $this->title;
}
/**
* @param string $title
* @return Preset
*/
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
/**
* @return Collection
*/
public function getWidgetParameters(): Collection
{
return $this->widgetParameters;
}
public function getWidgets(): Collection
{
return $this->widgets;
}
/**
* @param WidgetParameter $widgetParameter
* @return $this
*/
public function addWidgetParameter(WidgetParameter $widgetParameter): self
{
if (!$this->widgetParameters->contains($widgetParameter)) {
$this->addCollectionLog(
LogActionEnum::ADD,
'widgetParameter',
(object)[
'name' => $widgetParameter->getName(),
'value' => $widgetParameter->getValue()
]
);
$widgetParameter->setPreset($this);
$this->widgetParameters[] = $widgetParameter;
}
return $this;
}
/**
* @param WidgetParameter $widgetParameter
* @return $this
*/
public function removeWidgetParameter(WidgetParameter $widgetParameter): self
{
if ($this->widgetParameters->removeElement($widgetParameter)) {
$this->addCollectionLog(
LogActionEnum::REMOVE,
'widgetParameter',
(object)[
'name' => $widgetParameter->getName(),
'value' => $widgetParameter->getValue()
]
);
if ($widgetParameter->getPreset() === $this) {
$widgetParameter->setPreset(null);
}
}
return $this;
}
/**
* @param $action
* @param $property
* @param object $value
* @return $this
*/
public function addCollectionLog($action, $property, object $value): self
{
if (LogActionEnum::exists($action)) {
$log = json_decode($this->log ?? '[]', true);
$log[$property][$action][] = $value;
$this->log = json_encode($log);
}
return $this;
}
}