<?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: 'Emission',
itemOperations: [
'get' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'emission:item:get',
'enable_max_depth' => true
]
],
'put' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'emission:item:put',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'emission:item:put',
'enable_max_depth' => true
]
],
'delete' => [
// 'security' => "is_granted('ROLE_USER')",
]
],
collectionOperations: [
'get' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => ['emission:collection:get', 'createdAt'],
'enable_max_depth' => true
]
],
'post' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'emission:collection:post',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'emission:collection:post',
'enable_max_depth' => true
]
],
]
)]
#[ApiFilter(SearchFilter::class, properties: [
'name' => 'partial',
'url' => 'partial',
'createdAt' => 'start',
])]
#[ApiFilter(OrderFilter::class, properties: [
'name',
'url',
'createdAt'
])]
class Emission
{
use TimestampableEntity;
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: 'integer')]
#[Groups([
'emission:collection:get',
'emission:item:get',
'insured:collection:post',
'insured: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([
'emission:collection:get',
'emission:collection:post',
'emission:item:get',
'emission:item:put',
'insured_emission:collection:get',
'insured_emission:item:get',
'insured_emission:item:put',
'insured:item:get',
])]
private ?string $name = null;
/**
* URL of the emission.
*
* @see https://schema.org/url
*/
#[ORM\Column(type: 'string')]
#[ApiProperty(iri: 'https://schema.org/url')]
#[Assert\Url]
#[Groups([
'emission:collection:get',
'emission:collection:post',
'emission:item:get',
'emission:item:put',
])]
private ?string $url = null;
#[ORM\OneToMany(mappedBy: 'emission', targetEntity: InsuredEmission::class, cascade: ["persist", "remove"], orphanRemoval: true)]
#[Groups([
'emission:collection:get',
'emission:collection:post',
'emission:item:get',
'emission:item:put',
])]
#[MaxDepth(1)]
private $insureds;
public function __construct()
{
$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 setUrl(?string $url): void
{
$this->url = $url;
}
public function getUrl(): ?string
{
return $this->url;
}
/**
* @return Collection|InsuredEmission[]
*/
public function getInsureds(): Collection
{
return $this->insureds;
}
public function addInsured(InsuredEmission $insured): self
{
if (!$this->insureds->contains($insured)) {
$this->insureds[] = $insured;
$insured->setEmission($this);
}
return $this;
}
public function removeInsured(InsuredEmission $insured): self
{
if ($this->insureds->removeElement($insured)) {
// set the owning side to null (unless already changed)
if ($insured->getEmission() === $this) {
$insured->setEmission(null);
}
}
return $this;
}
}