<?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 Gedmo\Mapping\Annotation as Gedmo;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Vich\Uploadable
* @ORM\Entity(repositoryClass=ProductRepository::class)
*/
class Product implements TranslatableInterface
{
use TranslatableTrait;
public function __call($method, $arguments)
{
return $this->proxyCurrentLocaleTranslation($method, $arguments);
}
public function __get($name)
{
$method = 'get' . ucfirst($name);
$arguments = [];
return $this->proxyCurrentLocaleTranslation($method, $arguments);
}
public function __toString()
{
return "#" . $this->id . " " . $this->getTitle();
}
/**
* @ORM\OneToMany(targetEntity=ApplicationProduct::class, mappedBy="product")
*/
private $applicationProducts;
/**
* @ORM\OneToMany(targetEntity=RelatedProduct::class, mappedBy="product",cascade={"persist"})
*/
private $relatedProducts;
/**
* @ORM\OneToMany(targetEntity=RelatedProduct::class, mappedBy="targetProduct",cascade={"persist"})
*/
private $relatedTargetProducts;
public function __construct()
{
$this->applicationProducts = new ArrayCollection();
$this->relatedProducts = new ArrayCollection();
$this->relatedTargetProducts = new ArrayCollection();
$this->productMedia = new ArrayCollection();
}
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @Gedmo\SortablePosition()
* @ORM\Column(type="integer", nullable=true)
*/
private $position;
/**
* @Gedmo\SortableGroup()
* @Assert\NotBlank()
* @ORM\ManyToOne(targetEntity=Category::class, inversedBy="products")
*/
private $category;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
* @Vich\UploadableField(mapping="product", fileNameProperty="productFileName", size="productFileSize")
* @Assert\Expression("this.getProductFile() or this.getProductFileName()", message = "Veuillez sélectionner un fichier")
* @Assert\File(
* maxSize = "1200k"
* )
*
* @var File
*/
private $productFile;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @var string
*/
private $productFileName;
/**
* @ORM\Column(type="integer", nullable=true)
*
* @var integer
*/
private $productFileSize;
/**
* @ORM\Column(type="datetime", nullable=true)
*
* @var \DateTime
*/
private $productFileUpdatedAt;
/**
* @Assert\NotBlank()
* @ORM\ManyToOne(targetEntity=SubCategory::class, inversedBy="products")
*/
private $subCategory;
/**
* @ORM\Column(type="boolean")
*/
private $hide = false;
/**
* @ORM\OneToMany(targetEntity=ProductMedia::class, mappedBy="product", cascade={"remove"})
*/
private $productMedia;
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
*/
public function setProductFile(File $image = null)
{
$this->productFile = $image;
if (null !== $image) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->productFileUpdatedAt = new \DateTimeImmutable();
}
}
public function getProductFile()
{
return $this->productFile;
}
public function getProductFileName(): ?string
{
return $this->productFileName;
}
public function setProductFileName(?string $productFileName): self
{
$this->productFileName = $productFileName;
return $this;
}
public function getProductFileSize(): ?int
{
return $this->productFileSize;
}
public function setProductFileSize(?int $productFileSize): self
{
$this->productFileSize = $productFileSize;
return $this;
}
public function getProductFileUpdatedAt(): ?\DateTimeInterface
{
return $this->productFileUpdatedAt;
}
public function setProductFileUpdatedAt(?\DateTimeInterface $productFileUpdatedAt): self
{
$this->productFileUpdatedAt = $productFileUpdatedAt;
return $this;
}
public function getId(): ?int
{
return $this->id;
}
public function getPosition(): ?int
{
return $this->position;
}
public function setPosition(?int $position): self
{
$this->position = $position;
return $this;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): self
{
$this->category = $category;
return $this;
}
public function getSubCategory(): ?SubCategory
{
return $this->subCategory;
}
public function setSubCategory(?SubCategory $subCategory): self
{
$this->subCategory = $subCategory;
return $this;
}
/**
* @return Collection<int, ApplicationProduct>
*/
public function getApplicationProducts(): Collection
{
return $this->applicationProducts;
}
public function addApplicationProduct(ApplicationProduct $applicationProduct): self
{
if (!$this->applicationProducts->contains($applicationProduct)) {
$this->applicationProducts[] = $applicationProduct;
$applicationProduct->setProduct($this);
}
return $this;
}
public function removeApplicationProduct(ApplicationProduct $applicationProduct): self
{
if ($this->applicationProducts->removeElement($applicationProduct)) {
// set the owning side to null (unless already changed)
if ($applicationProduct->getProduct() === $this) {
$applicationProduct->setProduct(null);
}
}
return $this;
}
/**
* @return Collection<int, RelatedProduct>
*/
public function getRelatedProducts(): Collection
{
return $this->relatedProducts;
}
public function addRelatedProduct(RelatedProduct $relatedProduct): self
{
if (!$this->relatedProducts->contains($relatedProduct)) {
$this->relatedProducts[] = $relatedProduct;
$relatedProduct->setProduct($this);
}
return $this;
}
public function removeRelatedProduct(RelatedProduct $relatedProduct): self
{
if ($this->relatedProducts->removeElement($relatedProduct)) {
// set the owning side to null (unless already changed)
if ($relatedProduct->getProduct() === $this) {
$relatedProduct->setProduct(null);
}
}
return $this;
}
public function removeAllRelatedProduct(): self
{
foreach ($this->relatedProducts as $relatedProduct) {
if ($this->relatedProducts->removeElement($relatedProduct)) {
// set the owning side to null (unless already changed)
if ($relatedProduct->getProduct() === $this) {
$relatedProduct->setProduct(null);
}
}
}
return $this;
}
public function removeAllRelatedTargetProduct(): self
{
foreach ($this->relatedTargetProducts as $relatedTargetProduct) {
if ($this->relatedTargetProducts->removeElement($relatedTargetProduct)) {
// set the owning side to null (unless already changed)
if ($relatedTargetProduct->getTargetProduct() === $this) {
$relatedTargetProduct->setTargetProduct(null);
}
}
}
return $this;
}
/**
* @return Collection<int, RelatedProduct>
*/
public function getRelatedTargetProducts(): Collection
{
return $this->relatedTargetProducts;
}
public function addRelatedTargetProduct(RelatedProduct $relatedTargetProduct): self
{
if (!$this->relatedTargetProducts->contains($relatedTargetProduct)) {
$this->relatedTargetProducts[] = $relatedTargetProduct;
$relatedTargetProduct->setTargetProduct($this);
}
return $this;
}
public function removeRelatedTargetProduct(RelatedProduct $relatedTargetProduct): self
{
if ($this->relatedTargetProducts->removeElement($relatedTargetProduct)) {
// set the owning side to null (unless already changed)
if ($relatedTargetProduct->getTargetProduct() === $this) {
$relatedTargetProduct->setTargetProduct(null);
}
}
return $this;
}
public function getHide(): ?bool
{
return $this->hide;
}
public function setHide(bool $hide): self
{
$this->hide = $hide;
return $this;
}
/**
* @return Collection<int, ProductMedia>
*/
public function getProductMedia(): Collection
{
return $this->productMedia;
}
public function addProductMedium(ProductMedia $productMedium): self
{
if (!$this->productMedia->contains($productMedium)) {
$this->productMedia[] = $productMedium;
$productMedium->setProduct($this);
}
return $this;
}
public function removeProductMedium(ProductMedia $productMedium): self
{
if ($this->productMedia->removeElement($productMedium)) {
// set the owning side to null (unless already changed)
if ($productMedium->getProduct() === $this) {
$productMedium->setProduct(null);
}
}
return $this;
}
}