<?php
namespace App\Entity;
use App\Repository\CategoryRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
use phpDocumentor\Reflection\Types\Array_;
use phpDocumentor\Reflection\Types\Integer;
/**
* @ORM\Entity(repositoryClass=CategoryRepository::class)
*/
class Category implements JsonSerializable
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $icon;
/**
* @ORM\Column(name="`order`",type="integer")
*/
private $order;
/**
* @ORM\Column(type="boolean", length=255)
*/
private $showInHomepage;
/**
* @ORM\Column(type="boolean", length=255)
*/
private $showInMenu;
/**
* @ORM\Column(type="boolean", length=255)
*/
private $isActive;
/**
* @ORM\OneToMany(targetEntity=Produit::class, mappedBy="categories")
*/
private $produits;
/**
* @ORM\OneToMany(targetEntity=Category::class, mappedBy="parent")
* @ORM\OrderBy({"order" = "ASC"})
*/
private $subCategories;
/**
* @ORM\ManyToOne(targetEntity=Category::class, inversedBy="subCategories")
*/
private $parent;
public function __construct()
{
$this->produits = new ArrayCollection();
$this->produitsCategory = new ArrayCollection();
$this->parentsCategory = new ArrayCollection();
$this->categories = 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 getIcon(): ?string
{
return $this->icon;
}
public function setIcon(?string $icon): self
{
$this->icon = $icon;
return $this;
}
public function getShowInHomepage(): ?bool
{
return $this->showInHomepage;
}
public function setShowInHomepage(?bool $showInHomepage): self
{
$this->showInHomepage = $showInHomepage;
return $this;
}
public function getShowInMenu(): ?bool
{
return $this->showInMenu;
}
public function setShowInMenu(?bool $showInMenu): self
{
$this->showInMenu = $showInMenu;
return $this;
}
public function getIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(?bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function getOrder(): ?int
{
return $this->order;
}
public function setOrder(int $order): self
{
$this->order = $order;
return $this;
}
/**
* @return Collection|Produit[]
*/
public function getProduits(): Collection
{
return $this->produits->filter(function($element) {
return is_null($element->getDeletedAt());
});
}
/**
* @return Collection|Produit[]
*/
public function getAllProduits(): Collection
{
$products = new ArrayCollection($this->produits->toArray());
foreach ($this->getSubCategories() as $child) {
$childProducts = $child->getAllProduits();
$products->add($childProducts->toArray());
}
return $products;
}
public function addProduit(Produit $produit): self
{
if (!$this->produits->contains($produit)) {
$this->produits[] = $produit;
$produit->setCategories($this);
}
return $this;
}
public function removeProduit(Produit $produit): self
{
if ($this->produits->removeElement($produit)) {
// set the owning side to null (unless already changed)
if ($produit->getCategories() === $this) {
$produit->setCategories(null);
}
}
return $this;
}
/**
* @return Collection|Produit[]
*/
public function getProduitsCategory(): Collection
{
return $this->produitsCategory;
}
public function addProduitsCategory(Produit $produitsCategory): self
{
if (!$this->produitsCategory->contains($produitsCategory)) {
$this->produitsCategory[] = $produitsCategory;
$produitsCategory->setPrincipalCategory($this);
}
return $this;
}
public function removeProduitsCategory(Produit $produitsCategory): self
{
if ($this->produitsCategory->removeElement($produitsCategory)) {
// set the owning side to null (unless already changed)
if ($produitsCategory->getPrincipalCategory() === $this) {
$produitsCategory->setPrincipalCategory(null);
}
}
return $this;
}
/**
* @return self[]
*/
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): ?self
{
$this->parent = $parent;
return $this;
}
public function addParentCategory(self $category): self
{
if (!$this->parent->contains($category)) {
$this->parent[] = $category;
}
return $this;
}
public function removeParentCategory(self $parentCategory): self
{
$this->parentsCategory->removeElement($parentCategory);
return $this;
}
/**
* @return Collection|self[]
*/
public function getSubCategories(): Collection
{
return $this->subCategories;
}
public function jsonSerialize()
{
return array(
'id' => $this->getId(),
'name' => $this->getName(),
);
}
}