<?php
namespace App\Entity;
use App\Repository\CategoryRepository;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\Common\Collections\ArrayCollection;
use JMS\Serializer\Annotation as Serializer;
#[Gedmo\Tree(type: 'nested')]
#[Serializer\ExclusionPolicy("ALL")]
#[ORM\Entity(repositoryClass: CategoryRepository::class)]
#[ORM\Table(name: '`category`')]
class Category
{
#[ORM\Id]
#[ORM\GeneratedValue(strategy:"AUTO")]
#[ORM\Column(type: "integer")]
#[Serializer\Expose]
protected ?int $id = null;
#[ORM\Column(type: "boolean")]
private $enabled = true;
/**
* @Serializer\Expose()
*/
#[ORM\Column(type: "string", length: 250)]
protected $name;
/**
* @Gedmo\Slug(handlers={
* @Gedmo\SlugHandler(class="Gedmo\Sluggable\Handler\TreeSlugHandler", options={
* @Gedmo\SlugHandlerOption(name="parentRelationField", value="parent"),
* @Gedmo\SlugHandlerOption(name="separator", value="/")
* })
* }, fields={"name"})
* @Serializer\Expose()
*/
#[ORM\Column(type: "string", length: 128, unique: true, nullable: true )]
protected $slug;
#[Gedmo\TreeParent]
#[Serializer\Expose]
#[ORM\ManyToOne(targetEntity:"Category", inversedBy:"children", fetch: "EAGER")]
#[ORM\JoinColumn(name: "parent_id", referencedColumnName: "id", onDelete: "CASCADE")]
protected $parent;
#[ORM\OneToMany(targetEntity:"Category", mappedBy:"parent", orphanRemoval: true, cascade: ['all'])]
#[ORM\OrderBy(["lft" => "ASC"])]
#[ORM\JoinColumn(onDelete: "CASCADE")]
protected $children;
#[Gedmo\TreeLeft]
#[ORM\Column(type: "integer")]
protected $lft;
#[Gedmo\TreeRight]
#[ORM\Column(type: "integer")]
protected $rgt;
#[Gedmo\TreeLevel]
#[ORM\Column(type: "integer")]
protected $lvl;
#[Gedmo\TreeRoot]
#[ORM\Column(type: "integer", nullable: true)]
protected $root;
public function __construct()
{
$this->children = new ArrayCollection;
}
public function getId()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function getSlug()
{
return $this->slug;
}
public function setSlug($slug)
{
$this->slug = $slug;
}
public function setParent($parent)
{
$this->parent = $parent;
}
public function getParent()
{
return $this->parent;
}
public function addChild(Category $child)
{
if (!$this->children->contains($child)) {
$child->setParent($this);
$this->children->add($child);
}
}
public function removeChild(Category $child)
{
if ($this->children->contains($child)) {
$child->setParent(null);
$this->children->removeElement($child);
}
}
public function getChildren()
{
return $this->children;
}
public function __toString()
{
return $this->name;
}
public function setLft($lft)
{
$this->lft = $lft;
}
public function getLft()
{
return $this->lft;
}
public function setRgt($rgt)
{
$this->rgt = $rgt;
}
public function getRgt()
{
return $this->rgt;
}
public function setLvl($lvl)
{
$this->lvl = $lvl;
}
public function getLvl()
{
return $this->lvl;
}
public function setRoot($root)
{
$this->root = $root;
}
public function getRoot()
{
return $this->root;
}
public function isChildOf(Category $category)
{
$me = $this;
while ($me->parent) {
if ($category === $me->parent) {
return true;
}
$me = $me->parent;
}
return false;
}
public function getEnabled()
{
return $this->enabled;
}
public function setEnabled($enabled)
{
$this->enabled = $enabled;
}
public function getPath()
{
if ($this->lvl === 1 || $this->lvl === 0) {
return $this->name;
}
return $this->parent->getName().'/'.$this->name;
}
}