<?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)]
#[ApiResource(
collectionOperations: [
'get'=>['normalization_context' => ['groups' => ['readQuestions','read','profile']]],
'post',
],
itemOperations: [
'get'=>['normalization_context' => ['groups' => 'readQuestions']],
'put' => [
'method' => 'POST',
'path' => '/answer/{id}',
'controller' => AnswerController::class,
],
'delete',
],
)]
class Answer
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Groups(['readQuestions','read','profile'])]
private $id;
#[ORM\Column(type: 'string', length: 255)]
#[Groups(['readQuestions','read','profile'])]
private $value;
#[ORM\Column(type: 'datetime_immutable')]
private $createdAt;
#[ORM\ManyToOne(targetEntity: Question::class, inversedBy: 'answers', cascade: ["persist"] )]
// #[Groups(['readQuestions','read','profile'])]
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','read'])]
private Collection $translations;
#[ORM\Column(nullable: true)]
private ?bool $showSubQuestions = null;
#[ORM\OneToOne(inversedBy: 'originAnswer', cascade: ['persist', 'remove'])]
private ?Question $subQuestion = null;
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;
}
public function isShowSubQuestions(): ?bool
{
return $this->showSubQuestions;
}
public function setShowSubQuestions(?bool $showSubQuestions): self
{
$this->showSubQuestions = $showSubQuestions;
return $this;
}
#[Groups(['readQuestions','read','profile'])]
public function getQuestionId(){
return $this->getQuestion()->getId();
}
public function getSubQuestion(): ?Question
{
return $this->subQuestion;
}
public function setSubQuestion(?Question $subQuestion): static
{
$this->subQuestion = $subQuestion;
return $this;
}
#[Groups(['readQuestions','read'])]
public function getSubQuestionId(){
$result = null;
if($this->subQuestion){
$result = $this->getSubQuestion()->getId();
}
return $result;
}
public function getQuestionType(){
return $this->question->type;
}
}