<?php
namespace App\Entity;
use App\Repository\DiscountCodeRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;
#[ORM\Entity(repositoryClass: DiscountCodeRepository::class)]
class DiscountCode
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addConstraint(new UniqueEntity([
'fields' => 'code',
'message' => 'Already use',
]));
}
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 255)]
private $code;
#[ORM\Column(type: 'float', nullable: true)]
#[Assert\Type(type: 'float', message: 'Veillez saisir un nombre valide')]
#[Assert\Expression('this.checkRange()', message: 'Veillez saisir un nombre valide')]
private $discount;
#[ORM\Column(type: 'string', length: 255)]
private $type;
#[ORM\Column(type: 'date')]
private $createdAt;
#[ORM\Column(type: 'string', length: 255)]
private $status;
#[ORM\Column(type: 'date', nullable: true)]
private $validity;
#[ORM\Column(type: 'integer', nullable: true)]
private $numberOfUses;
#[ORM\OneToMany(targetEntity: Subscription::class, mappedBy: 'discount')]
private $subscriptions;
#[ORM\Column(length: 255, nullable: true)]
private ?string $recipientEmail = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $recipientType = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $language = null;
public function __construct()
{
$this->subscriptions = new ArrayCollection();
}
public function checkRange(){
if($this->discount > 100 || $this->discount < 0){
return false;
}
return true;
}
public function getId(): ?int
{
return $this->id;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
public function getDiscount(): ?float
{
return $this->discount;
}
public function setDiscount(?float $discount): self
{
$this->discount = $discount;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
public function getValidity(): ?\DateTimeInterface
{
return $this->validity;
}
public function setValidity(?\DateTimeInterface $validity): self
{
$this->validity = $validity;
return $this;
}
public function getNumberOfUses(): ?int
{
return $this->numberOfUses;
}
public function setNumberOfUses(?int $numberOfUses): self
{
$this->numberOfUses = $numberOfUses;
return $this;
}
/**
* @return Collection|Subscription[]
*/
public function getSubscriptions(): Collection
{
return $this->subscriptions;
}
public function addSubscription(Subscription $subscription): self
{
if (!$this->subscriptions->contains($subscription)) {
$this->subscriptions[] = $subscription;
$subscription->setDiscount($this);
}
return $this;
}
public function removeSubscription(Subscription $subscription): self
{
if ($this->subscriptions->removeElement($subscription)) {
// set the owning side to null (unless already changed)
if ($subscription->getDiscount() === $this) {
$subscription->setDiscount(null);
}
}
return $this;
}
public function getRecipientEmail(): ?string
{
return $this->recipientEmail;
}
public function setRecipientEmail(?string $recipientEmail): static
{
$this->recipientEmail = $recipientEmail;
return $this;
}
public function getRecipientType(): ?string
{
return $this->recipientType;
}
public function setRecipientType(?string $recipientType): static
{
$this->recipientType = $recipientType;
return $this;
}
public function getLanguage(): ?string
{
return $this->language;
}
public function setLanguage(?string $language): static
{
$this->language = $language;
return $this;
}
}