src/Entity/SurveyForm.php line 41

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 App\Enum\SurveyFormType;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Gedmo\Mapping\Annotation as Gedmo;
  13. use Gedmo\Timestampable\Traits\TimestampableEntity;
  14. use Greg0ire\Enum\Bridge\Symfony\Validator\Constraint\Enum as EnumAssert;
  15. use Symfony\Component\Serializer\Annotation\Groups;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17. /**
  18.  * @ORM\Entity(repositoryClass="App\Repository\SurveyFormRepository")
  19.  * @ORM\HasLifecycleCallbacks()
  20.  */
  21. #[ApiResource(
  22.     attributes: ['security' => "is_granted('ROLE_USER')"],
  23.     normalizationContext: ['groups' => ['surveyForm:read']],
  24.     denormalizationContext: ['groups' => ['surveyForm:write']],
  25.     collectionOperations: [
  26.         'get',
  27.         'post' => ['security' => "is_granted('ROLE_ADMIN')"],
  28.     ],
  29.     itemOperations: [
  30.         'get',
  31.         'delete',
  32.         'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
  33.     ],
  34. )]
  35. #[ApiFilter(PropertyFilter::class)]
  36. #[ApiFilter(SearchFilter::class, properties: ['id''title' => 'partial'])]
  37. #[ApiFilter(OrderFilter::class, properties: ['id''title''slug''createdAt'], arguments: ['orderParameterName' => 'order'])]
  38. class SurveyForm
  39. {
  40.     use TimestampableEntity;
  41.     /**
  42.      * @ORM\Id()
  43.      * @ORM\GeneratedValue()
  44.      * @ORM\Column(type="integer")
  45.      */
  46.     private $id;
  47.     /**
  48.      * @var string
  49.      *
  50.      * @ORM\Column(type="string", length=128)
  51.      * @Assert\Length(max="128")
  52.      * @Assert\NotBlank()
  53.      * @Groups({"surveyForm:read","surveyForm:write"})
  54.      */
  55.     #[ApiFilter(SearchFilter::class, strategy'ipartial')]
  56.     public $title;
  57.     /**
  58.      * @var string
  59.      *
  60.      * @ORM\Column(type="string", length=128, unique=true)
  61.      * @Gedmo\Slug(fields={"title"})
  62.      * @Groups({"surveyForm:read", "surveyForm:write"})
  63.      */
  64.     public $slug;
  65.     /**
  66.      * @var Company
  67.      *
  68.      * @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="surveyForms")
  69.      * @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
  70.      * @Groups({"surveyForm:read", "surveyForm:write"})
  71.      */
  72.     public $company;
  73.     /**
  74.      * @var ArrayCollection
  75.      *
  76.      * @ORM\OneToMany(targetEntity="App\Entity\SurveyFormStep", mappedBy="form", cascade={"persist"})
  77.      * @Groups({"surveyForm:read", "surveyForm:write"})
  78.      */
  79.     public $formSteps;
  80.     /**
  81.      * @var string
  82.      *
  83.      * @ORM\Column(type="string", length=64)
  84.      * @Assert\Length(max="64")
  85.      * @Assert\NotBlank()
  86.      * @EnumAssert(class="App\Enum\SurveyFormType")
  87.      * @Groups({"surveyForm:read", "surveyForm:write"})
  88.      */
  89.     public $type;
  90.     /**
  91.      * @var ArrayCollection
  92.      *
  93.      * @ORM\ManyToMany(targetEntity="App\Entity\Race")
  94.      * @Groups({"surveyForm:read", "surveyForm:write"})
  95.      */
  96.     public $races;
  97.     /**
  98.      * @var ArrayCollection
  99.      *
  100.      * @ORM\ManyToMany(targetEntity="App\Entity\Gender")
  101.      * @Groups({"surveyForm:read", "surveyForm:write"})
  102.      */
  103.     public $genders;
  104.     /**
  105.      * @var ArrayCollection
  106.      *
  107.      * @ORM\ManyToMany(targetEntity="App\Entity\Religion")
  108.      * @Groups({"surveyForm:read", "surveyForm:write"})
  109.      */
  110.     public $religions;
  111.     /**
  112.      * @var ArrayCollection
  113.      *
  114.      * @ORM\ManyToMany(targetEntity="App\Entity\SexualOrientation")
  115.      * @Groups({"surveyForm:read", "surveyForm:write"})
  116.      */
  117.     public $sexualOrientations;
  118.     /**
  119.      * @ORM\Column(type="boolean")
  120.      * @Groups({"surveyForm:read", "surveyForm:write"})
  121.      */
  122.     public $isActive;
  123.     /**
  124.      * @var ArrayCollection|SurveyCompany[]
  125.      *
  126.      * @ORM\ManyToMany(targetEntity="App\Entity\SurveyCompany", inversedBy="surveyForms", cascade={"persist","remove"}, fetch="EXTRA_LAZY")
  127.      * @ORM\JoinTable(name="survey_form_survey_company",
  128.      *   joinColumns={@ORM\JoinColumn(name="survey_form_id", referencedColumnName="id")},
  129.      *   inverseJoinColumns={@ORM\JoinColumn(name="survey_company_id", referencedColumnName="id")}
  130.      * )
  131.      * @ORM\OrderBy({"id" = "ASC"})
  132.      * @Groups({"surveyForm:read", "surveyForm:write"})
  133.      */
  134.     public $surveyCompany;
  135.     public function __construct()
  136.     {
  137.         $this->isActive true;
  138.         $this->formSteps = new ArrayCollection();
  139.         $this->races = new ArrayCollection();
  140.         $this->genders = new ArrayCollection();
  141.         $this->religions = new ArrayCollection();
  142.         $this->sexualOrientations = new ArrayCollection();
  143.         $this->surveyCompany = new ArrayCollection();
  144.     }
  145.     public function __toString(): string
  146.     {
  147.         return $this->getTitle();
  148.     }
  149.     public function getId(): ?int
  150.     {
  151.         return $this->id;
  152.     }
  153.     public function getTitle(): ?string
  154.     {
  155.         return $this->title;
  156.     }
  157.     public function setTitle(string $title): self
  158.     {
  159.         $this->title $title;
  160.         return $this;
  161.     }
  162.     public function getSlug(): ?string
  163.     {
  164.         return $this->slug;
  165.     }
  166.     public function setSlug(string $slug): self
  167.     {
  168.         $this->slug $slug;
  169.         return $this;
  170.     }
  171.     public function getCompany(): ?Company
  172.     {
  173.         return $this->company;
  174.     }
  175.     public function setCompany(?Company $company): self
  176.     {
  177.         $this->company $company;
  178.         return $this;
  179.     }
  180.     /**
  181.      * @return Collection|SurveyFormStep[]
  182.      */
  183.     public function getFormSteps(): Collection
  184.     {
  185.         return $this->formSteps;
  186.     }
  187.     public function addFormStep(SurveyFormStep $formStep): self
  188.     {
  189.         if (!$this->formSteps->contains($formStep)) {
  190.             $this->formSteps[] = $formStep;
  191.             $formStep->setForm($this);
  192.         }
  193.         return $this;
  194.     }
  195.     public function removeFormStep(SurveyFormStep $formStep): self
  196.     {
  197.         if ($this->formSteps->contains($formStep)) {
  198.             $this->formSteps->removeElement($formStep);
  199.             // set the owning side to null (unless already changed)
  200.             if ($formStep->getForm() === $this) {
  201.                 $formStep->setForm(null);
  202.             }
  203.         }
  204.         return $this;
  205.     }
  206.     /**
  207.      * @return Collection|Race[]
  208.      */
  209.     public function getRaces(): Collection
  210.     {
  211.         return $this->races;
  212.     }
  213.     public function addRace(Race $race): self
  214.     {
  215.         if (!$this->races->contains($race)) {
  216.             $this->races[] = $race;
  217.         }
  218.         return $this;
  219.     }
  220.     public function removeRace(Race $race): self
  221.     {
  222.         if ($this->races->contains($race)) {
  223.             $this->races->removeElement($race);
  224.         }
  225.         return $this;
  226.     }
  227.     /**
  228.      * @return Collection|Gender[]
  229.      */
  230.     public function getGenders(): Collection
  231.     {
  232.         return $this->genders;
  233.     }
  234.     public function addGender(Gender $gender): self
  235.     {
  236.         if (!$this->genders->contains($gender)) {
  237.             $this->genders[] = $gender;
  238.         }
  239.         return $this;
  240.     }
  241.     public function removeGender(Gender $gender): self
  242.     {
  243.         if ($this->genders->contains($gender)) {
  244.             $this->genders->removeElement($gender);
  245.         }
  246.         return $this;
  247.     }
  248.     /**
  249.      * @return Collection|Religion[]
  250.      */
  251.     public function getReligions(): Collection
  252.     {
  253.         return $this->religions;
  254.     }
  255.     public function addReligion(Religion $religion): self
  256.     {
  257.         if (!$this->religions->contains($religion)) {
  258.             $this->religions[] = $religion;
  259.         }
  260.         return $this;
  261.     }
  262.     public function removeReligion(Religion $religion): self
  263.     {
  264.         if ($this->religions->contains($religion)) {
  265.             $this->religions->removeElement($religion);
  266.         }
  267.         return $this;
  268.     }
  269.     /**
  270.      * @return Collection|SexualOrientation[]
  271.      */
  272.     public function getSexualOrientations(): Collection
  273.     {
  274.         return $this->sexualOrientations;
  275.     }
  276.     public function addSexualOrientation(SexualOrientation $sexualOrientation): self
  277.     {
  278.         if (!$this->sexualOrientations->contains($sexualOrientation)) {
  279.             $this->sexualOrientations[] = $sexualOrientation;
  280.         }
  281.         return $this;
  282.     }
  283.     public function removeSexualOrientation(SexualOrientation $sexualOrientation): self
  284.     {
  285.         if ($this->sexualOrientations->contains($sexualOrientation)) {
  286.             $this->sexualOrientations->removeElement($sexualOrientation);
  287.         }
  288.         return $this;
  289.     }
  290.     public function getType(): ?string
  291.     {
  292.         return $this->type;
  293.     }
  294.     public function setType(string $type): self
  295.     {
  296.         $this->type $type;
  297.         return $this;
  298.     }
  299.     public function getObjects(): Collection
  300.     {
  301.         $objects = new ArrayCollection();
  302.         if (SurveyFormType::RACES === $this->type) {
  303.             $objects $this->races;
  304.         } elseif (SurveyFormType::GENDERS === $this->type) {
  305.             $objects $this->genders;
  306.         } elseif (SurveyFormType::RELIGIONS === $this->type) {
  307.             $objects $this->religions;
  308.         } elseif (SurveyFormType::SEXUAL_ORIENTATIONS === $this->type) {
  309.             $objects $this->sexualOrientations;
  310.         }
  311.         return $objects;
  312.     }
  313.     public function getIsActive(): ?bool
  314.     {
  315.         return $this->isActive;
  316.     }
  317.     public function setIsActive(bool $isActive): self
  318.     {
  319.         $this->isActive $isActive;
  320.         return $this;
  321.     }
  322.     /**
  323.      * @return Collection|SurveyCompany[]
  324.      */
  325.     public function getSurveyCompany(): Collection
  326.     {
  327.         return $this->surveyCompany;
  328.     }
  329.     public function addSurveyCompany(SurveyCompany $surveyCompany): self
  330.     {
  331.         if (!$this->surveyCompany->contains($surveyCompany)) {
  332.             $this->surveyCompany[] = $surveyCompany;
  333.             $surveyCompany->addSurveyForm($this);
  334.         }
  335.         return $this;
  336.     }
  337.     public function removeSurveyCompany(SurveyCompany $surveyCompany): self
  338.     {
  339.         if ($this->surveyCompany->contains($surveyCompany)) {
  340.             $this->surveyCompany->removeElement($surveyCompany);
  341.             $surveyCompany->removeSurveyForm($this);
  342.         }
  343.         return $this;
  344.     }
  345.     /**
  346.      * @Groups({"surveyForm:read"})
  347.      * Returns createdAt.
  348.      */
  349.     public function getCreatedAt(): ?\DateTimeInterface
  350.     {
  351.         return $this->createdAt;
  352.     }
  353. }