src/Entity/Product.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Collection as ProductCollection;
  4. use App\Traits\CustomSlugify;
  5. use DateTime;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection as DoctrineCollection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Gedmo\Mapping\Annotation as Gedmo;
  10. use JsonSerializable;
  11. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. /**
  14.  * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
  15.  * @UniqueEntity("name")
  16.  */
  17. class Product implements JsonSerializable
  18. {
  19.     use CustomSlugify;
  20.     /**
  21.      * @ORM\Id()
  22.      * @ORM\GeneratedValue()
  23.      * @ORM\Column(type="integer")
  24.      */
  25.     private $id;
  26.     /**
  27.      * @ORM\Column(type="string", length=50)
  28.      * @Assert\Length(min = 4, max = 50, allowEmptyString = false )
  29.      * @Assert\Regex("/[A-Za-z]/")
  30.      */
  31.     private $name;
  32.     /**
  33.      * @Gedmo\Translatable
  34.      * @ORM\Column(type="string", length=512, nullable=true)
  35.      */
  36.     private $description;
  37.     /**
  38.      * @ORM\Column(type="string", length=30, nullable=true)
  39.      */
  40.     private $colorCode;
  41.     /**
  42.      * @ORM\Column(type="boolean")
  43.      */
  44.     private $bestSeller;
  45.     /**
  46.      * @ORM\Column(type="smallint")
  47.      */
  48.     private $orderAppearance;
  49.     /**
  50.      * @ORM\Column(type="string", length=255, nullable=true)
  51.      */
  52.     private $fileArchi;
  53.     /**
  54.      * @ORM\Column(type="string", length=255, nullable=true)
  55.      */
  56.     private $fileRevit;
  57.     /**
  58.      * @ORM\Column(type="boolean")
  59.      */
  60.     private $actif;
  61.     /**
  62.      * @ORM\Column(length=64, unique=true)
  63.      */
  64.     private $slug;
  65.     /**
  66.      * @var DateTime
  67.      * @Gedmo\Timestampable(on="create")
  68.      * @ORM\Column(type="datetime", nullable=true)
  69.      */
  70.     protected $createdAt;
  71.     /**
  72.      * @var DateTime
  73.      * @Gedmo\Timestampable(on="update")
  74.      * @ORM\Column(type="datetime", nullable=true)
  75.      */
  76.     protected $updatedAt;
  77.     /**
  78.      * @ORM\OneToMany(targetEntity="Attachment", mappedBy="product", cascade={"persist", "remove"}, fetch="EAGER")
  79.      */
  80.     private $attachments;
  81.     /**
  82.      * @ORM\ManyToOne(targetEntity="App\Entity\Collection", inversedBy="products")
  83.      * @ORM\JoinColumn(nullable=false)
  84.      */
  85.     private $collection;
  86.     /**
  87.      * @ORM\ManyToMany(targetEntity="Project", inversedBy="products", cascade={"persist"})
  88.      */
  89.     private $projects;
  90.     /**
  91.      * @ORM\ManyToMany(targetEntity="App\Entity\Color", mappedBy="products", fetch="EAGER")
  92.      * @ORM\JoinTable(name="color_product")
  93.      */
  94.     private $colors;
  95.     /**
  96.      * @ORM\ManyToMany(targetEntity=Order::class, mappedBy="products")
  97.      */
  98.     private $orders;
  99.     /**
  100.      * @ORM\Column(type="boolean", nullable=true)
  101.      */
  102.     private $new;
  103.     /**
  104.      * @ORM\Column(type="boolean", nullable=true)
  105.      */
  106.     private $moq;
  107.     /**
  108.      * @ORM\Column(type="boolean", nullable=true)
  109.      */
  110.     private $stock;
  111.     public function __construct()
  112.     {
  113.         $this->attachments = new ArrayCollection();
  114.         $this->projects = new ArrayCollection();
  115.         $this->colors = new ArrayCollection();
  116.         $this->orders = new ArrayCollection();
  117.     }
  118.     public function setTranslatableLocale($locale)
  119.     {
  120.         $this->locale $locale;
  121.     }
  122.     public function __toString()
  123.     {
  124.         return $this->name.'';
  125.     }
  126.     public function jsonSerialize()
  127.     {
  128.         return [
  129.             'id' => $this->getId(),
  130.             'name' => $this->getName(),
  131.             'slug' => $this->getSlug(),
  132.             'description' => $this->getDescription(),
  133.             'orderAppearance' => $this->getOrderAppearance(),
  134.             'colorCode' => $this->getColorCode(),
  135.             'bestSeller' => $this->getBestSeller(),
  136.             'collectionSlug' => $this->getCollection()->getSlug(),
  137.             'fileArchi' => $this->getFileArchi(),
  138.             'fileRevit' => $this->getFileRevit(),
  139.             'colors' => $this->getColors()->getValues(),
  140.             'attachments' => isset($this->attachmentsFile) ? $this->attachmentsFile null,
  141.             'new' => $this->getNew(),
  142.             'moq' => $this->getMoq(),
  143.             'stock' => $this->getStock()
  144.         ];
  145.     }
  146.     public function getId(): ?int
  147.     {
  148.         return $this->id;
  149.     }
  150.     public function getName(): ?string
  151.     {
  152.         return $this->name;
  153.     }
  154.     public function setName(string $name): self
  155.     {
  156.         $this->name ucfirst($name);
  157.         return $this;
  158.     }
  159.     /**
  160.      * @return mixed
  161.      */
  162.     public function getDescription()
  163.     {
  164.         return $this->description;
  165.     }
  166.     /**
  167.      * @param mixed $description
  168.      */
  169.     public function setDescription($description): void
  170.     {
  171.         $this->description $description;
  172.     }
  173.     public function getColorCode(): ?string
  174.     {
  175.         return $this->colorCode;
  176.     }
  177.     public function setColorCode(string $colorCode): self
  178.     {
  179.         $this->colorCode $colorCode;
  180.         return $this;
  181.     }
  182.     public function getBestSeller(): ?bool
  183.     {
  184.         return $this->bestSeller;
  185.     }
  186.     public function setBestSeller(bool $bestSeller): self
  187.     {
  188.         $this->bestSeller $bestSeller;
  189.         return $this;
  190.     }
  191.     public function getOrderAppearance(): ?int
  192.     {
  193.         return $this->orderAppearance;
  194.     }
  195.     public function setOrderAppearance(int $orderAppearance): self
  196.     {
  197.         $this->orderAppearance $orderAppearance;
  198.         return $this;
  199.     }
  200.     public function getFileArchi(): ?string
  201.     {
  202.         return $this->fileArchi;
  203.     }
  204.     public function setFileArchi(?string $fileArchi): self
  205.     {
  206.         $this->fileArchi $fileArchi;
  207.         return $this;
  208.     }
  209.     public function getFileRevit(): ?string
  210.     {
  211.         return $this->fileRevit;
  212.     }
  213.     public function setFileRevit(?string $fileRevit): self
  214.     {
  215.         $this->fileRevit $fileRevit;
  216.         return $this;
  217.     }
  218.     public function getActif(): ?bool
  219.     {
  220.         return $this->actif;
  221.     }
  222.     public function setActif(bool $actif): self
  223.     {
  224.         $this->actif $actif;
  225.         return $this;
  226.     }
  227.     /**
  228.      * Set slug.
  229.      *
  230.      * @param string $slug
  231.      *
  232.      * @return Product
  233.      */
  234.     public function setSlug($slug)
  235.     {
  236.         $this->slug $slug;
  237.         return $this;
  238.     }
  239.     /**
  240.      * Get slug.
  241.      *
  242.      * @return string
  243.      */
  244.     public function getSlug()
  245.     {
  246.         return $this->slug;
  247.     }
  248.     /**
  249.      * @ORM\PrePersist
  250.      * @ORM\PreUpdate
  251.      */
  252.     public function updateSlug()
  253.     {
  254.         $this->slug $this->slugify($this->name);
  255.     }
  256.     /**
  257.      * @ORM\PrePersist
  258.      */
  259.     public function initOrderApparearance()
  260.     {
  261.         $this->orderAppearance 0;
  262.         $this->createdAt = new DateTime();
  263.         $this->updatedAt = new DateTime();
  264.     }
  265.     public function getCreatedAt(): ?\DateTimeInterface
  266.     {
  267.         return $this->createdAt;
  268.     }
  269.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  270.     {
  271.         $this->createdAt $createdAt;
  272.         return $this;
  273.     }
  274.     public function getUpdatedAt(): ?\DateTimeInterface
  275.     {
  276.         return $this->updatedAt;
  277.     }
  278.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  279.     {
  280.         $this->updatedAt $updatedAt;
  281.         return $this;
  282.     }
  283.     /**
  284.      * @return DoctrineCollection|Attachment[]
  285.      */
  286.     public function getAttachments(): DoctrineCollection
  287.     {
  288.         return $this->attachments;
  289.     }
  290.     public function addAttachment(Attachment $attachment): self
  291.     {
  292.         if (!$this->attachments->contains($attachment)) {
  293.             $this->attachments[] = $attachment;
  294.             $attachment->setProduct($this);
  295.         }
  296.         return $this;
  297.     }
  298.     public function removeAttachment(Attachment $attachment): self
  299.     {
  300.         if ($this->attachments->contains($attachment)) {
  301.             $this->attachments->removeElement($attachment);
  302.             // set the owning side to null (unless already changed)
  303.             if ($attachment->getProduct() === $this) {
  304.                 $attachment->setProduct(null);
  305.             }
  306.         }
  307.         return $this;
  308.     }
  309.     public function getCollection(): ?ProductCollection
  310.     {
  311.         return $this->collection;
  312.     }
  313.     public function setCollection(?ProductCollection $collection): self
  314.     {
  315.         $this->collection $collection;
  316.         return $this;
  317.     }
  318.     /**
  319.      * @return DoctrineCollection|Project[]
  320.      */
  321.     public function getProjects(): DoctrineCollection
  322.     {
  323.         return $this->projects;
  324.     }
  325.     public function addProject(Project $project): self
  326.     {
  327.         if (!$this->projects->contains($project)) {
  328.             $this->projects[] = $project;
  329.         }
  330.         return $this;
  331.     }
  332.     public function removeProject(Project $project): self
  333.     {
  334.         if ($this->projects->contains($project)) {
  335.             $this->projects->removeElement($project);
  336.         }
  337.         return $this;
  338.     }
  339.     /**
  340.      * @return DoctrineCollection|Color[]
  341.      */
  342.     public function getColors(): DoctrineCollection
  343.     {
  344.         return $this->colors;
  345.     }
  346.     public function addColor(Color $color): self
  347.     {
  348.         if (!$this->colors->contains($color)) {
  349.             $this->colors[] = $color;
  350.             $color->addProduct($this);
  351.         }
  352.         return $this;
  353.     }
  354.     public function removeColor(Color $color): self
  355.     {
  356.         if ($this->colors->contains($color)) {
  357.             $this->colors->removeElement($color);
  358.             $color->removeProduct($this);
  359.         }
  360.         return $this;
  361.     }
  362.     /**
  363.      * @return DoctrineCollection|Order[]
  364.      */
  365.     public function getOrders(): DoctrineCollection
  366.     {
  367.         return $this->orders;
  368.     }
  369.     public function addOrder(Order $order): self
  370.     {
  371.         if (!$this->orders->contains($order)) {
  372.             $this->orders[] = $order;
  373.             $order->addProduct($this);
  374.         }
  375.         return $this;
  376.     }
  377.     public function removeOrder(Order $order): self
  378.     {
  379.         if ($this->orders->contains($order)) {
  380.             $this->orders->removeElement($order);
  381.             $order->removeProduct($this);
  382.         }
  383.         return $this;
  384.     }
  385.     public function getNew(): ?bool
  386.     {
  387.         return $this->new;
  388.     }
  389.     public function setNew(?bool $new): self
  390.     {
  391.         $this->new $new;
  392.         return $this;
  393.     }
  394.     public function getMoq(): ?bool
  395.     {
  396.         return $this->moq;
  397.     }
  398.     public function setMoq(?bool $moq): self
  399.     {
  400.         $this->moq $moq;
  401.         return $this;
  402.     }
  403.     public function getStock(): ?bool
  404.     {
  405.         return $this->stock;
  406.     }
  407.     public function setStock(?bool $stock): self
  408.     {
  409.         $this->stock $stock;
  410.         return $this;
  411.     }
  412. }