<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
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;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
/**
* @see https://schema.org/Thing
*
* @author Jordi Fernandes Alves <jfadev@gmail.com>
*/
#[ORM\Entity]
#[ApiResource(
iri: 'Checklist',
itemOperations: [
'get' => ['normalization_context' => ['groups' => 'checklist:item:get', 'enable_max_depth' => true]],
'put' => [
'normalization_context' => ['groups' => 'checklist:item:put', 'enable_max_depth' => true],
'denormalization_context' => ['groups' => 'checklist:item:put', 'enable_max_depth' => true],
],
'delete' => [],
],
collectionOperations: [
'get' => [
'normalization_context' => [
'groups' => ['checklist:collection:get', 'createdAt'],
'enable_max_depth' => true,
],
],
'post' => [
'normalization_context' => ['groups' => 'checklist:collection:post', 'enable_max_depth' => true],
'denormalization_context' => ['groups' => 'checklist:collection:post', 'enable_max_depth' => true],
],
],
)]
#[ApiFilter(
SearchFilter::class,
properties: ['checklistName' => 'partial', 'createdAt' => 'start'],
)]
#[ApiFilter(
OrderFilter::class,
properties: ['checklistName', 'createdAt'],
)]
#[ApiFilter(PropertyFilter::class)]
class Checklist
{
use TimestampableEntity;
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'NONE')]
#[ORM\Column(type: 'uuid', unique: true)]
private ?UuidInterface $id = null;
#[ORM\Column(type: 'string', nullable: false)]
#[ApiProperty(iri: 'https://schema.org/name')]
#[Assert\NotBlank]
#[Assert\Type('string')]
#[Groups(['checklist:collection:get', 'checklist:collection:post', 'checklist:item:get', 'checklist:item:put', 'contract:item:get', 'contract:item:put', 'contract:collection:get', 'contract:collection:post'])]
private ?string $checklistName = null;
#[ORM\OneToMany(targetEntity: 'App\Entity\Task', mappedBy: 'checklist', cascade: ['persist', 'remove'], orphanRemoval: true)]
#[ORM\OrderBy(['taskIndex' => 'ASC'])]
#[Groups(['checklist:collection:get', 'checklist:collection:post', 'checklist:item:get', 'checklist:item:put', 'contract:item:get', 'contract:item:put', 'contract:collection:get', 'contract:collection:post'])]
#[MaxDepth(1)]
private ?Collection $tasks = null;
#[ORM\Column(type: 'text', nullable: true)]
#[ApiProperty(iri: 'https://schema.org/observation')]
#[Assert\Type('string')]
#[Groups(['checklist:collection:get', 'checklist:collection:post', 'checklist:item:get', 'checklist:item:put', 'contract:item:get', 'contract:item:put', 'contract:collection:get', 'contract:collection:post'])]
private ?string $notes = null;
#[ORM\OneToMany(targetEntity: 'App\Entity\Contract', mappedBy: 'checklist')]
#[ApiProperty(iri: 'https://schema.org/Contract')]
#[Groups(['checklist:collection:get', 'checklist:collection:post', 'checklist:item:get', 'checklist:item:put'])]
#[MaxDepth(1)]
private ?Collection $contracts = null;
// #[ORM\OneToMany(targetEntity: 'App\Entity\Lifting', mappedBy: 'checklist')]
// #[ApiProperty(iri: 'https://schema.org/Lifting')]
// #[Groups(['checklist:collection:get', 'checklist:collection:post', 'checklist:item:get', 'checklist:item:put'])]
// #[MaxDepth(1)]
// private ?Collection $liftings = null;
public function __construct()
{
$this->id = Uuid::uuid4();
$this->tasks = new ArrayCollection();
$this->contracts = new ArrayCollection();
// $this->liftings = new ArrayCollection();
}
public function getId(): ?UuidInterface
{
return $this->id;
}
public function setChecklistName(?string $checklistName): void
{
$this->checklistName = $checklistName;
}
public function getChecklistName(): ?string
{
return $this->checklistName;
}
public function setNotes(?string $notes): void
{
$this->notes = $notes;
}
public function getNotes(): ?string
{
return $this->notes;
}
public function addTask(Task $task): void
{
$task->setChecklist($this);
$this->tasks[] = $task;
}
public function removeTask(Task $task): void
{
$this->tasks->removeElement($task);
}
public function getTasks(): Collection
{
return $this->tasks;
}
public function getContracts(): Collection
{
return $this->contracts;
}
public function addContract(Contract $contract): self
{
if (!$this->contracts->contains($contract)) {
$this->contracts[] = $contract;
$contract->setChecklist($this);
}
return $this;
}
public function removeContract(Contract $contract): self
{
if ($this->contracts->removeElement($contract)) {
if ($contract->getChecklist() === $this) {
$contract->setChecklist(null);
}
}
return $this;
}
// public function getLiftings(): Collection
// {
// return $this->liftings;
// }
// public function addLifting(Lifting $lifting): self
// {
// if (!$this->liftings->contains($lifting)) {
// $this->liftings[] = $lifting;
// $lifting->setChecklist($this);
// }
// return $this;
// }
// public function removeLifting(Lifting $lifting): self
// {
// if ($this->liftings->removeElement($lifting)) {
// if ($lifting->getChecklist() === $this) {
// $lifting->setChecklist(null);
// }
// }
// return $this;
// }
}