<?php
namespace App\Entity;
use App\Repository\BrandRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Blameable\Traits\BlameableEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\PropertyAccess\PropertyAccess;
#[ORM\Entity(repositoryClass: BrandRepository::class)]
class Brand 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]
#[Groups("serial")]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Assert\NotNull()]
#[Assert\Length(max: 255)]
#[Groups("serial")]
private ?string $name = null;
#[ORM\Column(nullable: true, options: ['default' => false])]
private ?bool $hidden_on_cover = false;
#[ORM\OneToMany(mappedBy: 'brand', targetEntity: Media::class, cascade: ["persist", "remove"])]
#[ORM\OrderBy(["position" => "ASC"])]
private Collection $media;
#[ORM\OneToMany(mappedBy: 'brand', targetEntity: Product::class)]
private Collection $products;
#[ORM\OneToMany(mappedBy: 'brand', targetEntity: ProductUnique::class)]
private Collection $productUniques;
#[ORM\OneToOne(cascade: ['persist', 'remove'])]
#[Assert\NotNull()]
private ?Media $logo = null;
public function __call($method, $arguments)
{
return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), $method);
}
public function __construct()
{
$this->products = new ArrayCollection();
$this->media = new ArrayCollection();
$this->productUniques = new ArrayCollection();
}
public function __toString()
{
return $this->name;
}
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 getHiddenOnCover(): ?bool
{
return $this->hidden_on_cover;
}
public function setHiddenOnCover(bool $hidden_on_cover): self
{
$this->hidden_on_cover = $hidden_on_cover;
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->setBrand($this);
}
return $this;
}
public function removeProduct(Product $product): self
{
if ($this->products->removeElement($product)) {
// set the owning side to null (unless already changed)
if ($product->getBrand() === $this) {
$product->setBrand(null);
}
}
return $this;
}
/**
* @return Collection<int, ProductUnique>
*/
public function getProductUniques(): Collection
{
return $this->productUniques;
}
public function addProductUnique(ProductUnique $productUnique): self
{
if (!$this->productUniques->contains($productUnique)) {
$this->productUniques->add($productUnique);
$productUnique->setBrand($this);
}
return $this;
}
public function removeProductUnique(ProductUnique $productUnique): self
{
if ($this->productUniques->removeElement($productUnique)) {
// set the owning side to null (unless already changed)
if ($productUnique->getBrand() === $this) {
$productUnique->setBrand(null);
}
}
return $this;
}
public function getLogo(): ?Media
{
return $this->logo;
}
public function setLogo(Media $logo): self
{
$logo->setDirectory('brand');
$this->logo = $logo;
return $this;
}
/**
* Main image is the one that have position 0
* @return Media|null
*/
public function getMainBrandImage(): ?Media
{
if ($this->media) {
$filteredBrandImages = $this->media->filter(function (Media $brandImage) {
return $brandImage->getPosition() == 0;
});
return (count($filteredBrandImages) > 0) ? $filteredBrandImages->first() : null;
} else {
return null;
}
}
/**
* @return Collection<int, Media>
*/
public function getMedia(): Collection
{
return $this->media;
}
public function addMedium(Media $medium): self
{
if (!$this->media->contains($medium)) {
$medium->setDirectory('brand');
$this->media->add($medium);
$medium->setBrand($this);
}
return $this;
}
public function removeMedium(Media $medium): self
{
if ($this->media->removeElement($medium)) {
// set the owning side to null (unless already changed)
if ($medium->getBrand() === $this) {
$medium->setBrand(null);
}
}
return $this;
}
}