src/Entity/Station.php line 12

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. /**
  7. * @ORM\Entity(repositoryClass="App\Repository\StationRepository")
  8. */
  9. class Station
  10. {
  11. /**
  12. * @ORM\Id()
  13. * @ORM\GeneratedValue()
  14. * @ORM\Column(type="integer")
  15. */
  16. private $id;
  17. /**
  18. * @ORM\Column(type="string", length=255)
  19. */
  20. private $name;
  21. /**
  22. * @ORM\Column(type="string", length=255)
  23. */
  24. private $shortcode;
  25. /**
  26. * @ORM\Column(type="text", nullable=true)
  27. */
  28. private $description;
  29. /**
  30. * @ORM\Column(type="string", length=255)
  31. */
  32. private $listenUrl;
  33. /**
  34. * @ORM\ManyToOne(targetEntity="App\Entity\Server", inversedBy="stations")
  35. * @ORM\JoinColumn(nullable=false)
  36. */
  37. private $server;
  38. /**
  39. * @ORM\Column(type="integer")
  40. */
  41. private $remoteId;
  42. /**
  43. * @ORM\OneToMany(targetEntity="App\Entity\Event", mappedBy="station")
  44. */
  45. private $events;
  46. /**
  47. * @ORM\OneToMany(targetEntity="App\Entity\SongStation", mappedBy="station", orphanRemoval=true)
  48. */
  49. private $songStations;
  50. /**
  51. * @ORM\Column(type="integer", nullable=true)
  52. */
  53. private $defaultPlaylistRemoteId;
  54. /**
  55. * @ORM\Column(type="string", length=255, nullable=true)
  56. */
  57. private $channelLogo;
  58. /**
  59. * @ORM\Column(type="string", length=255, nullable=true)
  60. */
  61. private $channelVideo;
  62. /**
  63. * @ORM\Column(type="string", length=12, nullable=true)
  64. */
  65. private $color;
  66. public function __toString(): string
  67. {
  68. return $this->name ?? '';
  69. }
  70. public function toArray(): array
  71. {
  72. return [
  73. 'id' => $this->id,
  74. 'name' => $this->name,
  75. 'shortcode' => $this->shortcode,
  76. 'description' => $this->description,
  77. 'listenUrl' => $this->listenUrl,
  78. 'server' => $this->server ? $this->server->getId() : null,
  79. 'remoteId' => $this->remoteId,
  80. 'defaultPlaylistRemoteId' => $this->defaultPlaylistRemoteId,
  81. 'channelLogo' => $this->channelLogo,
  82. 'channelVideo' => $this->channelVideo,
  83. ];
  84. }
  85. public function __construct()
  86. {
  87. $this->songStations = new ArrayCollection();
  88. }
  89. public function getId(): ?int
  90. {
  91. return $this->id;
  92. }
  93. public function getName(): ?string
  94. {
  95. return $this->name;
  96. }
  97. public function setName(string $name): self
  98. {
  99. $this->name = $name;
  100. return $this;
  101. }
  102. public function getShortcode(): ?string
  103. {
  104. return $this->shortcode;
  105. }
  106. public function setShortcode(string $shortcode): self
  107. {
  108. $this->shortcode = $shortcode;
  109. return $this;
  110. }
  111. public function getDescription(): ?string
  112. {
  113. return $this->description;
  114. }
  115. public function setDescription(?string $description): self
  116. {
  117. $this->description = $description;
  118. return $this;
  119. }
  120. public function getListenUrl(): ?string
  121. {
  122. return $this->listenUrl;
  123. }
  124. public function setListenUrl(string $listenUrl): self
  125. {
  126. $this->listenUrl = $listenUrl;
  127. return $this;
  128. }
  129. public function getServer(): ?Server
  130. {
  131. return $this->server;
  132. }
  133. public function setServer(?Server $server): self
  134. {
  135. $this->server = $server;
  136. return $this;
  137. }
  138. public function getRemoteId(): ?int
  139. {
  140. return $this->remoteId;
  141. }
  142. public function setRemoteId(int $remoteId): self
  143. {
  144. $this->remoteId = $remoteId;
  145. return $this;
  146. }
  147. /**
  148. * @return Collection|Event[]
  149. */
  150. public function getEvents(): Collection
  151. {
  152. return $this->events->filter(function (Event $event) {
  153. return $event->getEnabled();
  154. });
  155. }
  156. public function removeEvent(Event $event): self
  157. {
  158. if ($this->events->contains($event)) {
  159. $this->events->removeElement($event);
  160. // set the owning side to null (unless already changed)
  161. if ($event->getStation() === $this) {
  162. $event->setStation(null);
  163. }
  164. }
  165. return $this;
  166. }
  167. /**
  168. * @return Collection|Event[]
  169. */
  170. public function getEventsInOrder(): Collection
  171. {
  172. $events = $this->events->filter(function (Event $event) {
  173. return $event->getEnabled();
  174. })->toArray();
  175. usort($events, function (Event $e1, Event $e2) {
  176. return $e1->getTimeInt() > $e2->getTimeInt();
  177. });
  178. return new ArrayCollection($events);
  179. }
  180. public function getLastEventInADay(): ?Event
  181. {
  182. $event = null;
  183. foreach ($this->getEventsInOrder() as $e) {
  184. if ($e->getTimeInt() > (empty($event) ? 0 : $event->getTimeInt())) {
  185. $event = $e;
  186. }
  187. }
  188. return $event;
  189. }
  190. public function getFirstEventInADay(): ?Event
  191. {
  192. $event = null;
  193. foreach ($this->getEventsInOrder() as $e) {
  194. if ($e->getTimeInt() < (empty($event) ? 24 : $event->getTimeInt())) {
  195. $event = $e;
  196. }
  197. }
  198. return $event;
  199. }
  200. /**
  201. * @return Collection|SongStation[]
  202. */
  203. public function getSongStations(): Collection
  204. {
  205. return $this->songStations;
  206. }
  207. public function addSongStation(SongStation $songStation): self
  208. {
  209. if (!$this->songStations->contains($songStation)) {
  210. $this->songStations[] = $songStation;
  211. $songStation->setStation($this);
  212. }
  213. return $this;
  214. }
  215. public function removeSongStation(SongStation $songStation): self
  216. {
  217. if ($this->songStations->contains($songStation)) {
  218. $this->songStations->removeElement($songStation);
  219. // set the owning side to null (unless already changed)
  220. if ($songStation->getStation() === $this) {
  221. $songStation->setStation(null);
  222. }
  223. }
  224. return $this;
  225. }
  226. public function getDefaultPlaylistRemoteId(): ?int
  227. {
  228. return $this->defaultPlaylistRemoteId;
  229. }
  230. public function setDefaultPlaylistRemoteId(?int $defaultPlaylistRemoteId): self
  231. {
  232. $this->defaultPlaylistRemoteId = $defaultPlaylistRemoteId;
  233. return $this;
  234. }
  235. public function getChannelLogo(): ?string
  236. {
  237. return $this->channelLogo;
  238. }
  239. public function setChannelLogo(?string $channelLogo): self
  240. {
  241. $this->channelLogo = $channelLogo;
  242. return $this;
  243. }
  244. public function getChannelVideo(): ?string
  245. {
  246. return $this->channelVideo;
  247. }
  248. public function setChannelVideo(?string $channelVideo): self
  249. {
  250. $this->channelVideo = $channelVideo;
  251. return $this;
  252. }
  253. public function getColor(): ?string
  254. {
  255. return $this->color ?? '#4B39EF';
  256. }
  257. public function setColor(?string $color): self
  258. {
  259. $this->color = $color;
  260. return $this;
  261. }
  262. }