<?php
namespace App\Entity;
use App\Entity\traits\base64Field;
use App\Entity\traits\Timestapable;
use App\Repository\LevelRepository;
use Cocur\Slugify\Slugify;
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: LevelRepository::class)]
#[HasLifecycleCallbacks]
class Level
{
use Timestapable;
use base64Field;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(["show_recipe"])]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Groups(["list_recipes", "show_recipe"])]
private ?string $name = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
#[Groups(["list_recipes", "show_recipe"])]
private ?string $description = null;
#[ORM\Column(length: 255, nullable: true)]
#[Groups(["list_recipes", "show_recipe"])]
private ?string $slug = null;
#[ORM\OneToMany(mappedBy: 'level', targetEntity: Recipe::class)]
private Collection $recipes;
#[PrePersist]
public function addSlug()
{
$this->slug = (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->setLevel($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->getLevel() === $this) {
$recipe->setLevel(null);
}
}
return $this;
}
}