<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\StationRepository")
*/
class Station
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $shortcode;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="string", length=255)
*/
private $listenUrl;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Server", inversedBy="stations")
* @ORM\JoinColumn(nullable=false)
*/
private $server;
/**
* @ORM\Column(type="integer")
*/
private $remoteId;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Event", mappedBy="station")
*/
private $events;
/**
* @ORM\OneToMany(targetEntity="App\Entity\SongStation", mappedBy="station", orphanRemoval=true)
*/
private $songStations;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $defaultPlaylistRemoteId;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $channelLogo;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $channelVideo;
/**
* @ORM\Column(type="string", length=12, nullable=true)
*/
private $color;
public function __toString(): string
{
return $this->name ?? '';
}
public function toArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'shortcode' => $this->shortcode,
'description' => $this->description,
'listenUrl' => $this->listenUrl,
'server' => $this->server ? $this->server->getId() : null,
'remoteId' => $this->remoteId,
'defaultPlaylistRemoteId' => $this->defaultPlaylistRemoteId,
'channelLogo' => $this->channelLogo,
'channelVideo' => $this->channelVideo,
];
}
public function __construct()
{
$this->songStations = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getShortcode(): ?string
{
return $this->shortcode;
}
public function setShortcode(string $shortcode): self
{
$this->shortcode = $shortcode;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getListenUrl(): ?string
{
return $this->listenUrl;
}
public function setListenUrl(string $listenUrl): self
{
$this->listenUrl = $listenUrl;
return $this;
}
public function getServer(): ?Server
{
return $this->server;
}
public function setServer(?Server $server): self
{
$this->server = $server;
return $this;
}
public function getRemoteId(): ?int
{
return $this->remoteId;
}
public function setRemoteId(int $remoteId): self
{
$this->remoteId = $remoteId;
return $this;
}
/**
* @return Collection|Event[]
*/
public function getEvents(): Collection
{
return $this->events->filter(function (Event $event) {
return $event->getEnabled();
});
}
public function removeEvent(Event $event): self
{
if ($this->events->contains($event)) {
$this->events->removeElement($event);
// set the owning side to null (unless already changed)
if ($event->getStation() === $this) {
$event->setStation(null);
}
}
return $this;
}
/**
* @return Collection|Event[]
*/
public function getEventsInOrder(): Collection
{
$events = $this->events->filter(function (Event $event) {
return $event->getEnabled();
})->toArray();
usort($events, function (Event $e1, Event $e2) {
return $e1->getTimeInt() > $e2->getTimeInt();
});
return new ArrayCollection($events);
}
public function getLastEventInADay(): ?Event
{
$event = null;
foreach ($this->getEventsInOrder() as $e) {
if ($e->getTimeInt() > (empty($event) ? 0 : $event->getTimeInt())) {
$event = $e;
}
}
return $event;
}
public function getFirstEventInADay(): ?Event
{
$event = null;
foreach ($this->getEventsInOrder() as $e) {
if ($e->getTimeInt() < (empty($event) ? 24 : $event->getTimeInt())) {
$event = $e;
}
}
return $event;
}
/**
* @return Collection|SongStation[]
*/
public function getSongStations(): Collection
{
return $this->songStations;
}
public function addSongStation(SongStation $songStation): self
{
if (!$this->songStations->contains($songStation)) {
$this->songStations[] = $songStation;
$songStation->setStation($this);
}
return $this;
}
public function removeSongStation(SongStation $songStation): self
{
if ($this->songStations->contains($songStation)) {
$this->songStations->removeElement($songStation);
// set the owning side to null (unless already changed)
if ($songStation->getStation() === $this) {
$songStation->setStation(null);
}
}
return $this;
}
public function getDefaultPlaylistRemoteId(): ?int
{
return $this->defaultPlaylistRemoteId;
}
public function setDefaultPlaylistRemoteId(?int $defaultPlaylistRemoteId): self
{
$this->defaultPlaylistRemoteId = $defaultPlaylistRemoteId;
return $this;
}
public function getChannelLogo(): ?string
{
return $this->channelLogo;
}
public function setChannelLogo(?string $channelLogo): self
{
$this->channelLogo = $channelLogo;
return $this;
}
public function getChannelVideo(): ?string
{
return $this->channelVideo;
}
public function setChannelVideo(?string $channelVideo): self
{
$this->channelVideo = $channelVideo;
return $this;
}
public function getColor(): ?string
{
return $this->color ?? '#4B39EF';
}
public function setColor(?string $color): self
{
$this->color = $color;
return $this;
}
}