<?php
namespace App\Entity;
use App\Repository\PlanRepository;
use App\Traits\DateTimeTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Overblog\GraphQLBundle\Annotation\Field;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: PlanRepository::class)]
#[ORM\HasLifecycleCallbacks]
#[UniqueEntity(
fields: "name",
message: "This name is already used."
)]
class Plan extends BasicEntity
{
use DateTimeTrait;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: "integer")]
#[Field(name: "id", type: "int")]
private $id;
#[ORM\Column(type: "integer")]
#[Assert\NotBlank]
#[Field(name: "nbr_domains", type: "int")]
private $nbrDomains;
#[ORM\Column(type: "string", length: 255)]
#[Assert\NotBlank]
#[Field(name: "name", type: "string")]
private $name;
#[ORM\OneToMany(targetEntity: Subscription::class, mappedBy: "plan")]
private $subscriptions;
#[ORM\ManyToMany(targetEntity: Service::class)]
#[Field(name: "services")]
private $services;
#[ORM\Column(type: "float")]
#[Assert\NotBlank]
#[Field(name: "price", type: "float")]
private $price;
#[ORM\Column(type: "string", length: 30, nullable: true)]
#[Assert\NotBlank]
#[Field(name: "color", type: "string")]
private $color;
#[ORM\Column(options: ['default' => false])]
#[Field(name: "is_deleted", type: "boolean")]
private ?bool $isDeleted = null;
public function __construct()
{
$this->subscriptions = new ArrayCollection();
$this->services = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getNbrDomains(): ?int
{
return $this->nbrDomains;
}
public function setNbrDomains(int $nbrDomains): self
{
$this->nbrDomains = $nbrDomains;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
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->setPlan($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->getPlan() === $this) {
$subscription->setPlan(null);
}
}
return $this;
}
/**
* @return Collection|Service[]
*/
public function getServices(): ?Collection
{
return $this->services;
}
public function addService(Service $service): self
{
if (!$this->services->contains($service)) {
$this->services[] = $service;
}
return $this;
}
public function removeService(Service $service): self
{
$this->services->removeElement($service);
return $this;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(float $price): self
{
$this->price = $price;
return $this;
}
public function __toString()
{
return $this->getName();
}
public function getColor(): ?string
{
return $this->color;
}
public function setColor(?string $color): self
{
$this->color = $color;
return $this;
}
/**
* @param array $services
* @return $this
*/
public function setServices(array $services): self
{
$this->services = new ArrayCollection();
foreach ($services as $service) {
$this->addService($service);
}
return $this;
}
public function isIsDeleted(): ?bool
{
return $this->isDeleted;
}
public function setIsDeleted(bool $isDeleted): static
{
$this->isDeleted = $isDeleted;
return $this;
}
}