<?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;
/**
* The most generic type of item.
*
* @see https://schema.org/Thing
*
* @author Jordi Fernandes Alves <jfadev@gmail.com>
*/
#[ORM\Entity]
#[ApiResource(
iri: 'Modality',
itemOperations: [
'get' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'modality:item:get',
'enable_max_depth' => true
]
],
'put' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'modality:item:put',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'modality:item:put',
'enable_max_depth' => true
]
],
'delete' => [
// 'security' => "is_granted('ROLE_USER')",
]
],
collectionOperations: [
'get' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => ['modality:collection:get', 'createdAt'],
'enable_max_depth' => true
]
],
'post' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'modality:collection:post',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'modality:collection:post',
'enable_max_depth' => true
]
],
]
)]
#[ApiFilter(SearchFilter::class, properties: [
'name' => 'partial',
'createdAt' => 'start',
])]
#[ApiFilter(OrderFilter::class, properties: [
'name',
'createdAt'
])]
class Modality
{
use TimestampableEntity;
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: 'integer')]
#[Groups([
'import:insurer',
'modality:collection:get',
'modality:item:get',
])]
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',
'modality:collection:get',
'modality:collection:post',
'modality:item:get',
'modality:item:put',
'insurer_modality:item:get',
'demand: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([
'import:insurer',
'modality:collection:post',
'modality:item:get',
'modality:item:put',
])]
private ?string $description = null;
#[ORM\OneToMany(mappedBy: 'modality', targetEntity: Submodality::class, cascade: ["persist", "remove"], orphanRemoval: true)]
#[Groups([
'modality:item:get',
])]
#[MaxDepth(1)]
private $submodalities;
#[ORM\OneToMany(mappedBy: 'modality', targetEntity: InsurerModality::class, cascade: ["remove"], orphanRemoval: true)]
private $insurers;
public function __construct()
{
$this->submodalities = new ArrayCollection();
$this->insurers = 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 addSubmodality(Submodality $submodality): void
{
$this->submodalities[] = $submodality;
}
public function removeSubmodality(Submodality $submodality): void
{
$this->submodalities->removeElement($submodality);
}
public function getSubmodalities(): ?Collection
{
return $this->submodalities;
}
/**
* @return Collection|InsurerModality[]
*/
public function getInsurers(): Collection
{
return $this->insurers;
}
public function addInsurer(InsurerModality $insurer): self
{
if (!$this->insurers->contains($insurer)) {
$this->insurers[] = $insurer;
$insurer->setModality($this);
}
return $this;
}
public function removeInsurer(InsurerModality $insurer): self
{
if ($this->insurers->removeElement($insurer)) {
// set the owning side to null (unless already changed)
if ($insurer->getModality() === $this) {
$insurer->setModality(null);
}
}
return $this;
}
}