src/Entity/Category.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CategoryRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use JsonSerializable;
  8. use phpDocumentor\Reflection\Types\Array_;
  9. use phpDocumentor\Reflection\Types\Integer;
  10. /**
  11.  * @ORM\Entity(repositoryClass=CategoryRepository::class)
  12.  */
  13. class Category implements JsonSerializable
  14. {
  15.     /**
  16.      * @ORM\Id
  17.      * @ORM\GeneratedValue
  18.      * @ORM\Column(type="integer")
  19.      */
  20.     private $id;
  21.     /**
  22.      * @ORM\Column(type="string", length=255)
  23.      */
  24.     private $name;
  25.     /**
  26.      * @ORM\Column(type="string", length=255)
  27.      */
  28.     private $icon;
  29.     /**
  30.      * @ORM\Column(name="`order`",type="integer")
  31.      */
  32.     private $order;
  33.     /**
  34.      * @ORM\Column(type="boolean", length=255)
  35.      */
  36.     private $showInHomepage;
  37.     /**
  38.      * @ORM\Column(type="boolean", length=255)
  39.      */
  40.     private $showInMenu;
  41.     /**
  42.      * @ORM\Column(type="boolean", length=255)
  43.      */
  44.     private $isActive;
  45.     /**
  46.      * @ORM\OneToMany(targetEntity=Produit::class, mappedBy="categories")
  47.      */
  48.     private $produits;
  49.     /**
  50.      * @ORM\OneToMany(targetEntity=Category::class, mappedBy="parent")
  51.      * @ORM\OrderBy({"order" = "ASC"})
  52.      */
  53.     private $subCategories;
  54.     /**
  55.      * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="subCategories")
  56.      */
  57.     private $parent;
  58.     public function __construct()
  59.     {
  60.         $this->produits = new ArrayCollection();
  61.         $this->produitsCategory = new ArrayCollection();
  62.         $this->parentsCategory = new ArrayCollection();
  63.         $this->categories = new ArrayCollection();
  64.     }
  65.     public function getId(): ?int
  66.     {
  67.         return $this->id;
  68.     }
  69.     public function getName(): ?string
  70.     {
  71.         return $this->name;
  72.     }
  73.     public function setName(string $name): self
  74.     {
  75.         $this->name $name;
  76.         return $this;
  77.     }
  78.     public function getIcon(): ?string
  79.     {
  80.         return $this->icon;
  81.     }
  82.     public function setIcon(?string $icon): self
  83.     {
  84.         $this->icon $icon;
  85.         return $this;
  86.     }
  87.     public function getShowInHomepage(): ?bool
  88.     {
  89.         return $this->showInHomepage;
  90.     }
  91.     public function setShowInHomepage(?bool $showInHomepage): self
  92.     {
  93.         $this->showInHomepage $showInHomepage;
  94.         return $this;
  95.     }
  96.     public function getShowInMenu(): ?bool
  97.     {
  98.         return $this->showInMenu;
  99.     }
  100.     public function setShowInMenu(?bool $showInMenu): self
  101.     {
  102.         $this->showInMenu $showInMenu;
  103.         return $this;
  104.     }
  105.     public function getIsActive(): ?bool
  106.     {
  107.         return $this->isActive;
  108.     }
  109.     public function setIsActive(?bool $isActive): self
  110.     {
  111.         $this->isActive $isActive;
  112.         return $this;
  113.     }
  114.     public function getOrder(): ?int
  115.     {
  116.         return $this->order;
  117.     }
  118.     public function setOrder(int $order): self
  119.     {
  120.         $this->order $order;
  121.         return $this;
  122.     }
  123.     /**
  124.      * @return Collection|Produit[]
  125.      */
  126.     public function getProduits(): Collection
  127.     {
  128.         return $this->produits->filter(function($element) {
  129.             return   is_null($element->getDeletedAt());
  130.         });
  131.     }
  132.     /**
  133.      * @return Collection|Produit[]
  134.      */
  135.     public function getAllProduits(): Collection
  136.     {
  137.         $products = new ArrayCollection($this->produits->toArray());
  138.         foreach ($this->getSubCategories() as $child) {
  139.             $childProducts $child->getAllProduits();
  140.             $products->add($childProducts->toArray());
  141.         }
  142.         return $products;
  143.     }
  144.     public function addProduit(Produit $produit): self
  145.     {
  146.         if (!$this->produits->contains($produit)) {
  147.             $this->produits[] = $produit;
  148.             $produit->setCategories($this);
  149.         }
  150.         return $this;
  151.     }
  152.     public function removeProduit(Produit $produit): self
  153.     {
  154.         if ($this->produits->removeElement($produit)) {
  155.             // set the owning side to null (unless already changed)
  156.             if ($produit->getCategories() === $this) {
  157.                 $produit->setCategories(null);
  158.             }
  159.         }
  160.         return $this;
  161.     }
  162.     /**
  163.      * @return Collection|Produit[]
  164.      */
  165.     public function getProduitsCategory(): Collection
  166.     {
  167.         return $this->produitsCategory;
  168.     }
  169.     public function addProduitsCategory(Produit $produitsCategory): self
  170.     {
  171.         if (!$this->produitsCategory->contains($produitsCategory)) {
  172.             $this->produitsCategory[] = $produitsCategory;
  173.             $produitsCategory->setPrincipalCategory($this);
  174.         }
  175.         return $this;
  176.     }
  177.     public function removeProduitsCategory(Produit $produitsCategory): self
  178.     {
  179.         if ($this->produitsCategory->removeElement($produitsCategory)) {
  180.             // set the owning side to null (unless already changed)
  181.             if ($produitsCategory->getPrincipalCategory() === $this) {
  182.                 $produitsCategory->setPrincipalCategory(null);
  183.             }
  184.         }
  185.         return $this;
  186.     }
  187.     /**
  188.      * @return self[]
  189.      */
  190.     public function getParent(): ?self
  191.     {
  192.         return $this->parent;
  193.     }
  194.     public function setParent(?self $parent): ?self
  195.     {
  196.         $this->parent $parent;
  197.         return $this;
  198.     }
  199.     public function addParentCategory(self $category): self
  200.     {
  201.         if (!$this->parent->contains($category)) {
  202.             $this->parent[] = $category;
  203.         }
  204.         return $this;
  205.     }
  206.     public function removeParentCategory(self $parentCategory): self
  207.     {
  208.         $this->parentsCategory->removeElement($parentCategory);
  209.         return $this;
  210.     }
  211.     /**
  212.      * @return Collection|self[]
  213.      */
  214.     public function getSubCategories(): Collection
  215.     {
  216.         return $this->subCategories;
  217.     }
  218.     public function jsonSerialize()
  219.     {
  220.         return array(
  221.             'id' => $this->getId(),
  222.             'name' => $this->getName(),
  223.         );
  224.     }
  225. }