src/Entity/SurveyFormStep.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  7. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use Symfony\Component\Serializer\Annotation\Groups;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. /**
  14.  * @ORM\Entity(repositoryClass="App\Repository\SurveyFormStepRepository")
  15.  * @ORM\HasLifecycleCallbacks()
  16.  */
  17. #[ApiResource(
  18.     attributes: ['security' => "is_granted('ROLE_USER')"],
  19.     normalizationContext: ['groups' => ['surveyFormStep:read']],
  20.     denormalizationContext: ['groups' => ['surveyFormStep:write']],
  21.     collectionOperations: [
  22.         'get',
  23.         'post' => [
  24.             'security' => "is_granted('ROLE_ADMIN')",
  25.         ],
  26.     ],
  27.     itemOperations: [
  28.         'get',
  29.         'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
  30.     ],
  31. )]
  32. #[ApiFilter(SearchFilter::class, properties: ['id''title' => 'partial''description' => 'partial'])]
  33. #[ApiFilter(PropertyFilter::class)]
  34. #[ApiFilter(OrderFilter::class, properties: ['id''title''form.title'], arguments: ['orderParameterName' => 'order'])]
  35. class SurveyFormStep
  36. {
  37.     /**
  38.      * @ORM\Id()
  39.      * @ORM\GeneratedValue()
  40.      * @ORM\Column(type="integer")
  41.      */
  42.     private $id;
  43.     /**
  44.      * @var string
  45.      *
  46.      * @ORM\Column(type="string", length=128)
  47.      * @Assert\NotBlank()
  48.      * @Assert\Length(max="128")
  49.      * @Groups({"surveyFormStep:read", "surveyFormStep:write"})
  50.      */
  51.     private $title;
  52.     /**
  53.      * @var string
  54.      *
  55.      * @ORM\Column(type="text", nullable=true)
  56.      * @Groups({"surveyFormStep:read", "surveyFormStep:write"})
  57.      */
  58.     private $description;
  59.     /**
  60.      * @var int
  61.      *
  62.      * @ORM\Column(type="integer")
  63.      * @Assert\NotBlank()
  64.      * @Assert\Type(type="integer")
  65.      * @Groups({"surveyFormStep:read", "surveyFormStep:write"})
  66.      */
  67.     private $position;
  68.     /**
  69.      * @var SurveyForm
  70.      *
  71.      * @ORM\ManyToOne(targetEntity="App\Entity\SurveyForm", inversedBy="formSteps")
  72.      * @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
  73.      * @Groups({"surveyFormStep:read", "surveyFormStep:write"})
  74.      */
  75.     private $form;
  76.     /**
  77.      * @var ArrayCollection|SurveyFormQuestion[]
  78.      *
  79.      * @ORM\ManyToMany(targetEntity="App\Entity\SurveyFormQuestion", inversedBy="formSteps")
  80.      * @ORM\JoinTable(name="survey_form_step_survey_form_question")
  81.      * @ORM\JoinColumn(name="survey_form_question_id", referencedColumnName="id")
  82.      * @Groups({"surveyFormStep:read", "surveyFormStep:write"})
  83.      */
  84.     public $formQuestions;
  85.     /**
  86.      * @var ArrayCollection|SurveyFormStepSurveyFormQuestion[]
  87.      *
  88.      * @ORM\OneToMany(targetEntity="App\Entity\SurveyFormStepSurveyFormQuestion", mappedBy="surveyFormStep", cascade={"persist"})
  89.      * @Groups({"surveyFormStep:read", "surveyFormStep:write"})
  90.      */
  91.     public iterable $surveyFormQuestions;
  92.     /**
  93.      * @ORM\Column(type="boolean")
  94.      */
  95.     private $isActive;
  96.     public function __construct()
  97.     {
  98.         $this->isActive true;
  99.         $this->formQuestions = new ArrayCollection();
  100.     }
  101.     public function __toString(): string
  102.     {
  103.         return $this->getForm()->getTitle();
  104.     }
  105.     public function getId(): ?int
  106.     {
  107.         return $this->id;
  108.     }
  109.     public function getTitle(): ?string
  110.     {
  111.         return $this->title;
  112.     }
  113.     public function setTitle(string $title): self
  114.     {
  115.         $this->title $title;
  116.         return $this;
  117.     }
  118.     public function getDescription(): ?string
  119.     {
  120.         return $this->description;
  121.     }
  122.     public function setDescription(?string $description): self
  123.     {
  124.         $this->description $description;
  125.         return $this;
  126.     }
  127.     public function getPosition(): ?int
  128.     {
  129.         return $this->position;
  130.     }
  131.     public function setPosition(int $position): self
  132.     {
  133.         $this->position $position;
  134.         return $this;
  135.     }
  136.     public function getForm(): ?SurveyForm
  137.     {
  138.         return $this->form;
  139.     }
  140.     public function setForm(?SurveyForm $form): self
  141.     {
  142.         $this->form $form;
  143.         return $this;
  144.     }
  145.     /**
  146.      * @return ArrayCollection|SurveyFormQuestion[]
  147.      */
  148.     public function getFormQuestions(): Collection
  149.     {
  150.         return $this->formQuestions;
  151.     }
  152.     public function addFormQuestion(SurveyFormQuestion $formQuestion): self
  153.     {
  154.         if (!$this->formQuestions->contains($formQuestion)) {
  155.             $this->formQuestions[] = $formQuestion;
  156.         }
  157.         return $this;
  158.     }
  159.     public function removeFormQuestion(SurveyFormQuestion $formQuestion): self
  160.     {
  161.         if ($this->formQuestions->contains($formQuestion)) {
  162.             $this->formQuestions->removeElement($formQuestion);
  163.         }
  164.         return $this;
  165.     }
  166.     /**
  167.      * @return Collection<int, SurveyFormStepSurveyFormQuestion>
  168.      */
  169.     public function getSurveyFormQuestions(): iterable
  170.     {
  171.         return $this->surveyFormQuestions;
  172.     }
  173.     public function addSurveyFormQuestion(SurveyFormStepSurveyFormQuestion $surveyFormQuestion): self
  174.     {
  175.         if (!$this->surveyFormQuestions->contains($surveyFormQuestion)) {
  176.             $surveyFormQuestion->setSurveyFormStep($this);
  177.             $this->surveyFormQuestions[] = $surveyFormQuestion;
  178.         }
  179.         return $this;
  180.     }
  181.     public function removeSurveyFormQuestion(SurveyFormStepSurveyFormQuestion $surveyFormQuestion): self
  182.     {
  183.         if ($this->surveyFormQuestions->contains($surveyFormQuestion)) {
  184.             $this->surveyFormQuestions->removeElement($surveyFormQuestion);
  185.             // set the owning side to null (unless already changed)
  186.             if ($surveyFormQuestion->getSurveyFormStep() === $this) {
  187.                 $surveyFormQuestion->setSurveyFormStep(null);
  188.             }
  189.         }
  190.         return $this;
  191.     }
  192.     public function getIsActive(): ?bool
  193.     {
  194.         return $this->isActive;
  195.     }
  196.     public function setIsActive(bool $isActive): self
  197.     {
  198.         $this->isActive $isActive;
  199.         return $this;
  200.     }
  201.     #[Groups(['surveyFormStep:read'])]
  202.     public function getTotalQuestions(): int
  203.     {
  204.         return $this->formQuestions->count();
  205.     }
  206. }