<?php
namespace App\Entity\Annotation;
use App\Entity\EntityIdTrait;
use App\Entity\Music\Resource;
use App\Entity\SetUserPrePersistInterface;
use App\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\Annotation\AnnotationRepository")
* @ORM\EntityListeners({"App\EventListener\SetUserEventListener"})
*/
class Annotation implements SetUserPrePersistInterface, TimestampableInterface
{
use EntityIdTrait;
use TimestampableTrait;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
* @Groups({"annotation:write", "annotation:read", "resource:read"})
*/
private $name;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Annotation\Anchor", mappedBy="annotation", cascade={"persist"})
* @Groups({"annotation:write", "annotation:read", "resource:read"})
* @Assert\Count(min=1)
* @Assert\Valid()
*/
private $anchors;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Annotation\Block", mappedBy="annotation", cascade={"persist"})
* @Groups({"annotation:write", "annotation:read", "resource:read"})
* @ORM\OrderBy({"position" = "ASC"})
* @Assert\Valid()
*/
private $blocks;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Annotation\Tag", inversedBy="annotations")
* @ORM\OrderBy({"name" = "ASC"})
* @Groups({"annotation:read", "annotation:write", "resource:read"})
* @ORM\OrderBy({"name" = "ASC"})
*/
private $tags;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Music\Resource", inversedBy="annotations")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
* @Groups({"annotation:write", "annotation:read"})
*/
private $resource;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Annotation\Comment", mappedBy="annotation")
* @Groups({"annotation:read", "resource:read"})
* @ORM\OrderBy({"createdAt" = "ASC"})
*/
private $comments;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="annotations")
* @ORM\JoinColumn(nullable=false)
* @Groups({"annotation:read"})
*/
private $user;
public function __construct()
{
$this->anchors = new ArrayCollection();
$this->blocks = new ArrayCollection();
$this->tags = new ArrayCollection();
$this->comments = new ArrayCollection();
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
/**
* @return Collection|Anchor[]
*/
public function getAnchors(): Collection
{
return $this->anchors;
}
public function addAnchor(Anchor $anchor): void
{
if (!$this->anchors->contains($anchor)) {
$this->anchors[] = $anchor;
$anchor->setAnnotation($this);
}
}
public function removeAnchor(Anchor $anchor): void
{
if ($this->anchors->contains($anchor)) {
$this->anchors->removeElement($anchor);
// set the owning side to null (unless already changed)
if ($anchor->getAnnotation() === $this) {
$anchor->setAnnotation(null);
}
}
}
/**
* @return Collection|Block[]
*/
public function getBlocks(): Collection
{
return $this->blocks;
}
public function addBlock(Block $block): void
{
if (!$this->blocks->contains($block)) {
$this->blocks[] = $block;
$block->setAnnotation($this);
}
}
public function removeBlock(Block $block): void
{
if ($this->blocks->contains($block)) {
$this->blocks->removeElement($block);
// set the owning side to null (unless already changed)
if ($block->getAnnotation() === $this) {
$block->setAnnotation(null);
}
}
}
/**
* @return Collection|Tag[]
*/
public function getTags(): Collection
{
return $this->tags;
}
public function addTag(Tag $tag): void
{
if (!$this->tags->contains($tag)) {
$this->tags[] = $tag;
}
}
public function removeTag(Tag $tag): void
{
if ($this->tags->contains($tag)) {
$this->tags->removeElement($tag);
}
}
public function getResource(): ?Resource
{
return $this->resource;
}
public function setResource(?Resource $resource): void
{
$this->resource = $resource;
}
/**
* @return Collection|Comment[]
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comment $comment): void
{
if (!$this->comments->contains($comment)) {
$this->comments[] = $comment;
$comment->setAnnotation($this);
}
}
public function removeComment(Comment $comment): void
{
if ($this->comments->contains($comment)) {
$this->comments->removeElement($comment);
// set the owning side to null (unless already changed)
if ($comment->getAnnotation() === $this) {
$comment->setAnnotation(null);
}
}
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): void
{
$this->user = $user;
}
}