src/Entity/Product.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ProductRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\HttpFoundation\File\File;
  8. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  9. /**
  10.  * @ORM\Entity(repositoryClass=ProductRepository::class)
  11.  *
  12.  * @ORM\HasLifecycleCallbacks()
  13.  *
  14.  * @Vich\Uploadable
  15.  */
  16. class Product {
  17.     use MetaTrait;
  18.     /**
  19.      * @ORM\Id
  20.      *
  21.      * @ORM\GeneratedValue
  22.      *
  23.      * @ORM\Column(type="integer")
  24.      */
  25.     private $id;
  26.     /**
  27.      * @ORM\ManyToOne(targetEntity=Tenant::class, inversedBy="products")
  28.      */
  29.     private $tenant;
  30.     /**
  31.      * @ORM\Column(type="string", length=255)
  32.      */
  33.     private $name;
  34.     /**
  35.      * @ORM\Column(type="text")
  36.      */
  37.     private $description;
  38.     /**
  39.      * @ORM\Column(type="string", length=255)
  40.      */
  41.     private $shortDescription;
  42.     /**
  43.      * @ORM\Column(type="json")
  44.      */
  45.     private $config = [];
  46.     /**
  47.      * @ORM\Column(type="decimal", precision=6, scale=2, nullable=true)
  48.      */
  49.     private $price;
  50.     /**
  51.      * @ORM\Column(type="string", length=255, nullable=true)
  52.      */
  53.     private $listImage;
  54.     /**
  55.      * @Vich\UploadableField(mapping="images", fileNameProperty="listImage")
  56.      */
  57.     private $listImageFile;
  58.     /**
  59.      * @ORM\OneToMany(
  60.      *     targetEntity=Image::class,
  61.      *     mappedBy="product",
  62.      *     cascade={"persist", "remove"},
  63.      *     orphanRemoval=true
  64.      * )
  65.      */
  66.     private $images;
  67.     /**
  68.      * @var string
  69.      */
  70.     private $configEditor;
  71.     /**
  72.      * @ORM\ManyToMany(targetEntity=Category::class, mappedBy="products")
  73.      */
  74.     private $categories;
  75.     /**
  76.      * @ORM\Column(type="float", nullable=true)
  77.      */
  78.     private $recurringPrice;
  79.     /**
  80.      * @ORM\Column(type="boolean")
  81.      */
  82.     private $needsConfirmation;
  83.     /**
  84.      * @ORM\Column(type="integer", nullable=true)
  85.      */
  86.     private $maxQuantity;
  87.     public function __construct() {
  88.         $this->images = new ArrayCollection();
  89.         $this->categories = new ArrayCollection();
  90.     }
  91.     public function __toString(): string {
  92.         return $this->getName();
  93.     }
  94.     public function getId(): int {
  95.         return $this->id;
  96.     }
  97.     public function getTenant(): ?Tenant {
  98.         return $this->tenant;
  99.     }
  100.     public function setTenant(?Tenant $tenant): self {
  101.         $this->tenant $tenant;
  102.         return $this;
  103.     }
  104.     public function getName(): ?string {
  105.         return $this->name;
  106.     }
  107.     public function setName(string $name): self {
  108.         $this->name $name;
  109.         return $this;
  110.     }
  111.     public function getDescription(): ?string {
  112.         return $this->description;
  113.     }
  114.     public function setDescription(string $description): self {
  115.         $this->description $description;
  116.         return $this;
  117.     }
  118.     public function getShortDescription(): ?string {
  119.         return $this->shortDescription;
  120.     }
  121.     public function setShortDescription(string $shortDescription): self {
  122.         $this->shortDescription $shortDescription;
  123.         return $this;
  124.     }
  125.     /**
  126.      * Returns the config array as a json encoded string because the admin
  127.      * configuration does not support array to string conversion yet.
  128.      * I will fix this with my own template but for now it is working fine.
  129.      *
  130.      * @return string|null
  131.      */
  132.     public function getConfig(): ?string {
  133.         return json_encode($this->config);
  134.     }
  135.     /**
  136.      * Accepts a mixed type because the config editor returns a json encoded string
  137.      * so the setter needs to convert it to an array.
  138.      *
  139.      * @param mixed $config
  140.      *
  141.      * @return self
  142.      */
  143.     public function setConfig(mixed $config): self {
  144.         if (is_string($config)) {
  145.             $config json_decode($configtrue);
  146.         }
  147.         $this->config $config;
  148.         return $this;
  149.     }
  150.     public function getPrice(): ?string {
  151.         return $this->price;
  152.     }
  153.     public function setPrice(?string $price): self {
  154.         $this->price $price;
  155.         return $this;
  156.     }
  157.     public function getConfigEditor(): ?string {
  158.         return $this->configEditor;
  159.     }
  160.     public function setConfigEditor(?string $configEditor): self {
  161.         $this->configEditor $configEditor;
  162.         return $this;
  163.     }
  164.     public function getListImage(): ?string {
  165.         return $this->listImage;
  166.     }
  167.     public function setListImage(?string $listImage): self {
  168.         $this->listImage $listImage;
  169.         return $this;
  170.     }
  171.     public function getListImageFile(): ?File {
  172.         return $this->listImageFile;
  173.     }
  174.     public function setListImageFile(?File $listImageFile): self {
  175.         $this->listImageFile $listImageFile;
  176.         if ($this->listImageFile instanceof File) {
  177.             $this->modifiedAt = new \DateTime();
  178.         }
  179.         return $this;
  180.     }
  181.     /**
  182.      * @return Collection|Image[]
  183.      */
  184.     public function getImages(): Collection {
  185.         return $this->images;
  186.     }
  187.     public function addImage(Image $image): self {
  188.         if (!$this->images->contains($image)) {
  189.             $this->images[] = $image;
  190.             $image->setProduct($this);
  191.         }
  192.         return $this;
  193.     }
  194.     public function removeImage(Image $image): self {
  195.         if ($this->images->removeElement($image)) {
  196.             // set the owning side to null (unless already changed)
  197.             if ($image->getProduct() === $this) {
  198.                 $image->setProduct(null);
  199.             }
  200.         }
  201.         return $this;
  202.     }
  203.     /**
  204.      * @ORM\PrePersist
  205.      */
  206.     public function setDefaults(): self {
  207.         return $this
  208.             ->setCreatedAt(new \DateTime())
  209.         ;
  210.     }
  211.     /**
  212.      * @ORM\PreUpdate
  213.      */
  214.     public function setModified(): self {
  215.         return $this
  216.             ->setModifiedAt(new \DateTime())
  217.         ;
  218.     }
  219.     /**
  220.      * @return Collection<int, Category>
  221.      */
  222.     public function getCategories(): Collection {
  223.         return $this->categories;
  224.     }
  225.     public function addCategory(Category $category): self {
  226.         if (!$this->categories->contains($category)) {
  227.             $this->categories[] = $category;
  228.             $category->addProduct($this);
  229.         }
  230.         return $this;
  231.     }
  232.     public function removeCategory(Category $category): self {
  233.         if ($this->categories->removeElement($category)) {
  234.             $category->removeProduct($this);
  235.         }
  236.         return $this;
  237.     }
  238.     public function getRecurringPrice(): ?float {
  239.         return $this->recurringPrice;
  240.     }
  241.     public function setRecurringPrice(?float $recurringPrice): self {
  242.         $this->recurringPrice $recurringPrice;
  243.         return $this;
  244.     }
  245.     public function getNeedsConfirmation(): ?bool {
  246.         return $this->needsConfirmation;
  247.     }
  248.     public function setNeedsConfirmation(bool $needsConfirmation): self {
  249.         $this->needsConfirmation $needsConfirmation;
  250.         return $this;
  251.     }
  252.     public function getMaxQuantity(): ?int {
  253.         return $this->maxQuantity;
  254.     }
  255.     public function setMaxQuantity(?int $maxQuantity): self {
  256.         $this->maxQuantity $maxQuantity;
  257.         return $this;
  258.     }
  259. }