<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Enum\SurveyFormType;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Greg0ire\Enum\Bridge\Symfony\Validator\Constraint\Enum as EnumAssert;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\SurveyFormRepository")
* @ORM\HasLifecycleCallbacks()
*/
#[ApiResource(
attributes: ['security' => "is_granted('ROLE_USER')"],
normalizationContext: ['groups' => ['surveyForm:read']],
denormalizationContext: ['groups' => ['surveyForm:write']],
collectionOperations: [
'get',
'post' => ['security' => "is_granted('ROLE_ADMIN')"],
],
itemOperations: [
'get',
'delete',
'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
],
)]
#[ApiFilter(PropertyFilter::class)]
#[ApiFilter(SearchFilter::class, properties: ['id', 'title' => 'partial'])]
#[ApiFilter(OrderFilter::class, properties: ['id', 'title', 'slug', 'createdAt'], arguments: ['orderParameterName' => 'order'])]
class SurveyForm
{
use TimestampableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var string
*
* @ORM\Column(type="string", length=128)
* @Assert\Length(max="128")
* @Assert\NotBlank()
* @Groups({"surveyForm:read","surveyForm:write"})
*/
#[ApiFilter(SearchFilter::class, strategy: 'ipartial')]
public $title;
/**
* @var string
*
* @ORM\Column(type="string", length=128, unique=true)
* @Gedmo\Slug(fields={"title"})
* @Groups({"surveyForm:read", "surveyForm:write"})
*/
public $slug;
/**
* @var Company
*
* @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="surveyForms")
* @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
* @Groups({"surveyForm:read", "surveyForm:write"})
*/
public $company;
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="App\Entity\SurveyFormStep", mappedBy="form", cascade={"persist"})
* @Groups({"surveyForm:read", "surveyForm:write"})
*/
public $formSteps;
/**
* @var string
*
* @ORM\Column(type="string", length=64)
* @Assert\Length(max="64")
* @Assert\NotBlank()
* @EnumAssert(class="App\Enum\SurveyFormType")
* @Groups({"surveyForm:read", "surveyForm:write"})
*/
public $type;
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="App\Entity\Race")
* @Groups({"surveyForm:read", "surveyForm:write"})
*/
public $races;
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="App\Entity\Gender")
* @Groups({"surveyForm:read", "surveyForm:write"})
*/
public $genders;
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="App\Entity\Religion")
* @Groups({"surveyForm:read", "surveyForm:write"})
*/
public $religions;
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="App\Entity\SexualOrientation")
* @Groups({"surveyForm:read", "surveyForm:write"})
*/
public $sexualOrientations;
/**
* @ORM\Column(type="boolean")
* @Groups({"surveyForm:read", "surveyForm:write"})
*/
public $isActive;
/**
* @var ArrayCollection|SurveyCompany[]
*
* @ORM\ManyToMany(targetEntity="App\Entity\SurveyCompany", inversedBy="surveyForms", cascade={"persist","remove"}, fetch="EXTRA_LAZY")
* @ORM\JoinTable(name="survey_form_survey_company",
* joinColumns={@ORM\JoinColumn(name="survey_form_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="survey_company_id", referencedColumnName="id")}
* )
* @ORM\OrderBy({"id" = "ASC"})
* @Groups({"surveyForm:read", "surveyForm:write"})
*/
public $surveyCompany;
public function __construct()
{
$this->isActive = true;
$this->formSteps = new ArrayCollection();
$this->races = new ArrayCollection();
$this->genders = new ArrayCollection();
$this->religions = new ArrayCollection();
$this->sexualOrientations = new ArrayCollection();
$this->surveyCompany = new ArrayCollection();
}
public function __toString(): string
{
return $this->getTitle();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
/**
* @return Collection|SurveyFormStep[]
*/
public function getFormSteps(): Collection
{
return $this->formSteps;
}
public function addFormStep(SurveyFormStep $formStep): self
{
if (!$this->formSteps->contains($formStep)) {
$this->formSteps[] = $formStep;
$formStep->setForm($this);
}
return $this;
}
public function removeFormStep(SurveyFormStep $formStep): self
{
if ($this->formSteps->contains($formStep)) {
$this->formSteps->removeElement($formStep);
// set the owning side to null (unless already changed)
if ($formStep->getForm() === $this) {
$formStep->setForm(null);
}
}
return $this;
}
/**
* @return Collection|Race[]
*/
public function getRaces(): Collection
{
return $this->races;
}
public function addRace(Race $race): self
{
if (!$this->races->contains($race)) {
$this->races[] = $race;
}
return $this;
}
public function removeRace(Race $race): self
{
if ($this->races->contains($race)) {
$this->races->removeElement($race);
}
return $this;
}
/**
* @return Collection|Gender[]
*/
public function getGenders(): Collection
{
return $this->genders;
}
public function addGender(Gender $gender): self
{
if (!$this->genders->contains($gender)) {
$this->genders[] = $gender;
}
return $this;
}
public function removeGender(Gender $gender): self
{
if ($this->genders->contains($gender)) {
$this->genders->removeElement($gender);
}
return $this;
}
/**
* @return Collection|Religion[]
*/
public function getReligions(): Collection
{
return $this->religions;
}
public function addReligion(Religion $religion): self
{
if (!$this->religions->contains($religion)) {
$this->religions[] = $religion;
}
return $this;
}
public function removeReligion(Religion $religion): self
{
if ($this->religions->contains($religion)) {
$this->religions->removeElement($religion);
}
return $this;
}
/**
* @return Collection|SexualOrientation[]
*/
public function getSexualOrientations(): Collection
{
return $this->sexualOrientations;
}
public function addSexualOrientation(SexualOrientation $sexualOrientation): self
{
if (!$this->sexualOrientations->contains($sexualOrientation)) {
$this->sexualOrientations[] = $sexualOrientation;
}
return $this;
}
public function removeSexualOrientation(SexualOrientation $sexualOrientation): self
{
if ($this->sexualOrientations->contains($sexualOrientation)) {
$this->sexualOrientations->removeElement($sexualOrientation);
}
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
public function getObjects(): Collection
{
$objects = new ArrayCollection();
if (SurveyFormType::RACES === $this->type) {
$objects = $this->races;
} elseif (SurveyFormType::GENDERS === $this->type) {
$objects = $this->genders;
} elseif (SurveyFormType::RELIGIONS === $this->type) {
$objects = $this->religions;
} elseif (SurveyFormType::SEXUAL_ORIENTATIONS === $this->type) {
$objects = $this->sexualOrientations;
}
return $objects;
}
public function getIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
/**
* @return Collection|SurveyCompany[]
*/
public function getSurveyCompany(): Collection
{
return $this->surveyCompany;
}
public function addSurveyCompany(SurveyCompany $surveyCompany): self
{
if (!$this->surveyCompany->contains($surveyCompany)) {
$this->surveyCompany[] = $surveyCompany;
$surveyCompany->addSurveyForm($this);
}
return $this;
}
public function removeSurveyCompany(SurveyCompany $surveyCompany): self
{
if ($this->surveyCompany->contains($surveyCompany)) {
$this->surveyCompany->removeElement($surveyCompany);
$surveyCompany->removeSurveyForm($this);
}
return $this;
}
/**
* @Groups({"surveyForm:read"})
* Returns createdAt.
*/
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
}