<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
use Symfony\Component\Serializer\Annotation\Groups;
use App\Repository\QuestionRepository;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: QuestionRepository::class)]
#[ApiResource(
normalizationContext: ['groups' => ['profile','read','readDeep','readQuestions']],
denormalizationContext: ['groups' => ['profile','write']],
)]
#[ApiFilter(ExistsFilter::class, properties: ['originAnswer'])]
class Question
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Groups(["read","readDeep",'profile'])]
private $id;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[Groups(["read","readDeep"])]
private $content;
#[ORM\Column(type: 'datetime_immutable')]
private $createdAt;
#[ORM\Column(type: 'boolean', nullable: true)]
private $isEnabled;
#[ORM\Column(type: 'integer', nullable: true)]
private $position;
#[ORM\OneToMany(mappedBy: 'question', targetEntity: Answer::class , cascade: ["persist"])]
#[Groups(["read","readDeep"])]
private $answers;
#[ORM\Column(type: 'boolean', nullable: true)]
// #[Groups(["read","readDeep"])]
#[Groups(["read","readDeep"])]
private $isRequired;
#[ORM\Column(type: 'integer', nullable: true)]
// #[Groups(["read","readDeep"])]
#[Groups(["read","readDeep"])]
private $min;
#[ORM\Column(type: 'integer', nullable: true)]
// #[Groups(["read","readDeep"])]
#[Groups(["read","readDeep"])]
private $max;
#[ORM\OneToMany(mappedBy: 'question', targetEntity: QuestionTranslation::class,cascade:["persist","remove"])]
#[Groups(["read","readDeep"])]
private Collection $translations;
#[ORM\OneToOne(mappedBy: 'subQuestion', cascade: ['persist', 'remove'])]
private ?Answer $originAnswer = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $type = null;
#[ORM\Column(nullable: true)]
#[Groups(["read","readDeep"])]
private ?bool $isMultipleChoices = null;
#[ORM\Column(nullable: true)]
#[Groups(["read","readDeep"])]
private ?int $minChoices = null;
public function __construct()
{
$this->answers = new ArrayCollection();
$this->createdAt = new DateTimeImmutable();
$this->translations = new ArrayCollection();
}
public function __toString()
{
return (string)$this->content;
}
public function getId(): ?int
{
return $this->id;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function isIsEnabled(): ?bool
{
return $this->isEnabled;
}
public function setIsEnabled(?bool $isEnabled): self
{
$this->isEnabled = $isEnabled;
return $this;
}
public function getPosition(): ?int
{
return $this->position;
}
public function setPosition(?int $position): self
{
$this->position = $position;
return $this;
}
/**
* @return Collection<int, Answer>
*/
public function getAnswers(): Collection
{
return $this->answers;
}
public function addAnswer(Answer $answer): self
{
if (!$this->answers->contains($answer)) {
$this->answers[] = $answer;
$answer->setQuestion($this);
}
return $this;
}
public function removeAnswer(Answer $answer): self
{
if ($this->answers->removeElement($answer)) {
// set the owning side to null (unless already changed)
if ($answer->getQuestion() === $this) {
$answer->setQuestion(null);
}
}
return $this;
}
public function getIsRequired(): ?bool
{
return $this->isRequired;
}
public function setIsRequired(?bool $isRequired): self
{
$this->isRequired = $isRequired;
return $this;
}
public function getMin(): ?int
{
return $this->min;
}
public function setMin(?int $min): self
{
$this->min = $min;
return $this;
}
public function getMax(): ?int
{
return $this->max;
}
public function setMax(?int $max): self
{
$this->max = $max;
return $this;
}
/**
* @return Collection<int, QuestionTranslation>
*/
public function getTranslations(): Collection
{
return $this->translations;
}
public function addTranslation(QuestionTranslation $translation): self
{
if (!$this->translations->contains($translation)) {
$this->translations->add($translation);
$translation->setQuestion($this);
}
return $this;
}
public function removeTranslation(QuestionTranslation $translation): self
{
if ($this->translations->removeElement($translation)) {
// set the owning side to null (unless already changed)
if ($translation->getQuestion() === $this) {
$translation->setQuestion(null);
}
}
return $this;
}
public function getOriginAnswer(): ?Answer
{
return $this->originAnswer;
}
public function setOriginAnswer(?Answer $originAnswer): static
{
// unset the owning side of the relation if necessary
if ($originAnswer === null && $this->originAnswer !== null) {
$this->originAnswer->setSubQuestion(null);
}
// set the owning side of the relation if necessary
if ($originAnswer !== null && $originAnswer->getSubQuestion() !== $this) {
$originAnswer->setSubQuestion($this);
}
$this->originAnswer = $originAnswer;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(?string $type): static
{
$this->type = $type;
return $this;
}
public function isIsMultipleChoices(): ?bool
{
return $this->isMultipleChoices;
}
public function setIsMultipleChoices(?bool $isMultipleChoices): static
{
$this->isMultipleChoices = $isMultipleChoices;
return $this;
}
public function getMinChoices(): ?int
{
return $this->minChoices;
}
public function setMinChoices(?int $minChoices): static
{
$this->minChoices = $minChoices;
return $this;
}
}