<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Operation\Operation;
use App\Repository\EligibilityRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Ramsey\Uuid\Doctrine\UuidGenerator;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName;
/**
* @ApiResource(
* attributes={
* "normalization_context"={"groups"={
* "Eligibility:output",
* "Eligibility:io"
* }},
* "denormalization_context"={"groups"={
* "Eligibility:input",
* "Eligibility:io"
* }}
* },
* collectionOperations={
* "get"={
* "method"="GET",
* "access_control"="is_granted('ROLE_USER')",
* },
* },
* itemOperations={
* "get"={
* "method"="GET",
* "access_control"="is_granted('ROLE_USER')",
* },
* }
* )
* @ORM\Entity(repositoryClass=EligibilityRepository::class)
*/
class Eligibility
{
public const ELIGIBILITY_LIST = [
self::ELIGIBILITY_LABEL_ALL,
self::ELIGIBILITY_LABEL_BIRTHDAY,
self::ELIGIBILITY_LABEL_DEPARTMENT,
self::ELIGIBILITY_NEW_CLIENT
];
public const ELIGIBILITY_LABEL_ALL = 'all';
public const ELIGIBILITY_LABEL_BIRTHDAY = 'birthday';
public const ELIGIBILITY_LABEL_DEPARTMENT = 'department';
public const ELIGIBILITY_NEW_CLIENT = 'new_client';
/**
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;
/**
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
* @SerializedName("id")
* @ApiProperty(identifier=true)
* @Groups({
* "Eligibility:output",
* "get_all_public_coupons",
* "get_coupons_by_user_department",
* "OperationOdr:io",
* "Operation:io",
* })
*/
private string $id;
/**
* @ORM\Column(type="string", length=255)
* @SerializedName("label")
* @Groups({
* "Eligibility:output",
* "get_all_public_coupons",
* "get_coupons_by_user_department",
* "OperationOdr:io",
* "Operation:io",
* })
*/
private ?string $label;
/**
* @ORM\ManyToMany(targetEntity=Operation::class, inversedBy="eligibilities")
*/
private Collection $operation;
public function __construct()
{
$this->operation = new ArrayCollection();
}
public function getId(): ?string
{
return $this->id;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
/**
* @return Collection|Operation[]
*/
public function getOperation(): Collection
{
return $this->operation;
}
public function addOperation(Operation $operation): self
{
if (!$this->operation->contains($operation)) {
$this->operation[] = $operation;
}
return $this;
}
public function removeOperation(Operation $operation): self
{
$this->operation->removeElement($operation);
return $this;
}
public function checkEligibility(): bool
{
return in_array($this->label, self::ELIGIBILITY_LIST, true);
}
public function __toString()
{
return $this->label;
}
}