<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Blameable\Traits\BlameableEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use App\Repository\LabelRepository;
#[ORM\Entity(repositoryClass: LabelRepository::class)]
class Label implements TranslatableInterface
{
use BlameableEntity; //Hook blameable behaviour. Updates createdBy, updatedBy fields
use TimestampableEntity; //Hook timestampable behaviour. Updates createdAt, updatedAt fields
use TranslatableTrait;
#[Assert\Valid]
protected $translations;
#[ORM\Id]
#[ORM\GeneratedValue(strategy: "IDENTITY")]
#[ORM\Column()]
private ?int $id = null;
#[ORM\Column(length: 255, unique: true)]
private ?string $slug = null;
#[ORM\OneToOne(cascade: ['persist', 'remove'])]
private ?Media $icon = null;
#[ORM\ManyToMany(targetEntity: Product::class, mappedBy: 'labels')]
private Collection $products;
public function __call($method, $arguments)
{
return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), $method);
}
public function __construct()
{
$this->products = new ArrayCollection();
}
public function __toString()
{
return $this->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 getIcon(): ?Media
{
return $this->icon;
}
public function setIcon(?Media $icon): self
{
$icon->setDirectory('label');
$this->icon = $icon;
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->addLabel($this);
}
return $this;
}
public function removeProduct(Product $product): self
{
if ($this->products->removeElement($product)) {
$product->removeLabel($this);
}
return $this;
}
public function getTranslatedName($locale)
{
foreach ($this->translations as $translation) {
if ($translation->getLocale() === $locale) {
return $translation->getName() ?? $translation->getSlug();
}
}
return null;
}
}