src/Entity/Category.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Validator\Constraints as AppAssert;
  4. use App\Entity\Product\ConfirmedProduct;
  5. use DateTime;
  6. use DateTimeImmutable;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Exception;
  10. use Gedmo\Mapping\Annotation as Gedmo;
  11. use JMS\Serializer\Annotation\Groups;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  14. /**
  15.  * Class Category
  16.  *
  17.  * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
  18.  * @ORM\Table(name="malys_category")
  19.  * @ORM\HasLifecycleCallbacks()
  20.  * @AppAssert\Category
  21.  * @Vich\Uploadable
  22.  * @package App\Entity
  23.  */
  24. class Category
  25. {
  26.     /**
  27.      * @var integer
  28.      *
  29.      * @ORM\Column(name="id", type="integer")
  30.      * @ORM\Id
  31.      * @ORM\GeneratedValue(strategy="AUTO")
  32.      * @Groups({"api","apicategory"})
  33.      */
  34.     protected $id;
  35.     /**
  36.      * @var string
  37.      *
  38.      * @ORM\Column(name="label", type="string", length=255, nullable=false)
  39.      * @Assert\NotBlank
  40.      */
  41.     protected $label;
  42.     protected $stemmedLabel// used in elasticsearch
  43.     /**
  44.      * @var string
  45.      *
  46.      * @ORM\Column(name="description", type="text", nullable=false)
  47.      * @Assert\NotBlank
  48.      * @Groups({"datatable"})
  49.      */
  50.     protected $description;
  51.     /**
  52.      * @var ArrayCollection
  53.      *
  54.      * @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
  55.      * @Groups({"apicategory"})
  56.      */
  57.     protected $children;
  58.     /**
  59.      * @var Category
  60.      *
  61.      * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
  62.      * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
  63.      */
  64.     protected $parent;
  65.     /**
  66.      * @var string
  67.      *
  68.      * @ORM\Column(name="picture", type="string", nullable=true)
  69.      */
  70.     protected $picture;
  71.     /**
  72.      * Not persisted
  73.      * @Vich\UploadableField(mapping="category_picture", fileNameProperty="picture")
  74.      */
  75.     private $pictureFile;
  76.     /**
  77.      * @var ArrayCollection
  78.      * One Category has Many Products.
  79.      * @ORM\OneToMany(targetEntity="App\Entity\Product\ConfirmedProduct", mappedBy="category")
  80.      */
  81.     protected $products;
  82.     /**
  83.      * @var DateTime
  84.      *
  85.      * @ORM\Column(name="createdAt", type="datetime", nullable=false)
  86.      */
  87.     protected $createdAt;
  88.     /**
  89.      * @var DateTime
  90.      *
  91.      * @ORM\Column(name="updatedAt", type="datetime", nullable=true)
  92.      */
  93.     protected $updatedAt;
  94.     /**
  95.      * @Gedmo\Slug(fields={"label"})
  96.      * @ORM\Column(length=128, unique=true)
  97.      */
  98.     protected $slug;
  99.     /**
  100.      * @var string
  101.      *
  102.      * @ORM\Column(name="color", type="string", length=255, nullable=true)
  103.      */
  104.     protected $color;
  105.     public function __toString()
  106.     {
  107.         return $this->getLabel();
  108.     }
  109.     /**
  110.      * Category constructor.
  111.      */
  112.     public function __construct()
  113.     {
  114.         $this->children = new ArrayCollection();
  115.         $this->products = new ArrayCollection();
  116.     }
  117.     /**
  118.      * @return int
  119.      */
  120.     public function getId()
  121.     {
  122.         return $this->id;
  123.     }
  124.     /**
  125.      * @param int $id
  126.      */
  127.     public function setId($id)
  128.     {
  129.         $this->id $id;
  130.     }
  131.     /**
  132.      * @return string
  133.      */
  134.     public function getLabel()
  135.     {
  136.         return $this->label;
  137.     }
  138.     /**
  139.      * @param string $label
  140.      */
  141.     public function setLabel($label)
  142.     {
  143.         $this->label $label;
  144.     }
  145.     /**
  146.      * @return string
  147.      */
  148.     public function getDescription()
  149.     {
  150.         return $this->description;
  151.     }
  152.     /**
  153.      * @param string $description
  154.      */
  155.     public function setDescription($description)
  156.     {
  157.         $this->description $description;
  158.     }
  159.     /**
  160.      * @return ArrayCollection
  161.      */
  162.     public function getChildren()
  163.     {
  164.         return $this->children;
  165.     }
  166.     /**
  167.      * @param ArrayCollection $children
  168.      */
  169.     public function setChildren($children)
  170.     {
  171.         $this->children $children;
  172.     }
  173.     /**
  174.      * @return Category
  175.      */
  176.     public function getParent()
  177.     {
  178.         return $this->parent;
  179.     }
  180.     /**
  181.      * @param Category $parent
  182.      */
  183.     public function setParent($parent)
  184.     {
  185.         $this->parent $parent;
  186.     }
  187.     /**
  188.      * @return string
  189.      */
  190.     public function getPicture()
  191.     {
  192.         return $this->picture;
  193.     }
  194.     /**
  195.      * @param string $picture
  196.      */
  197.     public function setPicture($picture)
  198.     {
  199.         $this->picture $picture;
  200.     }
  201.     public function setPictureFile($pictureFile null)
  202.     {
  203.         $this->pictureFile $pictureFile;
  204.         if (null !== $pictureFile) {
  205.             $this->updatedAt = new DateTimeImmutable();
  206.         }
  207.     }
  208.     public function getPictureFile()
  209.     {
  210.         return $this->pictureFile;
  211.     }
  212.     /**
  213.      * @return ArrayCollection
  214.      */
  215.     public function getProducts()
  216.     {
  217.         return $this->products;
  218.     }
  219.     /**
  220.      * @param ArrayCollection $products
  221.      */
  222.     public function setProducts($products)
  223.     {
  224.         $this->products $products;
  225.     }
  226.     /**
  227.      * @param ConfirmedProduct $product
  228.      */
  229.     public function addProduct($product)
  230.     {
  231.         $this->products->add($product);
  232.     }
  233.     /**
  234.      * @param ConfirmedProduct $product
  235.      */
  236.     public function removeProduct($product)
  237.     {
  238.         if ($this->products->contains($product)) {
  239.             $this->products->removeElement($product);
  240.         }
  241.     }
  242.     /**
  243.      * @return DateTime
  244.      */
  245.     public function getCreatedAt()
  246.     {
  247.         return $this->createdAt;
  248.     }
  249.     /**
  250.      * Set createdAt
  251.      *
  252.      * @return Category
  253.      * @ORM\PrePersist
  254.      * @throws Exception
  255.      */
  256.     public function setCreatedAt()
  257.     {
  258.         $this->createdAt = new DateTime();
  259.         $this->updatedAt = new DateTime();
  260.         return $this;
  261.     }
  262.     /**
  263.      * @return DateTime
  264.      */
  265.     public function getUpdatedAt()
  266.     {
  267.         return $this->updatedAt;
  268.     }
  269.     /**
  270.      * Set updatedAt
  271.      *
  272.      * @return Category
  273.      * @ORM\PreUpdate
  274.      * @throws Exception
  275.      */
  276.     public function setUpdatedAt()
  277.     {
  278.         $this->updatedAt = new DateTime();
  279.         return $this;
  280.     }
  281.     /**
  282.      * @return string
  283.      */
  284.     public function getCustom()
  285.     {
  286.         return
  287.             $this->id .'||'.
  288.             $this->label .'||'.
  289.             $this->picture .'||'.
  290.             $this->slug;
  291.     }
  292.     /**
  293.      * @return mixed
  294.      */
  295.     public function getSlug()
  296.     {
  297.         return $this->slug;
  298.     }
  299.     /**
  300.      * @param mixed $slug
  301.      */
  302.     public function setSlug($slug)
  303.     {
  304.         $this->slug $slug;
  305.     }
  306.     /**
  307.      * @return mixed
  308.      */
  309.     public function getStemmedLabel()
  310.     {
  311.         return $this->label;
  312.     }
  313.     /**
  314.      * @return string
  315.      */
  316.     public function getColor()
  317.     {
  318.         return $this->color;
  319.     }
  320.     /**
  321.      * @param string $color
  322.      */
  323.     public function setColor($color)
  324.     {
  325.         $this->color $color;
  326.     }
  327.     public function getCategorieLabel(): ?string
  328.     {
  329.         return $this->parent $this->parent->getLabel() : null;
  330.     }
  331. }