src/Entity/Category.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CategoryRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;use Doctrine\DBAL\Types\Types;use Doctrine\ORM\Mapping as ORM;
  6. #[ORM\Entity(repositoryClassCategoryRepository::class)]
  7. class Category
  8. {
  9.     #[ORM\Id]
  10.     #[ORM\GeneratedValue]
  11.     #[ORM\Column]
  12.     private ?int $id null;
  13.     #[ORM\Column(length255)]
  14.     private ?string $name null;
  15.     #[ORM\Column(length100)]
  16.     private ?string $slug null;
  17.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  18.     private ?string $description null;
  19.     #[ORM\Column(length255)]
  20.     private ?string $image null;
  21.     #[ORM\Column(length255nullabletrue)]
  22.     private ?string $bannerImage null;
  23.     #[ORM\Column(length160nullabletrue)]
  24.     private ?string $metaTitle null;
  25.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  26.     private ?string $metaDescription null;
  27.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  28.     private ?string $metaKeywords null;
  29.     #[ORM\Column]
  30.     private ?bool $isActive null;
  31.     #[ORM\Column]
  32.     private ?int $productCount null;
  33.     #[ORM\Column]
  34.     private ?int $viewCount null;
  35.     #[ORM\Column]
  36.     private ?\DateTimeImmutable $createdAt null;
  37.     #[ORM\Column]
  38.     private ?\DateTimeImmutable $updatedAt null;
  39.     #[ORM\OneToMany(targetEntityProduct::class, mappedBy'category')]
  40.     private Collection $products;
  41.     public function __construct()
  42.     {
  43.         $this->products = new ArrayCollection();
  44.     }
  45.     public function __toString(): string
  46.     {
  47.         return $this->name;
  48.     }
  49.     // === LIFECYCLE CALLBACKS ===
  50.     #[ORM\PrePersist]
  51.     #[ORM\PreUpdate]
  52.     public function updateTimestamps(): void
  53.     {
  54.         $this->updatedAt = new \DateTimeImmutable();
  55.     }
  56.     #[ORM\PrePersist]
  57.     public function generateSlug(): void
  58.     {
  59.         if (empty($this->slug)) {
  60.             $this->slug $this->createSlug($this->name);
  61.         }
  62.     }
  63.     // === MÉTHODES UTILES ===
  64.     private function createSlug(string $text): string
  65.     {
  66.         $slug preg_replace('/[^a-z0-9]+/''-'strtolower($text));
  67.         return trim($slug'-');
  68.     }
  69.     public function getFullPath(): string
  70.     {
  71.         $path = [];
  72.         $category $this;
  73.         while ($category !== null) {
  74.             $path[] = $category->getName();
  75.             $category $category->getParent();
  76.         }
  77.         return implode(' > 'array_reverse($path));
  78.     }
  79.     public function getName(): ?string
  80.     {
  81.         return $this->name;
  82.     }
  83.     public function setName(string $name): static
  84.     {
  85.         $this->name $name;
  86.         return $this;
  87.     }
  88.     public function incrementProductCount(): void
  89.     {
  90.         $this->productCount++;
  91.     }
  92.     public function decrementProductCount(): void
  93.     {
  94.         $this->productCount max(0$this->productCount 1);
  95.     }
  96.     public function incrementViewCount(): void
  97.     {
  98.         $this->viewCount++;
  99.     }
  100.     public function getId(): ?int
  101.     {
  102.         return $this->id;
  103.     }
  104.     public function getSlug(): ?string
  105.     {
  106.         return $this->slug;
  107.     }
  108.     public function setSlug(string $slug): static
  109.     {
  110.         $this->slug $slug;
  111.         return $this;
  112.     }
  113.     public function getDescription(): ?string
  114.     {
  115.         return $this->description;
  116.     }
  117.     public function setDescription(?string $description): static
  118.     {
  119.         $this->description $description;
  120.         return $this;
  121.     }
  122.     public function getImage(): ?string
  123.     {
  124.         return $this->image;
  125.     }
  126.     public function setImage(string $image): static
  127.     {
  128.         $this->image $image;
  129.         return $this;
  130.     }
  131.     public function getBannerImage(): ?string
  132.     {
  133.         return $this->bannerImage;
  134.     }
  135.     public function setBannerImage(?string $bannerImage): static
  136.     {
  137.         $this->bannerImage $bannerImage;
  138.         return $this;
  139.     }
  140.     public function getMetaTitle(): ?string
  141.     {
  142.         return $this->metaTitle;
  143.     }
  144.     public function setMetaTitle(?string $metaTitle): static
  145.     {
  146.         $this->metaTitle $metaTitle;
  147.         return $this;
  148.     }
  149.     public function getMetaDescription(): ?string
  150.     {
  151.         return $this->metaDescription;
  152.     }
  153.     public function setMetaDescription(?string $metaDescription): static
  154.     {
  155.         $this->metaDescription $metaDescription;
  156.         return $this;
  157.     }
  158.     public function getMetaKeywords(): ?string
  159.     {
  160.         return $this->metaKeywords;
  161.     }
  162.     public function setMetaKeywords(?string $metaKeywords): static
  163.     {
  164.         $this->metaKeywords $metaKeywords;
  165.         return $this;
  166.     }
  167.     public function isIsActive(): ?bool
  168.     {
  169.         return $this->isActive;
  170.     }
  171.     public function setIsActive(bool $isActive): static
  172.     {
  173.         $this->isActive $isActive;
  174.         return $this;
  175.     }
  176.     public function getProductCount(): ?int
  177.     {
  178.         return $this->productCount;
  179.     }
  180.     public function setProductCount(int $productCount): static
  181.     {
  182.         $this->productCount $productCount;
  183.         return $this;
  184.     }
  185.     public function getViewCount(): ?int
  186.     {
  187.         return $this->viewCount;
  188.     }
  189.     public function setViewCount(int $viewCount): static
  190.     {
  191.         $this->viewCount $viewCount;
  192.         return $this;
  193.     }
  194.     public function getCreatedAt(): ?\DateTimeImmutable
  195.     {
  196.         return $this->createdAt;
  197.     }
  198.     public function setCreatedAt(\DateTimeImmutable $createdAt): static
  199.     {
  200.         $this->createdAt $createdAt;
  201.         return $this;
  202.     }
  203.     public function getUpdatedAt(): ?\DateTimeImmutable
  204.     {
  205.         return $this->updatedAt;
  206.     }
  207.     public function setUpdatedAt(\DateTimeImmutable $updatedAt): static
  208.     {
  209.         $this->updatedAt $updatedAt;
  210.         return $this;
  211.     }
  212.     /**
  213.      * @return Collection<int, Product>
  214.      */
  215.     public function getProducts(): Collection
  216.     {
  217.         return $this->products;
  218.     }
  219.     public function addProduct(Product $product): static
  220.     {
  221.         if (!$this->products->contains($product)) {
  222.             $this->products->add($product);
  223.             $product->setCategory($this);
  224.         }
  225.         return $this;
  226.     }
  227.     public function removeProduct(Product $product): static
  228.     {
  229.         if ($this->products->removeElement($product)) {
  230.             // set the owning side to null (unless already changed)
  231.             if ($product->getCategory() === $this) {
  232.                 $product->setCategory(null);
  233.             }
  234.         }
  235.         return $this;
  236.     }
  237. }