src/Entity/Emission.php line 77

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use ApiPlatform\Core\Annotation\ApiProperty;
  5. use ApiPlatform\Core\Annotation\ApiResource;
  6. use ApiPlatform\Core\Annotation\ApiFilter;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  8. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  9. use App\Trait\TimestampableEntity;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. use Symfony\Component\Serializer\Annotation\Groups;
  15. use Symfony\Component\Serializer\Annotation\MaxDepth;
  16. #[ORM\Entity]
  17. #[ApiResource(
  18.     iri'Emission',
  19.     itemOperations: [
  20.         'get' => [
  21.             // 'security' => "is_granted('ROLE_USER')",
  22.             'normalization_context' => [
  23.                 'groups' => 'emission:item:get',
  24.                 'enable_max_depth' => true
  25.             ]
  26.         ],
  27.         'put' => [
  28.             // 'security' => "is_granted('ROLE_USER')",
  29.             'normalization_context' => [
  30.                 'groups' => 'emission:item:put',
  31.                 'enable_max_depth' => true
  32.             ],
  33.             'denormalization_context' => [
  34.                 'groups' => 'emission:item:put',
  35.                 'enable_max_depth' => true
  36.             ]
  37.         ],
  38.         'delete' => [
  39.             // 'security' => "is_granted('ROLE_USER')",
  40.         ]
  41.     ],
  42.     collectionOperations: [
  43.         'get' => [
  44.             // 'security' => "is_granted('ROLE_USER')",
  45.             'normalization_context' => [
  46.                 'groups' => ['emission:collection:get''createdAt'],
  47.                 'enable_max_depth' => true
  48.             ]
  49.         ],
  50.         'post' => [
  51.             // 'security' => "is_granted('ROLE_USER')",
  52.             'normalization_context' => [
  53.                 'groups' => 'emission:collection:post',
  54.                 'enable_max_depth' => true
  55.             ],
  56.             'denormalization_context' => [
  57.                 'groups' => 'emission:collection:post',
  58.                 'enable_max_depth' => true
  59.             ]
  60.         ],
  61.     ]
  62. )]
  63. #[ApiFilter(SearchFilter::class, properties: [
  64.     'name' => 'partial',
  65.     'url' => 'partial',
  66.     'createdAt' => 'start',
  67. ])]
  68. #[ApiFilter(OrderFilter::class, properties: [
  69.     'name',
  70.     'url',
  71.     'createdAt'
  72. ])]
  73. class Emission
  74. {
  75.     use TimestampableEntity;
  76.     
  77.     #[ORM\Id]
  78.     #[ORM\GeneratedValue(strategy'AUTO')]
  79.     #[ORM\Column(type'integer')]
  80.     #[Groups([
  81.         'emission:collection:get',
  82.         'emission:item:get',
  83.         'insured:collection:post',
  84.         'insured:item:put',
  85.     ])]
  86.     private ?int $id null;
  87.     /**
  88.      * The name of the item.
  89.      *
  90.      * @see https://schema.org/name
  91.      */
  92.     #[ORM\Column(type'string')]
  93.     #[ApiProperty(iri'https://schema.org/name')]
  94.     #[Assert\Type('string')]
  95.     #[Groups([
  96.         'emission:collection:get',
  97.         'emission:collection:post',
  98.         'emission:item:get',
  99.         'emission:item:put',
  100.         'insured_emission:collection:get',
  101.         'insured_emission:item:get',
  102.         'insured_emission:item:put',
  103.         'insured:item:get',
  104.     ])]
  105.     private ?string $name null;
  106.     /**
  107.      * URL of the emission.
  108.      *
  109.      * @see https://schema.org/url
  110.      */
  111.     #[ORM\Column(type'string')]
  112.     #[ApiProperty(iri'https://schema.org/url')]
  113.     #[Assert\Url]
  114.     #[Groups([
  115.         'emission:collection:get',
  116.         'emission:collection:post',
  117.         'emission:item:get',
  118.         'emission:item:put',
  119.     ])]
  120.     private ?string $url null;
  121.     #[ORM\OneToMany(mappedBy'emission'targetEntityInsuredEmission::class, cascade: ["persist""remove"], orphanRemovaltrue)]
  122.     #[Groups([
  123.         'emission:collection:get',
  124.         'emission:collection:post',
  125.         'emission:item:get',
  126.         'emission:item:put',
  127.     ])]
  128.     #[MaxDepth(1)]
  129.     private $insureds;
  130.     public function __construct()
  131.     {
  132.         $this->insureds = new ArrayCollection();
  133.     }
  134.     public function getId(): ?int
  135.     {
  136.         return $this->id;
  137.     }
  138.     public function setName(?string $name): void
  139.     {
  140.         $this->name $name;
  141.     }
  142.     public function getName(): ?string
  143.     {
  144.         return $this->name;
  145.     }
  146.     public function setUrl(?string $url): void
  147.     {
  148.         $this->url $url;
  149.     }
  150.     public function getUrl(): ?string
  151.     {
  152.         return $this->url;
  153.     }
  154.     /**
  155.      * @return Collection|InsuredEmission[]
  156.      */
  157.     public function getInsureds(): Collection
  158.     {
  159.         return $this->insureds;
  160.     }
  161.     public function addInsured(InsuredEmission $insured): self
  162.     {
  163.         if (!$this->insureds->contains($insured)) {
  164.             $this->insureds[] = $insured;
  165.             $insured->setEmission($this);
  166.         }
  167.         return $this;
  168.     }
  169.     public function removeInsured(InsuredEmission $insured): self
  170.     {
  171.         if ($this->insureds->removeElement($insured)) {
  172.             // set the owning side to null (unless already changed)
  173.             if ($insured->getEmission() === $this) {
  174.                 $insured->setEmission(null);
  175.             }
  176.         }
  177.         return $this;
  178.     }
  179. }