<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Validator\CompanyInformationNotBlank;
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\CompanyInformationPageRepository")
* @CompanyInformationNotBlank()
*/
#[ApiResource(
attributes: ['security' => "is_granted('ROLE_USER')"],
normalizationContext: ['groups' => ['companyInformation:read']],
denormalizationContext: ['groups' => ['companyInformation:write']],
collectionOperations: [
'get',
'post' => [
'security' => "is_granted('ROLE_ADMIN')",
],
],
itemOperations: [
'get',
'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
],
)]
class CompanyInformationPage
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var Company
*
* @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="informationPages")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
* @Groups({"companyInformation:read", "companyInformation:write"})
*/
private $company;
/**
* @var CompanyFormPage
*
* @ORM\ManyToOne(targetEntity="App\Entity\CompanyFormPage")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
* @Groups({"companyInformation:read", "companyInformation:write"})
*/
private $formPage;
/**
* @var ArrayCollection|CompanyInformationGroup[]
*
* @ORM\OneToMany(targetEntity="App\Entity\CompanyInformationGroup", mappedBy="informationPage", cascade={"persist"})
* @Assert\Valid()
* @Groups({"companyInformation:read", "companyInformation:write"})
*/
private $informationGroups;
/**
* CompanyInformationPage constructor.
*/
public function __construct()
{
$this->informationGroups = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getFormPage(): ?CompanyFormPage
{
return $this->formPage;
}
public function setFormPage(?CompanyFormPage $formPage): self
{
$this->formPage = $formPage;
return $this;
}
/**
* @return Collection|CompanyInformationGroup[]
*/
public function getInformationGroups(): Collection
{
return $this->informationGroups;
}
public function addInformationGroup(CompanyInformationGroup $informationGroup): self
{
if (!$this->informationGroups->contains($informationGroup)) {
$this->informationGroups[] = $informationGroup;
$informationGroup->setInformationPage($this);
}
return $this;
}
public function removeInformationGroup(CompanyInformationGroup $informationGroup): self
{
if ($this->informationGroups->contains($informationGroup)) {
$this->informationGroups->removeElement($informationGroup);
// set the owning side to null (unless already changed)
if ($informationGroup->getInformationPage() === $this) {
$informationGroup->setInformationPage(null);
}
}
return $this;
}
public function orderInformationGroups(): void
{
$informationGroups = $this->informationGroups->toArray();
usort($informationGroups, function (CompanyInformationGroup $a, CompanyInformationGroup $b) {
return $a->getFormGroup()->getPosition() <=> $b->getFormGroup()->getPosition();
});
$this->informationGroups = new ArrayCollection($informationGroups);
}
}