<?php
namespace App\Entity;
use App\Repository\CategoryRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;use Doctrine\DBAL\Types\Types;use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: CategoryRepository::class)]
class Category
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\Column(length: 100)]
private ?string $slug = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $description = null;
#[ORM\Column(length: 255)]
private ?string $image = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $bannerImage = null;
#[ORM\Column(length: 160, nullable: true)]
private ?string $metaTitle = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $metaDescription = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $metaKeywords = null;
#[ORM\Column]
private ?bool $isActive = null;
#[ORM\Column]
private ?int $productCount = null;
#[ORM\Column]
private ?int $viewCount = null;
#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\Column]
private ?\DateTimeImmutable $updatedAt = null;
#[ORM\OneToMany(targetEntity: Product::class, mappedBy: 'category')]
private Collection $products;
public function __construct()
{
$this->products = new ArrayCollection();
}
public function __toString(): string
{
return $this->name;
}
// === LIFECYCLE CALLBACKS ===
#[ORM\PrePersist]
#[ORM\PreUpdate]
public function updateTimestamps(): void
{
$this->updatedAt = new \DateTimeImmutable();
}
#[ORM\PrePersist]
public function generateSlug(): void
{
if (empty($this->slug)) {
$this->slug = $this->createSlug($this->name);
}
}
// === MÉTHODES UTILES ===
private function createSlug(string $text): string
{
$slug = preg_replace('/[^a-z0-9]+/', '-', strtolower($text));
return trim($slug, '-');
}
public function getFullPath(): string
{
$path = [];
$category = $this;
while ($category !== null) {
$path[] = $category->getName();
$category = $category->getParent();
}
return implode(' > ', array_reverse($path));
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
public function incrementProductCount(): void
{
$this->productCount++;
}
public function decrementProductCount(): void
{
$this->productCount = max(0, $this->productCount - 1);
}
public function incrementViewCount(): void
{
$this->viewCount++;
}
public function getId(): ?int
{
return $this->id;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): static
{
$this->slug = $slug;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): static
{
$this->description = $description;
return $this;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(string $image): static
{
$this->image = $image;
return $this;
}
public function getBannerImage(): ?string
{
return $this->bannerImage;
}
public function setBannerImage(?string $bannerImage): static
{
$this->bannerImage = $bannerImage;
return $this;
}
public function getMetaTitle(): ?string
{
return $this->metaTitle;
}
public function setMetaTitle(?string $metaTitle): static
{
$this->metaTitle = $metaTitle;
return $this;
}
public function getMetaDescription(): ?string
{
return $this->metaDescription;
}
public function setMetaDescription(?string $metaDescription): static
{
$this->metaDescription = $metaDescription;
return $this;
}
public function getMetaKeywords(): ?string
{
return $this->metaKeywords;
}
public function setMetaKeywords(?string $metaKeywords): static
{
$this->metaKeywords = $metaKeywords;
return $this;
}
public function isIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): static
{
$this->isActive = $isActive;
return $this;
}
public function getProductCount(): ?int
{
return $this->productCount;
}
public function setProductCount(int $productCount): static
{
$this->productCount = $productCount;
return $this;
}
public function getViewCount(): ?int
{
return $this->viewCount;
}
public function setViewCount(int $viewCount): static
{
$this->viewCount = $viewCount;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeImmutable $updatedAt): static
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return Collection<int, Product>
*/
public function getProducts(): Collection
{
return $this->products;
}
public function addProduct(Product $product): static
{
if (!$this->products->contains($product)) {
$this->products->add($product);
$product->setCategory($this);
}
return $this;
}
public function removeProduct(Product $product): static
{
if ($this->products->removeElement($product)) {
// set the owning side to null (unless already changed)
if ($product->getCategory() === $this) {
$product->setCategory(null);
}
}
return $this;
}
}