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