src/Entity/Event.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Serializer\Annotation\Groups;
  7. /**
  8. * @ORM\Entity(repositoryClass="App\Repository\EventRepository")
  9. */
  10. class Event
  11. {
  12. /**
  13. * @ORM\Id()
  14. * @ORM\GeneratedValue()
  15. * @ORM\Column(type="integer")
  16. * @Groups("list")
  17. */
  18. private $id;
  19. /**
  20. * @ORM\Column(type="integer")
  21. * @Groups("list")
  22. */
  23. private $frequency;
  24. /**
  25. * @ORM\Column(type="string")
  26. * @Groups("list")
  27. */
  28. private $time;
  29. /**
  30. * @ORM\Column(type="string", length=255)
  31. * @Groups("list")
  32. */
  33. private $name;
  34. /**
  35. * @ORM\Column(type="integer")
  36. */
  37. private $type;
  38. /**
  39. * @ORM\ManyToOne(targetEntity="App\Entity\Station", inversedBy="events")
  40. */
  41. private $station;
  42. /**
  43. * @ORM\OneToMany(targetEntity="App\Entity\EventTag", mappedBy="event", orphanRemoval=true, cascade={"persist"})
  44. * @ORM\OrderBy({"ord" = "ASC"})
  45. */
  46. private $eventTags;
  47. /**
  48. * @ORM\Column(type="boolean")
  49. */
  50. private $enabled;
  51. public const TYPE_ROTATE = 0;
  52. public const TYPE_PLAY = 1;
  53. public const TYPE_ROTATE_MAIN = 2;
  54. public static function getTypeNames(){
  55. return [
  56. self::TYPE_ROTATE => 'Rotate Recursive',
  57. self::TYPE_PLAY => 'Play',
  58. self::TYPE_ROTATE_MAIN => 'Rotate Main'
  59. ];
  60. }
  61. /**
  62. * @Groups("list")
  63. * @return mixed|string
  64. */
  65. public function getTypeName(){
  66. return self::getTypeNames()[$this->type] ?? 'Unknown';
  67. }
  68. public static function getFrequencyNames(){
  69. return [
  70. 0 => 'Daily',
  71. 1 => 'Monday',
  72. 2 => 'Tuesday',
  73. 3 => 'Wednesday',
  74. 4 => 'Thursday',
  75. 5 => 'Friday',
  76. 6 => 'Saturday',
  77. 7 => 'Sunday'
  78. ];
  79. }
  80. public function getFrequencyName(): string
  81. {
  82. return self::getFrequencyNames()[$this->frequency] ?? 'Unknown';
  83. }
  84. public function __construct()
  85. {
  86. $this->eventTags = new ArrayCollection();
  87. $this->enabled = true;
  88. }
  89. public function getId(): ?int
  90. {
  91. return $this->id;
  92. }
  93. public function getFrequency(): ?int
  94. {
  95. return $this->frequency;
  96. }
  97. public function setFrequency(int $frequency): self
  98. {
  99. $this->frequency = $frequency;
  100. return $this;
  101. }
  102. public function getTime(): ?string
  103. {
  104. return $this->time;
  105. }
  106. public function getTimeInt(): int
  107. {
  108. return (int) $this->time;
  109. }
  110. public function getTimeName(): ?string
  111. {
  112. return self::getTimeOptions()[$this->time] ?? 'Unknown';
  113. }
  114. public static function getTimeOptions(): array
  115. {
  116. $minutes = [0];
  117. $options = [];
  118. for ($h=0; $h<24; $h++){
  119. foreach ($minutes as $m){
  120. $options[] =
  121. str_pad($h, 2, '0', STR_PAD_LEFT).
  122. ':'.
  123. str_pad($m, 2, '0', STR_PAD_LEFT);
  124. }
  125. }
  126. return $options;
  127. }
  128. public function setTime(string $time): self
  129. {
  130. $this->time = $time;
  131. return $this;
  132. }
  133. public function getName(): ?string
  134. {
  135. return $this->name;
  136. }
  137. public function setName(string $name): self
  138. {
  139. $this->name = $name;
  140. return $this;
  141. }
  142. public function getType(): ?int
  143. {
  144. return $this->type;
  145. }
  146. public function setType(int $type): self
  147. {
  148. $this->type = $type;
  149. return $this;
  150. }
  151. public function getStation(): ?Station
  152. {
  153. return $this->station;
  154. }
  155. public function setStation(?Station $station): self
  156. {
  157. $this->station = $station;
  158. return $this;
  159. }
  160. /**
  161. * @return Collection|EventTag[]
  162. */
  163. public function getEventTags(): Collection
  164. {
  165. return $this->eventTags->filter(fn($eventTag) => self::filterEventTags($eventTag));
  166. }
  167. public static function filterEventTags(EventTag $eventTag) : bool
  168. {
  169. $tag = $eventTag->getTag();
  170. if (empty($tag)){
  171. return false;
  172. }
  173. return !$tag->getDeleted();
  174. }
  175. public function addEventTag(EventTag $eventTag): self
  176. {
  177. if (!$this->eventTags->contains($eventTag)) {
  178. $this->eventTags[] = $eventTag;
  179. $eventTag->setEvent($this);
  180. }
  181. return $this;
  182. }
  183. public function removeEventTag(EventTag $eventTag): self
  184. {
  185. if ($this->eventTags->contains($eventTag)) {
  186. $this->eventTags->removeElement($eventTag);
  187. // set the owning side to null (unless already changed)
  188. if ($eventTag->getEvent() === $this) {
  189. $eventTag->setEvent(null);
  190. }
  191. }
  192. return $this;
  193. }
  194. public function getEnabled(): ?bool
  195. {
  196. return $this->enabled;
  197. }
  198. public function setEnabled(bool $enabled): self
  199. {
  200. $this->enabled = $enabled;
  201. return $this;
  202. }
  203. }