<?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 Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\SurveyFormStepRepository")
* @ORM\HasLifecycleCallbacks()
*/
#[ApiResource(
attributes: ['security' => "is_granted('ROLE_USER')"],
normalizationContext: ['groups' => ['surveyFormStep:read']],
denormalizationContext: ['groups' => ['surveyFormStep:write']],
collectionOperations: [
'get',
'post' => [
'security' => "is_granted('ROLE_ADMIN')",
],
],
itemOperations: [
'get',
'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
],
)]
#[ApiFilter(SearchFilter::class, properties: ['id', 'title' => 'partial', 'description' => 'partial'])]
#[ApiFilter(PropertyFilter::class)]
#[ApiFilter(OrderFilter::class, properties: ['id', 'title', 'form.title'], arguments: ['orderParameterName' => 'order'])]
class SurveyFormStep
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var string
*
* @ORM\Column(type="string", length=128)
* @Assert\NotBlank()
* @Assert\Length(max="128")
* @Groups({"surveyFormStep:read", "surveyFormStep:write"})
*/
private $title;
/**
* @var string
*
* @ORM\Column(type="text", nullable=true)
* @Groups({"surveyFormStep:read", "surveyFormStep:write"})
*/
private $description;
/**
* @var int
*
* @ORM\Column(type="integer")
* @Assert\NotBlank()
* @Assert\Type(type="integer")
* @Groups({"surveyFormStep:read", "surveyFormStep:write"})
*/
private $position;
/**
* @var SurveyForm
*
* @ORM\ManyToOne(targetEntity="App\Entity\SurveyForm", inversedBy="formSteps")
* @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
* @Groups({"surveyFormStep:read", "surveyFormStep:write"})
*/
private $form;
/**
* @var ArrayCollection|SurveyFormQuestion[]
*
* @ORM\ManyToMany(targetEntity="App\Entity\SurveyFormQuestion", inversedBy="formSteps")
* @ORM\JoinTable(name="survey_form_step_survey_form_question")
* @ORM\JoinColumn(name="survey_form_question_id", referencedColumnName="id")
* @Groups({"surveyFormStep:read", "surveyFormStep:write"})
*/
public $formQuestions;
/**
* @var ArrayCollection|SurveyFormStepSurveyFormQuestion[]
*
* @ORM\OneToMany(targetEntity="App\Entity\SurveyFormStepSurveyFormQuestion", mappedBy="surveyFormStep", cascade={"persist"})
* @Groups({"surveyFormStep:read", "surveyFormStep:write"})
*/
public iterable $surveyFormQuestions;
/**
* @ORM\Column(type="boolean")
*/
private $isActive;
public function __construct()
{
$this->isActive = true;
$this->formQuestions = new ArrayCollection();
}
public function __toString(): string
{
return $this->getForm()->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 getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getPosition(): ?int
{
return $this->position;
}
public function setPosition(int $position): self
{
$this->position = $position;
return $this;
}
public function getForm(): ?SurveyForm
{
return $this->form;
}
public function setForm(?SurveyForm $form): self
{
$this->form = $form;
return $this;
}
/**
* @return ArrayCollection|SurveyFormQuestion[]
*/
public function getFormQuestions(): Collection
{
return $this->formQuestions;
}
public function addFormQuestion(SurveyFormQuestion $formQuestion): self
{
if (!$this->formQuestions->contains($formQuestion)) {
$this->formQuestions[] = $formQuestion;
}
return $this;
}
public function removeFormQuestion(SurveyFormQuestion $formQuestion): self
{
if ($this->formQuestions->contains($formQuestion)) {
$this->formQuestions->removeElement($formQuestion);
}
return $this;
}
/**
* @return Collection<int, SurveyFormStepSurveyFormQuestion>
*/
public function getSurveyFormQuestions(): iterable
{
return $this->surveyFormQuestions;
}
public function addSurveyFormQuestion(SurveyFormStepSurveyFormQuestion $surveyFormQuestion): self
{
if (!$this->surveyFormQuestions->contains($surveyFormQuestion)) {
$surveyFormQuestion->setSurveyFormStep($this);
$this->surveyFormQuestions[] = $surveyFormQuestion;
}
return $this;
}
public function removeSurveyFormQuestion(SurveyFormStepSurveyFormQuestion $surveyFormQuestion): self
{
if ($this->surveyFormQuestions->contains($surveyFormQuestion)) {
$this->surveyFormQuestions->removeElement($surveyFormQuestion);
// set the owning side to null (unless already changed)
if ($surveyFormQuestion->getSurveyFormStep() === $this) {
$surveyFormQuestion->setSurveyFormStep(null);
}
}
return $this;
}
public function getIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
#[Groups(['surveyFormStep:read'])]
public function getTotalQuestions(): int
{
return $this->formQuestions->count();
}
}