src/Entity/Checklist.php line 105

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use ApiPlatform\Core\Annotation\ApiFilter;
  5. use ApiPlatform\Core\Annotation\ApiProperty;
  6. use ApiPlatform\Core\Annotation\ApiResource;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  8. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  9. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  10. use App\Trait\TimestampableEntity;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. use Symfony\Component\Serializer\Annotation\Groups;
  16. use Symfony\Component\Serializer\Annotation\MaxDepth;
  17. use Ramsey\Uuid\Uuid;
  18. use Ramsey\Uuid\UuidInterface;
  19. #[ORM\Entity]
  20. #[ApiResource(
  21.     iri'Checklist',
  22.     itemOperations: [
  23.         'get' => [
  24.             'security' => "
  25.                 is_granted('ROLE_ADMIN') or 
  26.                 is_granted('ROLE_OPERATOR') or
  27.                 is_granted('ROLE_OPERATIONS_COORDINATOR')
  28.             ",
  29.             'normalization_context' => [
  30.                 'groups' => 'checklist:item:get'
  31.                 'enable_max_depth' => true
  32.             ]
  33.         ],
  34.         'put' => [
  35.             'security' => "
  36.                 is_granted('ROLE_ADMIN') or 
  37.                 is_granted('ROLE_OPERATIONS_COORDINATOR')
  38.             ",
  39.             'normalization_context' => [
  40.                 'groups' => 'checklist:item:put'
  41.                 'enable_max_depth' => true
  42.             ],
  43.             'denormalization_context' => [
  44.                 'groups' => 'checklist:item:put'
  45.                 'enable_max_depth' => true
  46.             ],
  47.         ],
  48.         'delete' => [
  49.             'security' => "
  50.                 is_granted('ROLE_ADMIN') or 
  51.                 is_granted('ROLE_OPERATIONS_COORDINATOR')
  52.             ",
  53.         ],
  54.     ],
  55.     collectionOperations: [
  56.         'get' => [
  57.             'security' => "
  58.                 is_granted('ROLE_ADMIN') or 
  59.                 is_granted('ROLE_OPERATOR') or
  60.                 is_granted('ROLE_OPERATIONS_COORDINATOR')
  61.             ",
  62.             'normalization_context' => [
  63.                 'groups' => [
  64.                     'checklist:collection:get'
  65.                     'createdAt'
  66.                 ],
  67.                 'enable_max_depth' => true,
  68.             ],
  69.         ],
  70.         'post' => [
  71.             'security' => "
  72.                 is_granted('ROLE_ADMIN') or 
  73.                 is_granted('ROLE_OPERATIONS_COORDINATOR')
  74.             ",
  75.             'normalization_context' => [
  76.                 'groups' => 'checklist:collection:post'
  77.                 'enable_max_depth' => true
  78.             ],
  79.             'denormalization_context' => [
  80.                 'groups' => 'checklist:collection:post'
  81.                 'enable_max_depth' => true
  82.             ],
  83.         ],
  84.     ],
  85. )]
  86. #[ApiFilter(
  87.     SearchFilter::class,
  88.     properties: [
  89.         'checklistName' => 'partial'
  90.         'createdAt' => 'start'
  91.     ],
  92. )]
  93. #[ApiFilter(
  94.     OrderFilter::class,
  95.     properties: [
  96.         'checklistName'
  97.         'createdAt'
  98.     ],
  99. )]
  100. #[ApiFilter(PropertyFilter::class)]
  101. class Checklist
  102. {
  103.     use TimestampableEntity;
  104.     
  105.     #[ORM\Id]
  106.     #[ORM\GeneratedValue(strategy'NONE')]
  107.     #[ORM\Column(type'uuid'uniquetrue)]
  108.     private ?UuidInterface $id null;
  109.     #[ORM\Column(type'string'nullablefalse)]
  110.     #[ApiProperty()]
  111.     #[Assert\NotBlank]
  112.     #[Assert\Type('string')]
  113.     #[Groups([
  114.         'checklist:collection:get'
  115.         'checklist:collection:post'
  116.         'checklist:item:get'
  117.         'checklist:item:put'
  118.         'contract:item:get'
  119.         'contract:item:put'
  120.         'contract:collection:get'
  121.         'contract:collection:post'
  122.     ])]
  123.     private ?string $checklistName null;
  124.     #[ORM\OneToMany(
  125.         targetEntityTask::class, 
  126.         mappedBy'checklist'
  127.         cascade: ['persist''remove'], 
  128.         orphanRemovaltrue
  129.     )]
  130.     #[ORM\OrderBy(['taskIndex' => 'ASC'])]
  131.     #[Groups([
  132.         'checklist:collection:get'
  133.         'checklist:collection:post'
  134.         'checklist:item:get'
  135.         'checklist:item:put'
  136.         'contract:item:get'
  137.         'contract:item:put'
  138.         'contract:collection:get'
  139.         'contract:collection:post'
  140.     ])]
  141.     #[MaxDepth(1)]
  142.     private ?Collection $tasks null;
  143.     #[ApiProperty()]
  144.     #[Groups(['checklist:collection:get'])]
  145.     private ?int $taskCount null;
  146.     #[ORM\Column(type'text'nullabletrue)]
  147.     #[ApiProperty()]
  148.     ##[Assert\Type('string')]
  149.     #[Groups([
  150.         'checklist:collection:get'
  151.         'checklist:collection:post'
  152.         'checklist:item:get'
  153.         'checklist:item:put'
  154.         'contract:item:get'
  155.         'contract:item:put'
  156.         'contract:collection:get'
  157.         'contract:collection:post'
  158.     ])]
  159.     private ?string $notes null;
  160.     #[ORM\OneToMany(
  161.         targetEntityContract::class, 
  162.         mappedBy'checklist'
  163.     )]
  164.     private ?Collection $contracts null;
  165.     public function __construct()
  166.     {
  167.         $this->id Uuid::uuid4();
  168.         $this->tasks = new ArrayCollection();
  169.         $this->contracts = new ArrayCollection();
  170.     }
  171.     public function getId(): ?UuidInterface
  172.     {
  173.         return $this->id;
  174.     }
  175.     public function setChecklistName(?string $checklistName): void
  176.     {
  177.         $this->checklistName $checklistName;
  178.     }
  179.     public function getChecklistName(): ?string
  180.     {
  181.         return $this->checklistName;
  182.     }
  183.     public function setNotes(?string $notes): void
  184.     {
  185.         $this->notes $notes;
  186.     }
  187.     public function getNotes(): ?string
  188.     {
  189.         return $this->notes;
  190.     }
  191.     public function addTask(Task $task): void
  192.     {
  193.         $task->setChecklist($this);
  194.         $this->tasks[] = $task;
  195.     }
  196.     public function removeTask(Task $task): void
  197.     {
  198.         $this->tasks->removeElement($task);
  199.     }
  200.     public function getTasks(): Collection
  201.     {
  202.         return $this->tasks;
  203.     }
  204.     public function getTaskCount(): ?int
  205.     {
  206.         return $this->tasks->count();
  207.     }
  208.     public function getContracts(): Collection
  209.     {
  210.         return $this->contracts;
  211.     }
  212.     public function addContract(Contract $contract): self
  213.     {
  214.         if (!$this->contracts->contains($contract)) {
  215.             $this->contracts[] = $contract;
  216.             $contract->setChecklist($this);
  217.         }
  218.         return $this;
  219.     }
  220.     public function removeContract(Contract $contract): self
  221.     {
  222.         if ($this->contracts->removeElement($contract)) {
  223.             if ($contract->getChecklist() === $this) {
  224.                 $contract->setChecklist(null);
  225.             }
  226.         }
  227.         
  228.         return $this;
  229.     }
  230. }