<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\Api\AnswerController;
use App\Repository\AnswerRepository;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: AnswerRepository::class)]
class Answer
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Groups("readQuestions")]
private $id;
#[ORM\Column(type: 'string', length: 255)]
#[Groups("readQuestions")]
private $value;
#[ORM\Column(type: 'datetime_immutable')]
private $createdAt;
#[ORM\ManyToOne(targetEntity: Question::class, inversedBy: 'answers', cascade: ["persist"] )]
// #[Groups("readQuestions")]
private $question;
#[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'answers', cascade: ["persist"])]
private $users;
#[ORM\ManyToMany(targetEntity: Profile::class, mappedBy: 'answers')]
private Collection $profiles;
#[ORM\OneToMany(mappedBy: 'answer', targetEntity: AnswerTranslation::class, cascade: ["persist"])]
#[Groups("readQuestions")]
private Collection $translations;
public function __construct()
{
$this->users = new ArrayCollection();
$this->createdAt = new DateTimeImmutable();
$this->profiles = new ArrayCollection();
$this->translations = new ArrayCollection();
}
public function __toString()
{
return $this->value."->".$this->getQuestion()->getContent();
}
public function getId(): ?int
{
return $this->id;
}
public function getValue(): ?string
{
return $this->value;
}
public function setValue(string $value): self
{
$this->value = $value;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getQuestion(): ?Question
{
return $this->question;
}
public function setQuestion(?Question $question): self
{
$this->question = $question;
return $this;
}
/**
* @return Collection<int, User>
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->addAnswer($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->removeElement($user)) {
$user->removeAnswer($this);
}
return $this;
}
/**
* @return Collection<int, Profile>
*/
public function getProfiles(): Collection
{
return $this->profiles;
}
public function addProfile(Profile $profile): self
{
if (!$this->profiles->contains($profile)) {
$this->profiles->add($profile);
$profile->addAnswer($this);
}
return $this;
}
public function removeProfile(Profile $profile): self
{
if ($this->profiles->removeElement($profile)) {
$profile->removeAnswer($this);
}
return $this;
}
/**
* @return Collection<int, AnswerTranslation>
*/
public function getTranslations(): Collection
{
return $this->translations;
}
public function addTranslation(AnswerTranslation $translation): self
{
if (!$this->translations->contains($translation)) {
$this->translations->add($translation);
$translation->setAnswer($this);
}
return $this;
}
public function removeTranslation(AnswerTranslation $translation): self
{
if ($this->translations->removeElement($translation)) {
// set the owning side to null (unless already changed)
if ($translation->getAnswer() === $this) {
$translation->setAnswer(null);
}
}
return $this;
}
}