<?php
namespace App\Entity;
use App\Entity\traits\Timestapable;
use App\Repository\BudgetRepository;
use Cocur\Slugify\Slugify;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Doctrine\ORM\Mapping\PrePersist;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: BudgetRepository::class)]
#[HasLifecycleCallbacks]
class Budget
{
use Timestapable;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(["show_recipe"])]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Groups(["show_recipe", "for_abj"])]
private ?string $name = null;
#[ORM\Column(type: Types::TEXT)]
#[Groups(["show_recipe"])]
private ?string $description = null;
#[ORM\Column(length: 255, nullable: true)]
#[Groups(["show_recipe"])]
private ?string $slug = null;
#[ORM\OneToMany(mappedBy: 'budget', targetEntity: Recipe::class)]
private Collection $recipes;
#[PrePersist]
public function addSlugAndCreatedAt(){
$this->setCreatedAt(new \DateTimeImmutable());
$this->setSlug((new Slugify())->slugify($this->name));
}
public function __construct()
{
$this->recipes = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(?string $slug): self
{
$this->slug = $slug;
return $this;
}
/**
* @return Collection<int, Recipe>
*/
public function getRecipes($need=false)
{
if ($need) {
return $this->recipes;
}else{
return null;
}
}
public function addRecipe(Recipe $recipe): self
{
if (!$this->recipes->contains($recipe)) {
$this->recipes->add($recipe);
$recipe->setBudget($this);
}
return $this;
}
public function removeRecipe(Recipe $recipe): self
{
if ($this->recipes->removeElement($recipe)) {
// set the owning side to null (unless already changed)
if ($recipe->getBudget() === $this) {
$recipe->setBudget(null);
}
}
return $this;
}
}