src/Entity/Answer.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Controller\Api\AnswerController;
  5. use App\Repository\AnswerRepository;
  6. use DateTimeImmutable;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Symfony\Component\Serializer\Annotation\Groups;
  11. #[ORM\Entity(repositoryClassAnswerRepository::class)]
  12. #[ApiResource(
  13.     collectionOperations: [
  14.         'get'=>['normalization_context' => ['groups' => ['readQuestions','read','profile']]],
  15.         'post',
  16.     ],
  17.     itemOperations: [
  18.         'get'=>['normalization_context' => ['groups' => 'readQuestions']],
  19.         'put' => [
  20.             'method' => 'POST',
  21.             'path' => '/answer/{id}',
  22.             'controller' => AnswerController::class,
  23.         ],
  24.         'delete',
  25.         
  26.     ],
  27. )]
  28. class Answer
  29. {
  30.     #[ORM\Id]
  31.     #[ORM\GeneratedValue]
  32.     #[ORM\Column(type'integer')]
  33.     #[Groups(['readQuestions','read','profile'])]
  34.     private $id;
  35.     #[ORM\Column(type'string'length255)]
  36.     #[Groups(['readQuestions','read','profile'])]
  37.     private $value;
  38.     #[ORM\Column(type'datetime_immutable')]
  39.     private $createdAt;
  40.     #[ORM\ManyToOne(targetEntityQuestion::class, inversedBy'answers'cascade: ["persist"] )]
  41.     // #[Groups(['readQuestions','read','profile'])]
  42.     private $question;
  43.     #[ORM\ManyToMany(targetEntityUser::class, mappedBy'answers'cascade: ["persist"])]
  44.     private $users;
  45.     #[ORM\ManyToMany(targetEntityProfile::class, mappedBy'answers')]
  46.     private Collection $profiles;
  47.     #[ORM\OneToMany(mappedBy'answer'targetEntityAnswerTranslation::class, cascade: ["persist"])]
  48.     #[Groups(['readQuestions','read'])]
  49.     private Collection $translations;
  50.     #[ORM\Column(nullabletrue)]
  51.     private ?bool $showSubQuestions null;
  52.     #[ORM\OneToOne(inversedBy'originAnswer'cascade: ['persist''remove'])]
  53.     private ?Question $subQuestion null;
  54.    
  55.     public function __construct()
  56.     {
  57.         $this->users = new ArrayCollection();
  58.         $this->createdAt = new DateTimeImmutable();
  59.         $this->profiles = new ArrayCollection();
  60.         $this->translations = new ArrayCollection();
  61.     }
  62.     public function __toString()
  63.     {
  64.         return $this->value."->".$this->getQuestion()->getContent();
  65.     }
  66.     public function getId(): ?int
  67.     {
  68.         return $this->id;
  69.     }
  70.     public function getValue(): ?string
  71.     {
  72.         return $this->value;
  73.     }
  74.     public function setValue(string $value): self
  75.     {
  76.         $this->value $value;
  77.         return $this;
  78.     }
  79.     public function getCreatedAt(): ?\DateTimeImmutable
  80.     {
  81.         return $this->createdAt;
  82.     }
  83.     public function setCreatedAt(\DateTimeImmutable $createdAt): self
  84.     {
  85.         $this->createdAt $createdAt;
  86.         return $this;
  87.     }
  88.     public function getQuestion(): ?Question
  89.     {
  90.         return $this->question;
  91.     }
  92.     public function setQuestion(?Question $question): self
  93.     {
  94.         $this->question $question;
  95.         return $this;
  96.     }
  97.     /**
  98.      * @return Collection<int, User>
  99.      */
  100.     public function getUsers(): Collection
  101.     {
  102.         return $this->users;
  103.     }
  104.     public function addUser(User $user): self
  105.     {
  106.         if (!$this->users->contains($user)) {
  107.             $this->users[] = $user;
  108.             $user->addAnswer($this);
  109.         }
  110.         return $this;
  111.     }
  112.     public function removeUser(User $user): self
  113.     {
  114.         if ($this->users->removeElement($user)) {
  115.             $user->removeAnswer($this);
  116.         }
  117.         return $this;
  118.     }
  119.     /**
  120.      * @return Collection<int, Profile>
  121.      */
  122.     public function getProfiles(): Collection
  123.     {
  124.         return $this->profiles;
  125.     }
  126.     public function addProfile(Profile $profile): self
  127.     {
  128.         if (!$this->profiles->contains($profile)) {
  129.             $this->profiles->add($profile);
  130.             $profile->addAnswer($this);
  131.         }
  132.         return $this;
  133.     }
  134.     public function removeProfile(Profile $profile): self
  135.     {
  136.         if ($this->profiles->removeElement($profile)) {
  137.             $profile->removeAnswer($this);
  138.         }
  139.         return $this;
  140.     }
  141.     /**
  142.      * @return Collection<int, AnswerTranslation>
  143.      */
  144.     public function getTranslations(): Collection
  145.     {
  146.         return $this->translations;
  147.     }
  148.     public function addTranslation(AnswerTranslation $translation): self
  149.     {
  150.         if (!$this->translations->contains($translation)) {
  151.             $this->translations->add($translation);
  152.             $translation->setAnswer($this);
  153.         }
  154.         return $this;
  155.     }
  156.     public function removeTranslation(AnswerTranslation $translation): self
  157.     {
  158.         if ($this->translations->removeElement($translation)) {
  159.             // set the owning side to null (unless already changed)
  160.             if ($translation->getAnswer() === $this) {
  161.                 $translation->setAnswer(null);
  162.             }
  163.         }
  164.         return $this;
  165.     }
  166.     public function isShowSubQuestions(): ?bool
  167.     {
  168.         return $this->showSubQuestions;
  169.     }
  170.     public function setShowSubQuestions(?bool $showSubQuestions): self
  171.     {
  172.         $this->showSubQuestions $showSubQuestions;
  173.         return $this;
  174.     }
  175.     #[Groups(['readQuestions','read','profile'])]
  176.     public function getQuestionId(){
  177.         return $this->getQuestion()->getId();
  178.     }
  179.     public function getSubQuestion(): ?Question
  180.     {
  181.         return $this->subQuestion;
  182.     }
  183.     public function setSubQuestion(?Question $subQuestion): static
  184.     {
  185.         $this->subQuestion $subQuestion;
  186.         return $this;
  187.     }
  188.     
  189.      #[Groups(['readQuestions','read'])]
  190.     public function getSubQuestionId(){
  191.         $result null;
  192.         
  193.         if($this->subQuestion){
  194.             $result $this->getSubQuestion()->getId();
  195.         }
  196.         
  197.         
  198.         return $result;
  199.         
  200.     }
  201.     
  202.     public function getQuestionType(){
  203.         return $this->question->type;
  204.     }
  205. }