<?php
/**
* @author Leo BANNHOLTZER (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\CategoriesBundle\Entity;
use App\Entity\Context;
use Symfony\Component\Uid\Uuid;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use App\DoctrineExtensions\IsActiveEntity;
use Doctrine\Common\Collections\Collection;
use App\DoctrineExtensions\GedmoTree\TreeEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Bluue\CategoriesBundle\Entity\CategoryContext;
use Bluue\CategoriesBundle\Entity\CategoryProduct;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
use Bluue\CategoriesBundle\Repository\CategoryRepository;
use App\DoctrineExtensions\Timestampable\Traits\UserTimestampableEntity;
use App\DoctrineExtensions\SoftDeleteable\Traits\UserSoftDeleteableEntity;
/**
* @ORM\Entity(repositoryClass=CategoryRepository::class)
* @ORM\Table(name="categories_bundle__category", indexes={
* @ORM\Index(name="name", columns={"name"}),
* @ORM\Index(name="is_active", columns={"is_active"}),
* @ORM\Index(name="deleted_at", columns={"deleted_at"}),
* @ORM\Index(name="created_at", columns={"created_at"}),
* @ORM\Index(name="updated_at", columns={"updated_at"})
* })
* @Gedmo\TranslationEntity(class="Bluue\CategoriesBundle\Entity\CategoryTranslation")
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=false)
* @Gedmo\Tree(type="nested")
*/
class Category
{
use TreeEntity;
use UserTimestampableEntity;
use UserSoftDeleteableEntity;
use IsActiveEntity;
/**
* @ORM\Id
* @ORM\Column(type="uuid")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
*/
private ?Uuid $id = null;
/**
* @Gedmo\Translatable
* @ORM\Column(type="string", length=128)
*/
private string $name;
/**
* @Gedmo\Translatable
* @ORM\Column(type="text", nullable=true)
*/
private ?string $description;
/**
* @Gedmo\TreeParent()
* @ORM\ManyToOne(targetEntity=Category::class, inversedBy="childCategories")
*/
private ?Category $parent = null;
/**
* @ORM\OneToMany(targetEntity=Category::class, mappedBy="parent", fetch="EXTRA_LAZY")
* @ORM\OrderBy({"left" = "ASC"})
*/
private Collection $childCategories;
/**
* @ORM\OneToMany(
* targetEntity="CategoryTranslation",
* mappedBy="object",
* cascade={"persist", "remove"},
* fetch="EXTRA_LAZY"
* )
*/
private Collection $translations;
/**
* @ORM\OneToMany(targetEntity=CategoryProduct::class, mappedBy="category", fetch="EXTRA_LAZY")
*/
private Collection $categoryProducts;
/**
* @ORM\OneToMany(targetEntity=CategoryContext::class, mappedBy="category", fetch="EXTRA_LAZY")
*/
private Collection $categoryContexts;
public function __construct()
{
$this->translations = new ArrayCollection();
$this->categoryProducts = new ArrayCollection();
$this->childCategories = new ArrayCollection();
$this->categoryContexts = new ArrayCollection();
}
/**
* @return Uuid|null
*/
public function getId(): ?Uuid
{
return $this->id;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
* @return $this
*/
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return string|null
*/
public function getDescription(): ?string
{
return $this->description;
}
/**
* @param string $description
* @return $this
*/
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
/**
* @return self|null
*/
public function getParent(): ?self
{
return $this->parent;
}
/**
* @param self|null $parent
* @return $this
*/
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @param Context|null $context
* @return Collection|self[]
*/
public function getChildCategories(?Context $context = null): Collection
{
if ($context) {
return $this->childCategories->filter(function (Category $c) use ($context) {
return $c->getCategoryContexts()->exists(function ($key, CategoryContext $cc) use ($context) {
return $cc->getContext() === $context;
});
});
}
return $this->childCategories;
}
/**
* @return Collection|CategoryTranslation[]
*/
public function getTranslations()
{
return $this->translations;
}
/**
* @param CategoryTranslation $t
* @return $this
*/
public function addTranslation(CategoryTranslation $t): self
{
if (!$this->translations->contains($t)) {
$this->translations[] = $t;
$t->setObject($this);
}
return $this;
}
/**
* @return Collection|CategoryProduct[]
*/
public function getCategoryProducts(): Collection
{
return $this->categoryProducts;
}
/**
* @param CategoryProduct $categoryProduct
* @return $this
*/
public function addCategoryProduct(CategoryProduct $categoryProduct): self
{
if (!$this->categoryProducts->contains($categoryProduct)) {
$this->categoryProducts[] = $categoryProduct;
$categoryProduct->setCategory($this);
}
return $this;
}
/**
* @param CategoryProduct $categoryProduct
* @return $this
*/
public function removeCategoryProduct(CategoryProduct $categoryProduct): self
{
if ($this->categoryProducts->removeElement($categoryProduct)) {
if ($categoryProduct->getCategory() === $this) {
$categoryProduct->setCategory(null);
}
}
return $this;
}
/**
* @return Collection|CategoryContext[]
*/
public function getCategoryContexts(): Collection
{
return $this->categoryContexts;
}
/**
* @param Context $context
* @return CategoryContext|null
*/
public function getCategoryContext(Context $context): ?CategoryContext
{
$value = null;
if ($this->getId()) {
foreach ($this->getCategoryContexts() as $cc) {
if ($cc->getContext() == $context) {
$value = $cc;
}
}
}
return $value;
}
/**
* @param CategoryContext $categoryContext
* @return $this
*/
public function addCategoryContext(CategoryContext $categoryContext): self
{
if (!$this->categoryContexts->contains($categoryContext)) {
$this->categoryContexts[] = $categoryContext;
$categoryContext->setCategory($this);
}
return $this;
}
/**
* @param CategoryContext $categoryContext
* @return $this
*/
public function removeCategoryContext(CategoryContext $categoryContext): self
{
if ($this->categoryContexts->removeElement($categoryContext)) {
if ($categoryContext->getCategory() === $this) {
$categoryContext->setCategory(null);
}
}
return $this;
}
/**
* @return string
*/
public function getChoiceLabel(): string
{
// ne pas afficher la catégorie par défaut
$path = '';
$parent = $this->getParent();
while ($parent) {
$path = $parent->getName() . ' > ' . $path;
$parent = $parent->getParent();
}
return $path . $this->getName();
}
}