<?php
namespace App\Entity;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName;
/**
* @ORM\Entity(repositoryClass="App\Repository\SongRepository")
*/
class Song
{
public const MEDIA_DIR = '/code/var/media';
public const THUMBNAILS_DIR = '/code/var/media/thumbnails';
public const MAX_SONG_DISPLAY = 10000;
public static function path($name){
return self::MEDIA_DIR."/".$name.".mp3";
}
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups("list")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups("list")
*/
private $name;
/**
* @ORM\Column(type="string", length=30)
* @Groups("list")
*/
private $playtime;
/**
* @ORM\Column(type="datetime")
*/
private $created;
/**
* @ORM\Column(type="integer")
* @Groups("list")
*/
private $plays;
/**
* @ORM\Column(type="integer")
* @Groups("list")
*/
private $rate;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Tag", inversedBy="songs")
*/
private $tags;
/**
* @ORM\OneToMany(targetEntity="App\Entity\SongStation", mappedBy="song", orphanRemoval=true)
*/
private $songStations;
/**
* @ORM\Column(type="boolean")
*/
private $deleted;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $artistLink;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $thumbnailImage;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $trackListText;
public function __construct()
{
$this->created = new DateTime();
$this->tags = new ArrayCollection();
$this->rate = 0;
$this->songStations = new ArrayCollection();
$this->deleted = false;
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function getNameWithExtension(): ?string
{
return $this->name.'.mp3';
}
public function getFullPath(): ?string
{
return self::MEDIA_DIR.'/'.$this->getNameWithExtension();
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getPlaytime(): ?string
{
return $this->playtime;
}
public function getLength() : int
{
list($minutes, $seconds) = explode(':', $this->playtime);
return ((int) $minutes * 60) + (int) $seconds;
}
public function setPlaytime(string $playtime): self
{
$this->playtime = $playtime;
return $this;
}
/**
* @return \DateTimeInterface|null
*/
public function getCreated(): ?\DateTimeInterface
{
return $this->created;
}
/**
* @Groups("list")
* @SerializedName("added")
* @return string|null
*/
public function getCreatedFormatted(): ?string
{
return $this->created->format('d.m.y');
}
public function setCreated(\DateTimeInterface $created): self
{
$this->created = $created;
return $this;
}
public function getPlays(): ?int
{
return $this->plays;
}
public function setPlays(int $plays): self
{
$this->plays = $plays;
return $this;
}
public function getRate(): ?int
{
return $this->rate;
}
public function setRate(int $rate): self
{
$this->rate = $rate;
return $this;
}
/**
* @return Collection|Tag[]
*/
public function getTags(): Collection
{
return $this->tags;
}
/**
* @Groups("list")
* @SerializedName("tags")
* @return array
*/
public function getTagJson(): array
{
$tags = $this->tags->getValues();
usort($tags, function (Tag $tag){
if ($tag->getParent() !== NULL){
return -1;
}
});
return array_map(function (Tag $tag){
return [
$tag->getId(),
$tag->getName(),
$tag->getParent() === NULL ? $tag->getColor() : NULL
]; }, $tags);
}
public function addTag(Tag $tag): self
{
if (!$this->tags->contains($tag)) {
$this->tags[] = $tag;
}
return $this;
}
public function removeTag(Tag $tag): self
{
if ($this->tags->contains($tag)) {
$this->tags->removeElement($tag);
}
return $this;
}
/**
* @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->setSong($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->getSong() === $this) {
$songStation->setSong(null);
}
}
return $this;
}
public function getDeleted(): ?bool
{
return $this->deleted;
}
public function setDeleted(bool $deleted): self
{
$this->deleted = $deleted;
return $this;
}
public function getArtistLink(): ?string
{
return $this->artistLink;
}
public function setArtistLink(?string $artistLink): self
{
$this->artistLink = $artistLink;
return $this;
}
public function getThumbnailImage(): ?string
{
return $this->thumbnailImage;
}
public function setThumbnailImage(?string $thumbnailImage): self
{
$this->thumbnailImage = $thumbnailImage;
return $this;
}
public function getTrackListText(): ?string
{
return $this->trackListText;
}
public function setTrackListText(?string $trackListText): self
{
$this->trackListText = $trackListText;
return $this;
}
}