<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity(repositoryClass="App\Repository\EventRepository")
*/
class Event
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups("list")
*/
private $id;
/**
* @ORM\Column(type="integer")
* @Groups("list")
*/
private $frequency;
/**
* @ORM\Column(type="string")
* @Groups("list")
*/
private $time;
/**
* @ORM\Column(type="string", length=255)
* @Groups("list")
*/
private $name;
/**
* @ORM\Column(type="integer")
*/
private $type;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Station", inversedBy="events")
*/
private $station;
/**
* @ORM\OneToMany(targetEntity="App\Entity\EventTag", mappedBy="event", orphanRemoval=true, cascade={"persist"})
* @ORM\OrderBy({"ord" = "ASC"})
*/
private $eventTags;
/**
* @ORM\Column(type="boolean")
*/
private $enabled;
public const TYPE_ROTATE = 0;
public const TYPE_PLAY = 1;
public const TYPE_ROTATE_MAIN = 2;
public static function getTypeNames(){
return [
self::TYPE_ROTATE => 'Rotate Recursive',
self::TYPE_PLAY => 'Play',
self::TYPE_ROTATE_MAIN => 'Rotate Main'
];
}
/**
* @Groups("list")
* @return mixed|string
*/
public function getTypeName(){
return self::getTypeNames()[$this->type] ?? 'Unknown';
}
public static function getFrequencyNames(){
return [
0 => 'Daily',
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
7 => 'Sunday'
];
}
public function getFrequencyName(): string
{
return self::getFrequencyNames()[$this->frequency] ?? 'Unknown';
}
public function __construct()
{
$this->eventTags = new ArrayCollection();
$this->enabled = true;
}
public function getId(): ?int
{
return $this->id;
}
public function getFrequency(): ?int
{
return $this->frequency;
}
public function setFrequency(int $frequency): self
{
$this->frequency = $frequency;
return $this;
}
public function getTime(): ?string
{
return $this->time;
}
public function getTimeInt(): int
{
return (int) $this->time;
}
public function getTimeName(): ?string
{
return self::getTimeOptions()[$this->time] ?? 'Unknown';
}
public static function getTimeOptions(): array
{
$minutes = [0];
$options = [];
for ($h=0; $h<24; $h++){
foreach ($minutes as $m){
$options[] =
str_pad($h, 2, '0', STR_PAD_LEFT).
':'.
str_pad($m, 2, '0', STR_PAD_LEFT);
}
}
return $options;
}
public function setTime(string $time): self
{
$this->time = $time;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getType(): ?int
{
return $this->type;
}
public function setType(int $type): self
{
$this->type = $type;
return $this;
}
public function getStation(): ?Station
{
return $this->station;
}
public function setStation(?Station $station): self
{
$this->station = $station;
return $this;
}
/**
* @return Collection|EventTag[]
*/
public function getEventTags(): Collection
{
return $this->eventTags->filter(fn($eventTag) => self::filterEventTags($eventTag));
}
public static function filterEventTags(EventTag $eventTag) : bool
{
$tag = $eventTag->getTag();
if (empty($tag)){
return false;
}
return !$tag->getDeleted();
}
public function addEventTag(EventTag $eventTag): self
{
if (!$this->eventTags->contains($eventTag)) {
$this->eventTags[] = $eventTag;
$eventTag->setEvent($this);
}
return $this;
}
public function removeEventTag(EventTag $eventTag): self
{
if ($this->eventTags->contains($eventTag)) {
$this->eventTags->removeElement($eventTag);
// set the owning side to null (unless already changed)
if ($eventTag->getEvent() === $this) {
$eventTag->setEvent(null);
}
}
return $this;
}
public function getEnabled(): ?bool
{
return $this->enabled;
}
public function setEnabled(bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
}