<?php
// src/Entity/Subcategory.php
namespace App\Entity;
use App\Repository\SubcategoryRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Blameable\Traits\BlameableEntity;
use Gedmo\Mapping\Annotation\Slug;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
#[ORM\MappedSuperclass()]
#[UniqueEntity('slug')]
#[ORM\HasLifecycleCallbacks]
#[ORM\Entity(repositoryClass: SubcategoryRepository::class)]
class Subcategory implements TranslatableInterface
{
use BlameableEntity; //Hook blameable behaviour. Updates createdBy, updatedBy fields
use TimestampableEntity; //Hook timestampable behaviour. Updates createdAt, updatedAt fields
use TranslatableTrait;
#[ORM\Id]
#[ORM\GeneratedValue(strategy: "IDENTITY")]
#[ORM\Column]
private ?int $id = null;
#[Assert\Valid]
protected $translations;
#[Assert\NotNull(message: "La subcategorĂa debe estar asociada a una categorĂa.")]
#[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'subcategories')]
#[ORM\JoinColumn(nullable: false)]
private ?Category $category = null;
#[ORM\Column(length: 255, unique: true)]
#[Slug(fields: ['slug'])]
private ?string $slug = null;
#[ORM\ManyToMany(targetEntity: Product::class, mappedBy: 'subcategories')]
private Collection $products;
#[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'subcategoryInterests')]
private Collection $users;
#[ORM\OneToOne(cascade: ['persist', 'remove'])]
private ?Media $icon = null;
#[ORM\Column(nullable: true)]
private ?bool $active = null;
#[ORM\Column(nullable: true)]
private ?bool $featured = null;
#[ORM\Column(type: Types::SMALLINT, nullable: true)]
private ?int $homepageOrder = null;
public function __construct()
{
$this->products = new ArrayCollection();
$this->users = new ArrayCollection();
}
public function __call($method, $arguments)
{
return $this->proxyCurrentLocaleTranslation($method, $arguments);
}
public function __toString()
{
return $this->translate($this->getCurrentLocale())->getName();
}
#[ORM\PrePersist]
public function setSlugValue(){
$this->setSlug($this->translate('es')->getName());
}
public function getId(): ?int
{
return $this->id;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): self
{
$this->category = $category;
return $this;
}
/**
* @return Collection<int, Product>
*/
public function getProducts(): Collection
{
return $this->products;
}
public function addProduct(Product $product): self
{
if (!$this->products->contains($product)) {
$this->products->add($product);
$product->addSubcategory($this);
}
return $this;
}
public function removeProduct(Product $product): self
{
if ($this->products->removeElement($product)) {
$product->removeSubcategory($this);
}
return $this;
}
/**
* @return Collection<int, User>
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users->add($user);
$user->addSubcategoryInterest($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->removeElement($user)) {
$user->removeSubcategoryInterest($this);
}
return $this;
}
public function getIcon(): ?Media
{
return $this->icon;
}
public function setIcon(?Media $icon): self
{
$icon->setDirectory('category');
$this->icon = $icon;
return $this;
}
public function isActive(): ?bool
{
return $this->active;
}
public function setActive(?bool $active): self
{
$this->active = $active;
return $this;
}
public function isFeatured(): ?bool
{
return $this->featured;
}
public function setFeatured(?bool $featured): self
{
$this->featured = $featured;
return $this;
}
public function getHomepageOrder(): ?int
{
return $this->homepageOrder;
}
public function setHomepageOrder(?int $homepageOrder): self
{
$this->homepageOrder = $homepageOrder;
return $this;
}
}