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