src/Entity/CompanyInformationGroup.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Serializer\Annotation\Groups;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. /**
  10.  * @ORM\Entity(repositoryClass="App\Repository\CompanyInformationGroupRepository")
  11.  */
  12. #[ApiResource(
  13.     attributes: ['security' => "is_granted('ROLE_USER')"],
  14.     normalizationContext: ['groups' => ['companyInformationGroup:read']],
  15.     denormalizationContext: ['groups' => ['companyInformationGroup:write']],
  16.     collectionOperations: [
  17.         'get',
  18.         'post' => [
  19.             'security' => "is_granted('ROLE_ADMIN')",
  20.         ],
  21.     ],
  22.     itemOperations: [
  23.         'get',
  24.         'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
  25.     ],
  26. )]
  27. class CompanyInformationGroup
  28. {
  29.     /**
  30.      * @ORM\Id()
  31.      * @ORM\GeneratedValue()
  32.      * @ORM\Column(type="integer")
  33.      */
  34.     private $id;
  35.     /**
  36.      * @var CompanyInformationPage
  37.      *
  38.      * @ORM\ManyToOne(targetEntity="App\Entity\CompanyInformationPage", inversedBy="informationGroups")
  39.      * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
  40.      * @Groups({"companyInformationGroup:read", "companyInformationGroup:write"})
  41.      */
  42.     private $informationPage;
  43.     /**
  44.      * @var CompanyFormGroup
  45.      *
  46.      * @ORM\ManyToOne(targetEntity="App\Entity\CompanyFormGroup")
  47.      * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
  48.      * @Groups({"companyInformationGroup:read", "companyInformationGroup:write"})
  49.      */
  50.     private $formGroup;
  51.     /**
  52.      * @var ArrayCollection|CompanyInformationQuestion[]
  53.      *
  54.      * @ORM\OneToMany(targetEntity="App\Entity\CompanyInformationQuestion", mappedBy="informationGroup", cascade={"persist"})
  55.      * @Assert\Valid()
  56.      * @Groups({"companyInformationGroup:read", "companyInformationGroup:write"})
  57.      */
  58.     private $informationQuestions;
  59.     /**
  60.      * CompanyInformationGroup constructor.
  61.      */
  62.     public function __construct()
  63.     {
  64.         $this->informationQuestions = new ArrayCollection();
  65.     }
  66.     public function getId(): ?int
  67.     {
  68.         return $this->id;
  69.     }
  70.     public function getFormGroup(): ?CompanyFormGroup
  71.     {
  72.         return $this->formGroup;
  73.     }
  74.     public function setFormGroup(?CompanyFormGroup $formGroup): self
  75.     {
  76.         $this->formGroup $formGroup;
  77.         return $this;
  78.     }
  79.     /**
  80.      * @return Collection|CompanyInformationQuestion[]
  81.      */
  82.     public function getInformationQuestions(): Collection
  83.     {
  84.         return $this->informationQuestions;
  85.     }
  86.     public function addInformationQuestion(CompanyInformationQuestion $informationQuestion): self
  87.     {
  88.         if (!$this->informationQuestions->contains($informationQuestion)) {
  89.             $this->informationQuestions[] = $informationQuestion;
  90.             $informationQuestion->setInformationGroup($this);
  91.         }
  92.         return $this;
  93.     }
  94.     public function removeInformationQuestion(CompanyInformationQuestion $informationQuestion): self
  95.     {
  96.         if ($this->informationQuestions->contains($informationQuestion)) {
  97.             $this->informationQuestions->removeElement($informationQuestion);
  98.             // set the owning side to null (unless already changed)
  99.             if ($informationQuestion->getInformationGroup() === $this) {
  100.                 $informationQuestion->setInformationGroup(null);
  101.             }
  102.         }
  103.         return $this;
  104.     }
  105.     public function orderInformationQuestion()
  106.     {
  107.         $informationQuestions $this->informationQuestions->toArray();
  108.         usort($informationQuestions, function (CompanyInformationQuestion $aCompanyInformationQuestion $b) {
  109.             return $a->getFormQuestion()->getPosition() <=> $b->getFormQuestion()->getPosition();
  110.         });
  111.         $this->informationQuestions = new ArrayCollection($informationQuestions);
  112.     }
  113.     public function getInformationPage(): ?CompanyInformationPage
  114.     {
  115.         return $this->informationPage;
  116.     }
  117.     public function setInformationPage(?CompanyInformationPage $informationPage): self
  118.     {
  119.         $this->informationPage $informationPage;
  120.         return $this;
  121.     }
  122.     public function isAnswered(): bool
  123.     {
  124.         foreach ($this->informationQuestions as $informationQuestion) {
  125.             /** @var CompanyInformationQuestion $informationQuestion */
  126.             if (true === $informationQuestion->isAnswered()) {
  127.                 return true;
  128.             }
  129.         }
  130.         return false;
  131.     }
  132. }