<?php
namespace App\Entity;
use App\Repository\ChatRepository;
use App\Entity\Traits\Timestampable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Groups;
#[ORM\HasLifecycleCallbacks]
#[ORM\Entity(repositoryClass: ChatRepository::class)]
class Chat
{
use Timestampable;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['getChats', 'chatView', 'chatAllList'])]
private ?int $id = null;
#[ORM\OneToMany(mappedBy: 'chat', targetEntity: Message::class)]
#[Groups(['getChats', 'chatView', 'chatAllList'])]
private Collection $messages;
#[ORM\ManyToOne(inversedBy: 'chats')]
#[ORM\JoinColumn(nullable: false)]
#[Groups(['chatView', 'chatAllList'])]
private ?User $initiator = null;
#[ORM\ManyToOne(inversedBy: 'chats')]
#[ORM\JoinColumn(nullable: false)]
#[Groups(['chatView', 'chatAllList'])]
private ?User $recipient = null;
#[ORM\ManyToOne(inversedBy: 'chats')]
#[ORM\JoinColumn(nullable: false)]
private ?SchoolYear $year = null;
#[ORM\OneToMany(mappedBy: 'chat', targetEntity: ClassYear::class)]
private Collection $classes;
public function __construct()
{
$this->messages = new ArrayCollection();
$this->classes = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return Collection<int, Message>
*/
public function getMessages(): Collection
{
return $this->messages;
}
public function addMessage(Message $message): static
{
if (!$this->messages->contains($message)) {
$this->messages->add($message);
$message->setChat($this);
}
return $this;
}
public function removeMessage(Message $message): static
{
if ($this->messages->removeElement($message)) {
// set the owning side to null (unless already changed)
if ($message->getChat() === $this) {
$message->setChat(null);
}
}
return $this;
}
public function getInitiator(): ?User
{
return $this->initiator;
}
public function setInitiator(?User $initiator): static
{
$this->initiator = $initiator;
return $this;
}
public function getRecipient(): ?User
{
return $this->recipient;
}
public function setRecipient(?User $recipient): static
{
$this->recipient = $recipient;
return $this;
}
public function getYear(): ?SchoolYear
{
return $this->year;
}
public function setYear(?SchoolYear $year): static
{
$this->year = $year;
return $this;
}
/**
* @return Collection<int, ClassYear>
*/
public function getClasses(): Collection
{
return $this->classes;
}
public function addClass(ClassYear $class): static
{
if (!$this->classes->contains($class)) {
$this->classes->add($class);
$class->setChat($this);
}
return $this;
}
public function removeClass(ClassYear $class): static
{
if ($this->classes->removeElement($class)) {
// set the owning side to null (unless already changed)
if ($class->getChat() === $this) {
$class->setChat(null);
}
}
return $this;
}
}