<?php
namespace App\Entity\Channel\PositioningTest;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use DrosalysWeb\ObjectExtensions\Blame\Model\BlameInterface;
use DrosalysWeb\ObjectExtensions\Blame\Model\BlameTrait;
use DrosalysWeb\ObjectExtensions\Timestamp\Model\TimestampInterface;
use DrosalysWeb\ObjectExtensions\Timestamp\Model\TimestampTrait;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
#[
ORM\Entity(),
ORM\Table(name: 'channel_positioning_test_questions'),
]
class Question implements TimestampInterface, BlameInterface
{
use TimestampTrait;
use BlameTrait;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;
#[ORM\Column(type: 'boolean')]
private bool $multiple = false;
#[ORM\Column(type: 'string', length: 255)]
#[
Assert\NotBlank(message: 'positioningTest.question.label.NotBlank'),
Assert\Length(
max: 255,
maxMessage: 'positioningTest.question.label.Length.max'
)
]
private ?string $label = null;
#[ORM\Column(type: 'text', length: 5000, nullable: true)]
#[
Assert\Length(
max: 5000,
maxMessage: 'positioningTest.question.description.Length.max'
)
]
private ?string $description = null;
#[ORM\Column(type: 'text', length: 5000, nullable: true)]
#[
Assert\Length(
max: 5000,
maxMessage: 'positioningTest.question.video_url.Length.max'
)
]
private ?string $videoUrl = null;
#[ORM\Column(type: 'integer')]
private ?int $rank = null;
#[ORM\ManyToOne(targetEntity: Block::class, inversedBy: 'questions')]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
private ?Block $block = null;
#[ORM\OneToMany(mappedBy: 'question', targetEntity: FileQuestion::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
private ?Collection $files;
#[ORM\OneToMany(mappedBy: 'question', targetEntity: QuestionAnswer::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
private ?Collection $answers;
public function __construct()
{
$this->files = new ArrayCollection();
$this->answers = new ArrayCollection();
}
#[Assert\Callback]
public function validateAnswers(ExecutionContextInterface $context): void
{
if (!$this->getAnswers()->exists(fn($k, QuestionAnswer $questionAnswer) => $questionAnswer->isValid())) {
$context->buildViolation('positioningTest.question.answers.hasValid')
->atPath('answers')
->addViolation()
;
}
}
public function getId(): ?int
{
return $this->id;
}
public function isMultiple(): bool
{
return $this->multiple;
}
public function setMultiple(bool $multiple): static
{
$this->multiple = $multiple;
return $this;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(?string $label): static
{
$this->label = $label;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): static
{
$this->description = $description;
return $this;
}
public function getVideoUrl(): ?string
{
return $this->videoUrl;
}
public function setVideoUrl(?string $videoUrl): static
{
$this->videoUrl = $videoUrl;
return $this;
}
public function getRank(): ?int
{
return $this->rank;
}
public function setRank(?int $rank): static
{
$this->rank = $rank;
return $this;
}
public function getBlock(): ?Block
{
return $this->block;
}
public function setBlock(?Block $block): static
{
$this->block = $block;
return $this;
}
/**
* @return Collection<int, FileQuestion>|null
*/
public function getFiles(): ?Collection
{
return $this->files;
}
public function addFile(FileQuestion $file): self
{
if (!$this->files->contains($file)) {
$file->setQuestion($this);
$this->files[] = $file;
}
return $this;
}
public function removeFile(FileQuestion $file): self
{
if ($this->files->contains($file)) {
$this->files->removeElement($file);
}
return $this;
}
/**
* @return Collection<int, QuestionAnswer>|null
*/
public function getAnswers(): ?Collection
{
return $this->answers;
}
public function addAnswer(QuestionAnswer $answer): self
{
if (!$this->answers->contains($answer)) {
$answer->setQuestion($this);
$this->answers->add($answer);
}
return $this;
}
public function removeAnswer(QuestionAnswer $answer): self
{
if ($this->answers->contains($answer)) {
$this->answers->removeElement($answer);
}
return $this;
}
}