285 lines
6.9 KiB
PHP
285 lines
6.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Entity;
|
||
|
|
|
||
|
|
use App\Repository\QuestionRepository;
|
||
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
||
|
|
use Doctrine\Common\Collections\Collection;
|
||
|
|
use Doctrine\DBAL\Types\Types;
|
||
|
|
use Doctrine\ORM\Mapping as ORM;
|
||
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
||
|
|
|
||
|
|
#[ORM\Entity(repositoryClass: QuestionRepository::class)]
|
||
|
|
#[ORM\Table(name: 'question')]
|
||
|
|
class Question
|
||
|
|
{
|
||
|
|
#[ORM\Id]
|
||
|
|
#[ORM\GeneratedValue]
|
||
|
|
#[ORM\Column]
|
||
|
|
private ?int $id = null;
|
||
|
|
|
||
|
|
#[ORM\Column(length: 255)]
|
||
|
|
#[Assert\NotBlank(message: 'عنوان سوال الزامی است')]
|
||
|
|
#[Assert\Length(min: 10, max: 255, minMessage: 'عنوان سوال باید حداقل 10 کاراکتر باشد', maxMessage: 'عنوان سوال نمیتواند بیش از 255 کاراکتر باشد')]
|
||
|
|
private ?string $title = null;
|
||
|
|
|
||
|
|
#[ORM\Column(type: Types::TEXT)]
|
||
|
|
#[Assert\NotBlank(message: 'متن سوال الزامی است')]
|
||
|
|
#[Assert\Length(min: 20, minMessage: 'متن سوال باید حداقل 20 کاراکتر باشد')]
|
||
|
|
private ?string $content = null;
|
||
|
|
|
||
|
|
#[ORM\ManyToOne(targetEntity: User::class)]
|
||
|
|
#[ORM\JoinColumn(nullable: false)]
|
||
|
|
private ?User $author = null;
|
||
|
|
|
||
|
|
#[ORM\Column(type: 'datetime')]
|
||
|
|
private ?\DateTimeInterface $createdAt = null;
|
||
|
|
|
||
|
|
#[ORM\Column(type: 'datetime', nullable: true)]
|
||
|
|
private ?\DateTimeInterface $updatedAt = null;
|
||
|
|
|
||
|
|
#[ORM\Column(type: 'boolean')]
|
||
|
|
private bool $isSolved = false;
|
||
|
|
|
||
|
|
#[ORM\Column(type: 'integer')]
|
||
|
|
private int $views = 0;
|
||
|
|
|
||
|
|
#[ORM\Column(type: 'integer')]
|
||
|
|
private int $votes = 0;
|
||
|
|
|
||
|
|
#[ORM\Column(type: 'boolean')]
|
||
|
|
private bool $isActive = true;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @var Collection<int, Answer>
|
||
|
|
*/
|
||
|
|
#[ORM\OneToMany(targetEntity: Answer::class, mappedBy: 'question', orphanRemoval: true, cascade: ['persist'])]
|
||
|
|
private Collection $answers;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @var Collection<int, QuestionVote>
|
||
|
|
*/
|
||
|
|
#[ORM\OneToMany(targetEntity: QuestionVote::class, mappedBy: 'question', orphanRemoval: true)]
|
||
|
|
private Collection $questionVotes;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @var Collection<int, QuestionTagRelation>
|
||
|
|
*/
|
||
|
|
#[ORM\OneToMany(targetEntity: QuestionTagRelation::class, mappedBy: 'question', orphanRemoval: true, cascade: ['persist'])]
|
||
|
|
private Collection $tagRelations;
|
||
|
|
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
$this->answers = new ArrayCollection();
|
||
|
|
$this->questionVotes = new ArrayCollection();
|
||
|
|
$this->tagRelations = new ArrayCollection();
|
||
|
|
$this->createdAt = new \DateTime();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getId(): ?int
|
||
|
|
{
|
||
|
|
return $this->id;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getTitle(): ?string
|
||
|
|
{
|
||
|
|
return $this->title;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function setTitle(string $title): static
|
||
|
|
{
|
||
|
|
$this->title = $title;
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getContent(): ?string
|
||
|
|
{
|
||
|
|
return $this->content;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function setContent(string $content): static
|
||
|
|
{
|
||
|
|
$this->content = $content;
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getAuthor(): ?User
|
||
|
|
{
|
||
|
|
return $this->author;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function setAuthor(?User $author): static
|
||
|
|
{
|
||
|
|
$this->author = $author;
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getCreatedAt(): ?\DateTimeInterface
|
||
|
|
{
|
||
|
|
return $this->createdAt;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function setCreatedAt(\DateTimeInterface $createdAt): static
|
||
|
|
{
|
||
|
|
$this->createdAt = $createdAt;
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getUpdatedAt(): ?\DateTimeInterface
|
||
|
|
{
|
||
|
|
return $this->updatedAt;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function setUpdatedAt(?\DateTimeInterface $updatedAt): static
|
||
|
|
{
|
||
|
|
$this->updatedAt = $updatedAt;
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function isSolved(): bool
|
||
|
|
{
|
||
|
|
return $this->isSolved;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function setIsSolved(bool $isSolved): static
|
||
|
|
{
|
||
|
|
$this->isSolved = $isSolved;
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getViews(): int
|
||
|
|
{
|
||
|
|
return $this->views;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function setViews(int $views): static
|
||
|
|
{
|
||
|
|
$this->views = $views;
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function incrementViews(): static
|
||
|
|
{
|
||
|
|
$this->views++;
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getVotes(): int
|
||
|
|
{
|
||
|
|
return $this->votes;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function setVotes(int $votes): static
|
||
|
|
{
|
||
|
|
$this->votes = $votes;
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function isActive(): bool
|
||
|
|
{
|
||
|
|
return $this->isActive;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function setIsActive(bool $isActive): static
|
||
|
|
{
|
||
|
|
$this->isActive = $isActive;
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return Collection<int, Answer>
|
||
|
|
*/
|
||
|
|
public function getAnswers(): Collection
|
||
|
|
{
|
||
|
|
return $this->answers;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function addAnswer(Answer $answer): static
|
||
|
|
{
|
||
|
|
if (!$this->answers->contains($answer)) {
|
||
|
|
$this->answers->add($answer);
|
||
|
|
$answer->setQuestion($this);
|
||
|
|
}
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function removeAnswer(Answer $answer): static
|
||
|
|
{
|
||
|
|
if ($this->answers->removeElement($answer)) {
|
||
|
|
if ($answer->getQuestion() === $this) {
|
||
|
|
$answer->setQuestion(null);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return Collection<int, QuestionVote>
|
||
|
|
*/
|
||
|
|
public function getQuestionVotes(): Collection
|
||
|
|
{
|
||
|
|
return $this->questionVotes;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function addQuestionVote(QuestionVote $questionVote): static
|
||
|
|
{
|
||
|
|
if (!$this->questionVotes->contains($questionVote)) {
|
||
|
|
$this->questionVotes->add($questionVote);
|
||
|
|
$questionVote->setQuestion($this);
|
||
|
|
}
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function removeQuestionVote(QuestionVote $questionVote): static
|
||
|
|
{
|
||
|
|
if ($this->questionVotes->removeElement($questionVote)) {
|
||
|
|
if ($questionVote->getQuestion() === $this) {
|
||
|
|
$questionVote->setQuestion(null);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return Collection<int, QuestionTagRelation>
|
||
|
|
*/
|
||
|
|
public function getTagRelations(): Collection
|
||
|
|
{
|
||
|
|
return $this->tagRelations;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function addTagRelation(QuestionTagRelation $tagRelation): static
|
||
|
|
{
|
||
|
|
if (!$this->tagRelations->contains($tagRelation)) {
|
||
|
|
$this->tagRelations->add($tagRelation);
|
||
|
|
$tagRelation->setQuestion($this);
|
||
|
|
}
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function removeTagRelation(QuestionTagRelation $tagRelation): static
|
||
|
|
{
|
||
|
|
if ($this->tagRelations->removeElement($tagRelation)) {
|
||
|
|
if ($tagRelation->getQuestion() === $this) {
|
||
|
|
$tagRelation->setQuestion(null);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getAnswersCount(): int
|
||
|
|
{
|
||
|
|
return $this->answers->count();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getBestAnswer(): ?Answer
|
||
|
|
{
|
||
|
|
foreach ($this->answers as $answer) {
|
||
|
|
if ($answer->isAccepted()) {
|
||
|
|
return $answer;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|