<?php
namespace Uniski\ConfigBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Category Entity
*
* @ORM\Entity
* @ORM\Table("category")
*/
class Category
{
/**
* @var integer
*
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string")
*/
protected $name;
/**
* @var integer
*
* @ORM\Column(name="weight", type="integer")
*/
protected $weight = 0;
/**
* Parent
*
* @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL")
*/
protected $parent = null;
/**
* Children
*
* @ORM\OneToMany(targetEntity="Category", mappedBy="parent", cascade={"all"})
* @ORM\OrderBy({"weight" = "asc"})
*/
protected $children = array();
/**
* @Gedmo\Slug(fields={"name"})
* @ORM\Column(length=128, unique=true)
*/
private $slug;
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getId()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setWeight($weight)
{
$this->weight = $weight;
return $this;
}
public function getWeight()
{
return $this->weight;
}
public function setParent(Category $category)
{
$this->parent = $category;
return $this;
}
public function getParent()
{
return $this->parent;
}
public function addChild(Category $category)
{
$this->children[$category->getName()] = $category;
return $this;
}
public function hasChild($name)
{
return array_key_exists($name, $this->children);
}
public function removeChild($name)
{
if ($this->hasChild($name)) {
unset($this->children[$name]);
}
return $this;
}
public function hasChildren()
{
return count($this->getChildren()) ? true : false;
}
public function getChildren()
{
return $this->children;
}
public function isRoot()
{
return $this->getParent() ? false : true;
}
public function getRoot()
{
$category = $this->getParent();
while ($category && !$category->isRoot()) {
$category = $category->getParent();
}
return $category;
}
public function getLevel()
{
return $this->isRoot() ? 0 : $this->getParent()->getLevel() + 1;
}
/**
* Constructor
*/
public function __construct()
{
$this->children = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set slug
*
* @param string $slug
*
* @return Category
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
}