src/Entity/Incident.php line 50

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 App\Trait\TimestampableEntity;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Symfony\Component\Serializer\Annotation\Groups;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. use Ramsey\Uuid\Uuid;
  13. use Ramsey\Uuid\UuidInterface;
  14. /**
  15.  * Represents an incident report.
  16.  *
  17.  * @see https://schema.org/Thing
  18.  */
  19. #[ORM\Entity]
  20. #[ApiResource(
  21.     iri'incident',
  22.     itemOperations: [
  23.         'get' => ['normalization_context' => ['groups' => 'incident:item:get''enable_max_depth' => true]],
  24.         'put' => [
  25.             'normalization_context' => ['groups' => 'incident:item:put''enable_max_depth' => true],
  26.             'denormalization_context' => ['groups' => 'incident:item:put''enable_max_depth' => true],
  27.         ],
  28.         'delete' => [],
  29.     ],
  30.     collectionOperations: [
  31.         'get' => [
  32.             'normalization_context' => [
  33.                 'groups' => ['incident:collection:get''createdAt'],
  34.                 'enable_max_depth' => true,
  35.             ],
  36.         ],
  37.         'post' => [
  38.             'normalization_context' => ['groups' => 'incident:collection:post''enable_max_depth' => true],
  39.             'denormalization_context' => [
  40.                 'groups' => 'incident:collection:post',
  41.                 'enable_max_depth' => true,
  42.             ],
  43.         ],
  44.     ],
  45. )]
  46. class Incident
  47. {
  48.     use TimestampableEntity;
  49.     
  50.     #[ORM\Id]
  51.     #[ORM\GeneratedValue(strategy'NONE')]
  52.     #[ORM\Column(type'uuid'uniquetrue)]
  53.     #[ApiProperty(iri'https://schema.org/identifier')]
  54.     #[Groups(['incident:item:get''incident:collection:get'])]
  55.     private ?UuidInterface $id null;
  56.     /** @see _:category */
  57.     #[ORM\OneToOne(targetEntity'App\Entity\IncidentCategory')]
  58.     #[ORM\JoinColumn(nullablefalse)]
  59.     #[Groups(['incident:item:get''incident:collection:get''incident:item:put''incident:collection:post'])]
  60.     private ?IncidentCategory $category null;
  61.     /** @see _:subcategory */
  62.     #[ORM\OneToOne(targetEntity'App\Entity\IncidentSubcategory')]
  63.     #[ORM\JoinColumn(nullablefalse)]
  64.     #[Groups(['incident:item:get''incident:collection:get''incident:item:put''incident:collection:post'])]
  65.     private ?IncidentSubcategory $subcategory null;
  66.     /**
  67.      * A description of the incident.
  68.      *
  69.      * @see https://schema.org/description
  70.      */
  71.     #[ORM\Column(type'text'nullablefalse)]
  72.     #[ApiProperty(iri'https://schema.org/description')]
  73.     #[Assert\NotBlank]
  74.     #[Groups(['incident:item:get''incident:collection:get''incident:item:put''incident:collection:post'])]
  75.     private ?string $description null;
  76.     /**
  77.      * The date and time when the incident occurred.
  78.      *
  79.      * @see _:occurredAt
  80.      */
  81.     #[ORM\Column(type'datetime'nullablefalse)]
  82.     #[ApiProperty(iri'https://schema.org/DateTime')]
  83.     #[Assert\NotNull]
  84.     #[Groups(['incident:item:get''incident:collection:get''incident:item:put''incident:collection:post'])]
  85.     private ?\DateTimeInterface $occurredAt null;
  86.     /** @see _:relatedEntities */
  87.     #[ORM\ManyToMany(targetEntity'App\Entity\RelatedEntity')]
  88.     #[ORM\InverseJoinColumn(uniquetrue)]
  89.     #[Groups(['incident:item:get''incident:collection:get''incident:item:put''incident:collection:post'])]
  90.     private Collection $relatedEntities;
  91.     /** @see _:documents */
  92.     #[ORM\ManyToMany(targetEntity'App\Entity\Document')]
  93.     #[ORM\InverseJoinColumn(uniquetrue)]
  94.     #[Groups(['incident:item:get''incident:collection:get''incident:item:put''incident:collection:post'])]
  95.     private Collection $documents;
  96.     public function __construct()
  97.     {
  98.         $this->id Uuid::uuid4();
  99.         $this->relatedEntities = new ArrayCollection();
  100.         $this->documents = new ArrayCollection();
  101.     }
  102.     public function getId(): ?UuidInterface
  103.     {
  104.         return $this->id;
  105.     }
  106.     public function setCategory(?IncidentCategory $category): void
  107.     {
  108.         $this->category $category;
  109.     }
  110.     public function getCategory(): ?IncidentCategory
  111.     {
  112.         return $this->category;
  113.     }
  114.     public function setSubcategory(?IncidentSubcategory $subcategory): void
  115.     {
  116.         $this->subcategory $subcategory;
  117.     }
  118.     public function getSubcategory(): ?IncidentSubcategory
  119.     {
  120.         return $this->subcategory;
  121.     }
  122.     public function setDescription(?string $description): void
  123.     {
  124.         $this->description $description;
  125.     }
  126.     public function getDescription(): ?string
  127.     {
  128.         return $this->description;
  129.     }
  130.     public function setOccurredAt(?\DateTimeInterface $occurredAt): void
  131.     {
  132.         $this->occurredAt $occurredAt;
  133.     }
  134.     public function getOccurredAt(): ?\DateTimeInterface
  135.     {
  136.         return $this->occurredAt;
  137.     }
  138.     public function addRelatedEntity(RelatedEntity $relatedEntity): void
  139.     {
  140.         if (!$this->relatedEntities->contains($relatedEntity)) {
  141.             $this->relatedEntities[] = $relatedEntity;
  142.         }
  143.     }
  144.     public function removeRelatedEntity(RelatedEntity $relatedEntity): void
  145.     {
  146.         $this->relatedEntities->removeElement($relatedEntity);
  147.     }
  148.     public function getRelatedEntities(): Collection
  149.     {
  150.         return $this->relatedEntities;
  151.     }
  152.     public function addDocument(Document $document): void
  153.     {
  154.         if (!$this->documents->contains($document)) {
  155.             $this->documents[] = $document;
  156.         }
  157.     }
  158.     public function removeDocument(Document $document): void
  159.     {
  160.         $this->documents->removeElement($document);
  161.     }
  162.     public function getDocuments(): Collection
  163.     {
  164.         return $this->documents;
  165.     }
  166. }