<?php
namespace App\Entity;
use App\Repository\CategoryRepository;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Gedmo\Tree(type="nested")
* @ORM\Entity(repositoryClass=CategoryRepository::class)
*/
class Category
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(
* min=4,
* max=50,
* maxMessage="Describe your category in 20 chars or less"
* )
* @Assert\NotBlank()
*/
private $name;
/**
* @Gedmo\TreeLeft
* @ORM\Column(type="integer")
*/
private $lft;
/**
* @Gedmo\TreeRight
* @ORM\Column(type="integer")
*/
private $rgt;
/**
* @Gedmo\TreeParent
* @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $parent;
/**
* @Gedmo\TreeRoot
* @ORM\Column(type="integer", nullable=true)
*/
private $root;
/**
* @Gedmo\TreeLevel
* @ORM\Column(name="lvl", type="integer")
*/
private $level;
/**
* @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
*/
private $children;
/**
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private $created_at;
/**
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
*/
private $updated_at;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="categories")
* @ORM\JoinColumn(nullable=false)
*/
private $created_by;
/**
* @ORM\ManyToMany(targetEntity=Post::class, mappedBy="categories")
*/
private $posts;
/**
* @ORM\OneToMany(targetEntity=Product::class, mappedBy="category")
*/
private $products = [];
/**
* @ORM\Column(type="json")
*/
private $content = [];
/**
* @Gedmo\Slug(fields={"name"})
* @ORM\Column(type="string", length=255)
*/
private $slug;
public function __construct()
{
$this->posts = new ArrayCollection();
$this->products = new ArrayCollection();
}
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 getCreatedAt(): ?DateTimeInterface
{
return $this->created_at;
}
public function getUpdatedAt(): ?DateTimeInterface
{
return $this->updated_at;
}
public function setParent($parent)
{
$this->parent = $parent;
return $this;
}
public function getParent()
{
return $this->parent;
}
public function getRoot()
{
return $this->root;
}
public function getLevel()
{
return $this->level;
}
public function getChildren()
{
return $this->children;
}
public function getLeft()
{
return $this->lft;
}
public function getRight()
{
return $this->rgt;
}
public function getCreatedBy(): ?User
{
return $this->created_by;
}
public function setCreatedBy(?User $owner): self
{
$this->created_by = $owner;
return $this;
}
/**
* @return Collection|Post[]
*/
public function getPosts(): Collection
{
return $this->posts;
}
public function addPost(Post $post): self
{
if (!$this->posts->contains($post)) {
$this->posts[] = $post;
$post->addCategory($this);
}
return $this;
}
public function removePost(Post $post): self
{
if ($this->posts->removeElement($post)) {
$post->removeCategory($this);
}
return $this;
}
public function __toString()
{
return $this->getName() ?? '';
}
/**
* @return Collection|Product[]
*/
public function getProducts(): Collection
{
return $this->products;
}
public function addProduct(Product $product): self
{
if (!$this->products->contains($product)) {
$this->products[] = $product;
$product->setCategory($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->getCategory() === $this) {
$product->setCategory(null);
}
}
return $this;
}
public function getContent(): ?array
{
return $this->content;
}
public function setContent(array $content): self
{
$this->content = $content;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
}