<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\Api\BookUnits;
use App\Controller\Api\BookBonus;
use App\Controller\Api\BookVideos;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* attributes={"normalization_context"={"groups"={"Get"}},"order"={"position": "ASC"}},
* collectionOperations={"get"},
* itemOperations={
* "get",
* "units"={
* "method"="GET",
* "path"="/books/{id}/units",
* "controller"=BookUnits::class,
* },
* "bonus"={
* "method"="GET",
* "path"="/books/{id}/bonus",
* "controller"=BookBonus::class,
* },
* "videos"={
* "method"="GET",
* "path"="/books/{id}/videos",
* "controller"=BookVideos::class,
* }
* },
* )
*
* @ORM\Table(name="wow_book")
* @ORM\Entity(repositoryClass="App\Repository\BookRepository")
*/
class Book
{
/**
* @Groups({"Get"})
*
* @ApiProperty(
* attributes={
* "swagger_context"={
* "name"="BookId",
* "type"="int",
* }
* }
* )
*
* @var int The identifier of a Book
*
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @Groups({"Get"})
*
* @ApiProperty(
* attributes={
* "swagger_context"={
* "name"="BookId",
* "type"="int",
* }
* }
* )
*
* @var string The Books title (for example: red)
*
* @ORM\Column(type="string", length=100, unique=true)
*/
private $title;
/**
* @var int The Books position for sorting
*
* @ORM\Column(type="integer")
*/
private $position;
/**
* @var Collection|Unit[]
*
* @ORM\OneToMany(targetEntity="App\Entity\Unit", mappedBy="book", cascade={"merge", "persist"}, orphanRemoval=true)
* @ORM\OrderBy({"number" = "ASC"})
*/
private $units;
/**
* @var Collection|Video[]
*
* @ORM\OneToMany(targetEntity="App\Entity\Video", mappedBy="book", cascade={"merge", "persist"}, orphanRemoval=true)
*/
private $videos;
/**
* @Groups({"Get"})
*
* @ApiProperty(
* attributes={
* "swagger_context"={
* "name"="IconURIs",
* "type"="collection",
* }
* }
* )
*
* @var Collection|BookIcon[] A collection of icon uris KeyPair<string, string>
*
* @ORM\OneToMany(targetEntity="App\Entity\BookIcon", mappedBy="book", cascade={"merge", "persist"}, orphanRemoval=true)
*/
private $bookIcons;
/**
* @var Collection|Subscription[]
*
* @ORM\OneToMany(targetEntity="App\Entity\Subscription", mappedBy="book", cascade={"persist"})
*/
private $subscriptions;
/**
* Book constructor.
*/
public function __construct()
{
$this->units = new ArrayCollection();
$this->videos = new ArrayCollection();
$this->bookIcons = new ArrayCollection();
$this->subscriptions = new ArrayCollection();
}
public function __toString()
{
return $this->title;
}
/*
* Getters
*/
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @return string
*/
public function getTitle(): string
{
return $this->title;
}
/**
* @return int
*/
public function getPosition(): int
{
return $this->position;
}
/**
* @return Collection|Unit[]
*/
public function getUnits(): Collection
{
return $this->units;
}
/**
* @return Collection|Unit[]
*/
public function getBonusUnits(): Collection
{
return $this->units->filter(function (Unit $unit) {
return $unit->getType() === Unit::TYPE_BONUS;
});
}
/**
* @return Collection|Unit[]
*/
public function getEducationUnits(): Collection
{
return $this->units->filter(function (Unit $unit) {
return $unit->getType() === Unit::TYPE_EDUCATION;
});
}
/**
* @return Collection|Video[]
*/
public function getVideos(): Collection
{
return $this->videos;
}
/**
* @return Collection|BookIcon[]
*/
public function getBookIcons(): Collection
{
return $this->bookIcons;
}
/**
* @param $resolution
* @return null|BookIcon
*/
public function findBookIconByResolution($resolution): ?BookIcon
{
$criteria = Criteria::create()->where(Criteria::expr()->eq('resolution', $resolution));
return $this->bookIcons->matching($criteria)->get(0);
}
/**
* @param User $user
* @return array
*/
public function getVideosByUser(User $user)
{
if ($user->findActiveSubscriptionByBook($this)) {
return $this->videos;
}
return $this->videos->filter(function (Video $video) {
return $video->getUnit()->getNumber() === Unit::FREE_UNIT_NUMBER && $video->getUnit()->getType() === Unit::TYPE_EDUCATION;
})->getValues();
}
/*
* Setters
*/
/**
* @param string $title
* @return Book
*/
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
/**
* @param int $position
* @return Book
*/
public function setPosition(int $position): self
{
$this->position = $position;
return $this;
}
/**
* @param Unit $unit
* @return Book
*/
public function addUnit(Unit $unit): self
{
if (!$this->units->contains($unit)) {
$this->units[] = $unit;
$unit->setBook($this);
}
return $this;
}
/**
* @param Unit $unit
* @return Book
*/
public function removeUnit(Unit $unit): self
{
if ($this->units->contains($unit)) {
$this->units->removeElement($unit);
// set the owning side to null (unless already changed)
if ($unit->getBook() === $this) {
$unit->setBook(null);
}
}
return $this;
}
/**
* @param Video $video
* @return Book
*/
public function addVideo(Video $video): self
{
if (!$this->videos->contains($video)) {
$this->videos[] = $video;
$video->setBook($this);
}
return $this;
}
/**
* @param Video $video
* @return Book
*/
public function removeVideo(Video $video): self
{
if ($this->videos->contains($video)) {
$this->videos->removeElement($video);
// set the owning side to null (unless already changed)
if ($video->getBook() === $this) {
$video->setBook(null);
}
}
return $this;
}
/**
* @param BookIcon $bookIcon
* @return Book
*/
public function addBookIcon(BookIcon $bookIcon): self
{
if (!$this->bookIcons->contains($bookIcon)) {
$this->bookIcons[] = $bookIcon;
$bookIcon->setBook($this);
}
return $this;
}
/**
* @param BookIcon $bookIcon
* @return Book
*/
public function removeBookIcon(BookIcon $bookIcon): self
{
if ($this->bookIcons->contains($bookIcon)) {
$this->bookIcons->removeElement($bookIcon);
// set the owning side to null (unless already changed)
if ($bookIcon->getBook() === $this) {
$bookIcon->setBook(null);
}
}
return $this;
}
}