<?php
namespace App\Entity;
use App\Repository\ProductRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass=ProductRepository::class)
*
* @ORM\HasLifecycleCallbacks()
*
* @Vich\Uploadable
*/
class Product {
use MetaTrait;
/**
* @ORM\Id
*
* @ORM\GeneratedValue
*
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Tenant::class, inversedBy="products")
*/
private $tenant;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="text")
*/
private $description;
/**
* @ORM\Column(type="string", length=255)
*/
private $shortDescription;
/**
* @ORM\Column(type="json")
*/
private $config = [];
/**
* @ORM\Column(type="decimal", precision=6, scale=2, nullable=true)
*/
private $price;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $listImage;
/**
* @Vich\UploadableField(mapping="images", fileNameProperty="listImage")
*/
private $listImageFile;
/**
* @ORM\OneToMany(
* targetEntity=Image::class,
* mappedBy="product",
* cascade={"persist", "remove"},
* orphanRemoval=true
* )
*/
private $images;
/**
* @var string
*/
private $configEditor;
/**
* @ORM\ManyToMany(targetEntity=Category::class, mappedBy="products")
*/
private $categories;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $recurringPrice;
/**
* @ORM\Column(type="boolean")
*/
private $needsConfirmation;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $maxQuantity;
public function __construct() {
$this->images = new ArrayCollection();
$this->categories = new ArrayCollection();
}
public function __toString(): string {
return $this->getName();
}
public function getId(): int {
return $this->id;
}
public function getTenant(): ?Tenant {
return $this->tenant;
}
public function setTenant(?Tenant $tenant): self {
$this->tenant = $tenant;
return $this;
}
public function getName(): ?string {
return $this->name;
}
public function setName(string $name): self {
$this->name = $name;
return $this;
}
public function getDescription(): ?string {
return $this->description;
}
public function setDescription(string $description): self {
$this->description = $description;
return $this;
}
public function getShortDescription(): ?string {
return $this->shortDescription;
}
public function setShortDescription(string $shortDescription): self {
$this->shortDescription = $shortDescription;
return $this;
}
/**
* Returns the config array as a json encoded string because the admin
* configuration does not support array to string conversion yet.
* I will fix this with my own template but for now it is working fine.
*
* @return string|null
*/
public function getConfig(): ?string {
return json_encode($this->config);
}
/**
* Accepts a mixed type because the config editor returns a json encoded string
* so the setter needs to convert it to an array.
*
* @param mixed $config
*
* @return self
*/
public function setConfig(mixed $config): self {
if (is_string($config)) {
$config = json_decode($config, true);
}
$this->config = $config;
return $this;
}
public function getPrice(): ?string {
return $this->price;
}
public function setPrice(?string $price): self {
$this->price = $price;
return $this;
}
public function getConfigEditor(): ?string {
return $this->configEditor;
}
public function setConfigEditor(?string $configEditor): self {
$this->configEditor = $configEditor;
return $this;
}
public function getListImage(): ?string {
return $this->listImage;
}
public function setListImage(?string $listImage): self {
$this->listImage = $listImage;
return $this;
}
public function getListImageFile(): ?File {
return $this->listImageFile;
}
public function setListImageFile(?File $listImageFile): self {
$this->listImageFile = $listImageFile;
if ($this->listImageFile instanceof File) {
$this->modifiedAt = new \DateTime();
}
return $this;
}
/**
* @return Collection|Image[]
*/
public function getImages(): Collection {
return $this->images;
}
public function addImage(Image $image): self {
if (!$this->images->contains($image)) {
$this->images[] = $image;
$image->setProduct($this);
}
return $this;
}
public function removeImage(Image $image): self {
if ($this->images->removeElement($image)) {
// set the owning side to null (unless already changed)
if ($image->getProduct() === $this) {
$image->setProduct(null);
}
}
return $this;
}
/**
* @ORM\PrePersist
*/
public function setDefaults(): self {
return $this
->setCreatedAt(new \DateTime())
;
}
/**
* @ORM\PreUpdate
*/
public function setModified(): self {
return $this
->setModifiedAt(new \DateTime())
;
}
/**
* @return Collection<int, Category>
*/
public function getCategories(): Collection {
return $this->categories;
}
public function addCategory(Category $category): self {
if (!$this->categories->contains($category)) {
$this->categories[] = $category;
$category->addProduct($this);
}
return $this;
}
public function removeCategory(Category $category): self {
if ($this->categories->removeElement($category)) {
$category->removeProduct($this);
}
return $this;
}
public function getRecurringPrice(): ?float {
return $this->recurringPrice;
}
public function setRecurringPrice(?float $recurringPrice): self {
$this->recurringPrice = $recurringPrice;
return $this;
}
public function getNeedsConfirmation(): ?bool {
return $this->needsConfirmation;
}
public function setNeedsConfirmation(bool $needsConfirmation): self {
$this->needsConfirmation = $needsConfirmation;
return $this;
}
public function getMaxQuantity(): ?int {
return $this->maxQuantity;
}
public function setMaxQuantity(?int $maxQuantity): self {
$this->maxQuantity = $maxQuantity;
return $this;
}
}