<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use App\Trait\TimestampableEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\MaxDepth;
#[ORM\Entity]
#[ApiResource(
iri: 'Submodality',
itemOperations: [
'get' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'submodality:item:get',
'enable_max_depth' => true
]
],
'put' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'submodality:item:put',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'submodality:item:put',
'enable_max_depth' => true
]
],
'delete' => [
// 'security' => "is_granted('ROLE_USER')",
]
],
collectionOperations: [
'get' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => ['submodality:collection:get', 'createdAt'],
'enable_max_depth' => true
]
],
'post' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'submodality:collection:post',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'submodality:collection:post',
'enable_max_depth' => true
]
],
]
)]
#[ApiFilter(SearchFilter::class, properties: [
'modality' => 'exact',
'name' => 'partial',
'createdAt' => 'start',
])]
#[ApiFilter(OrderFilter::class, properties: [
'name',
'createdAt'
])]
class Submodality
{
use TimestampableEntity;
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: 'integer')]
#[Groups([
'submodality:collection:get',
'submodality:item:get',
'demand:item:put',
])]
private ?int $id = null;
/**
* The name of the item.
*
* @see https://schema.org/name
*/
#[ORM\Column(type: 'string')]
#[ApiProperty(iri: 'https://schema.org/name')]
#[Assert\Type('string')]
#[Groups([
'import:insurer',
'submodality:collection:get',
'submodality:collection:post',
'submodality:item:get',
'submodality:item:put',
'demand:item:get',
'modality:item:get',
'demand:collection:get',
])]
private ?string $name = null;
/**
* A description of the item.
*
* @see https://schema.org/description
*/
#[ORM\Column(type: 'text', nullable: true)]
#[ApiProperty(iri: 'https://schema.org/description')]
#[Assert\Type('string')]
#[Groups([
'submodality:collection:post',
'submodality:item:get',
'submodality:item:put',
])]
private ?string $description = null;
#[ORM\ManyToOne(inversedBy: "submodalities", targetEntity: Modality::class)]
#[ORM\JoinColumn(nullable: false)]
#[Groups([
'submodality:collection:post',
'modality:collection:post'
])]
#[MaxDepth(1)]
private ?Modality $modality = null;
#[ORM\OneToMany(targetEntity: InsurerSubmodality::class, mappedBy: 'submodality', cascade: ["remove"], orphanRemoval: true)]
private $insurers;
#[ORM\OneToMany(targetEntity: InsuredInsurer::class, mappedBy: 'submodality', cascade: ["remove"], orphanRemoval: true)]
private $insureds;
#[ApiProperty()]
#[Groups([
'modality:collection:get',
])]
private $modalityName;
public function __construct()
{
$this->insurers = new ArrayCollection();
$this->insureds = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function setName(?string $name): void
{
$this->name = $name;
}
public function getName(): ?string
{
return $this->name;
}
public function setDescription(?string $description): void
{
$this->description = $description;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setModality(?Modality $modality): void
{
$this->modality = $modality;
}
public function getModality(): ?Modality
{
return $this->modality;
}
/**
* @return Collection|Insurer[]
*/
public function getInsurers(): Collection
{
return $this->insurers;
}
public function addInsurer(InsurerSubmodality $insurer): self
{
if (!$this->insurers->contains($insurer)) {
$this->insurers[] = $insurer;
$insurer->setSubmodality($this);
}
return $this;
}
public function removeInsurer(InsurerSubmodality $insurer): self
{
if ($this->insurers->removeElement($insurer)) {
// set the owning side to null (unless already changed)
if ($insurer->getSubmodality() === $this) {
$insurer->setSubmodality(null);
}
}
return $this;
}
/**
* @return Collection|InsuredInsurer[]
*/
public function getInsureds(): Collection
{
return $this->insureds;
}
public function getModalityName(): ?string
{
return $this->getModality()->getName();
}
public function addInsured(InsuredInsurer $insured): self
{
if (!$this->insureds->contains($insured)) {
$this->insureds[] = $insured;
$insured->setSubmodality($this);
}
return $this;
}
public function removeInsured(InsuredInsurer $insured): self
{
if ($this->insureds->removeElement($insured)) {
// set the owning side to null (unless already changed)
if ($insured->getSubmodality() === $this) {
$insured->setSubmodality(null);
}
}
return $this;
}
}