<?php
namespace App\Entity;
use App\Entity\Collection as ProductCollection;
use App\Traits\CustomSlugify;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection as DoctrineCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use JsonSerializable;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
* @UniqueEntity("name")
*/
class Product implements JsonSerializable
{
use CustomSlugify;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=50)
* @Assert\Length(min = 4, max = 50, allowEmptyString = false )
* @Assert\Regex("/[A-Za-z]/")
*/
private $name;
/**
* @Gedmo\Translatable
* @ORM\Column(type="string", length=512, nullable=true)
*/
private $description;
/**
* @ORM\Column(type="string", length=30, nullable=true)
*/
private $colorCode;
/**
* @ORM\Column(type="boolean")
*/
private $bestSeller;
/**
* @ORM\Column(type="smallint")
*/
private $orderAppearance;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $fileArchi;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $fileRevit;
/**
* @ORM\Column(type="boolean")
*/
private $actif;
/**
* @ORM\Column(length=64, unique=true)
*/
private $slug;
/**
* @var DateTime
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime", nullable=true)
*/
protected $createdAt;
/**
* @var DateTime
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime", nullable=true)
*/
protected $updatedAt;
/**
* @ORM\OneToMany(targetEntity="Attachment", mappedBy="product", cascade={"persist", "remove"}, fetch="EAGER")
*/
private $attachments;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Collection", inversedBy="products")
* @ORM\JoinColumn(nullable=false)
*/
private $collection;
/**
* @ORM\ManyToMany(targetEntity="Project", inversedBy="products", cascade={"persist"})
*/
private $projects;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Color", mappedBy="products", fetch="EAGER")
* @ORM\JoinTable(name="color_product")
*/
private $colors;
/**
* @ORM\ManyToMany(targetEntity=Order::class, mappedBy="products")
*/
private $orders;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $new;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $moq;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $stock;
public function __construct()
{
$this->attachments = new ArrayCollection();
$this->projects = new ArrayCollection();
$this->colors = new ArrayCollection();
$this->orders = new ArrayCollection();
}
public function setTranslatableLocale($locale)
{
$this->locale = $locale;
}
public function __toString()
{
return $this->name.'';
}
public function jsonSerialize()
{
return [
'id' => $this->getId(),
'name' => $this->getName(),
'slug' => $this->getSlug(),
'description' => $this->getDescription(),
'orderAppearance' => $this->getOrderAppearance(),
'colorCode' => $this->getColorCode(),
'bestSeller' => $this->getBestSeller(),
'collectionSlug' => $this->getCollection()->getSlug(),
'fileArchi' => $this->getFileArchi(),
'fileRevit' => $this->getFileRevit(),
'colors' => $this->getColors()->getValues(),
'attachments' => isset($this->attachmentsFile) ? $this->attachmentsFile : null,
'new' => $this->getNew(),
'moq' => $this->getMoq(),
'stock' => $this->getStock()
];
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = ucfirst($name);
return $this;
}
/**
* @return mixed
*/
public function getDescription()
{
return $this->description;
}
/**
* @param mixed $description
*/
public function setDescription($description): void
{
$this->description = $description;
}
public function getColorCode(): ?string
{
return $this->colorCode;
}
public function setColorCode(string $colorCode): self
{
$this->colorCode = $colorCode;
return $this;
}
public function getBestSeller(): ?bool
{
return $this->bestSeller;
}
public function setBestSeller(bool $bestSeller): self
{
$this->bestSeller = $bestSeller;
return $this;
}
public function getOrderAppearance(): ?int
{
return $this->orderAppearance;
}
public function setOrderAppearance(int $orderAppearance): self
{
$this->orderAppearance = $orderAppearance;
return $this;
}
public function getFileArchi(): ?string
{
return $this->fileArchi;
}
public function setFileArchi(?string $fileArchi): self
{
$this->fileArchi = $fileArchi;
return $this;
}
public function getFileRevit(): ?string
{
return $this->fileRevit;
}
public function setFileRevit(?string $fileRevit): self
{
$this->fileRevit = $fileRevit;
return $this;
}
public function getActif(): ?bool
{
return $this->actif;
}
public function setActif(bool $actif): self
{
$this->actif = $actif;
return $this;
}
/**
* Set slug.
*
* @param string $slug
*
* @return Product
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug.
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updateSlug()
{
$this->slug = $this->slugify($this->name);
}
/**
* @ORM\PrePersist
*/
public function initOrderApparearance()
{
$this->orderAppearance = 0;
$this->createdAt = new DateTime();
$this->updatedAt = new DateTime();
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return DoctrineCollection|Attachment[]
*/
public function getAttachments(): DoctrineCollection
{
return $this->attachments;
}
public function addAttachment(Attachment $attachment): self
{
if (!$this->attachments->contains($attachment)) {
$this->attachments[] = $attachment;
$attachment->setProduct($this);
}
return $this;
}
public function removeAttachment(Attachment $attachment): self
{
if ($this->attachments->contains($attachment)) {
$this->attachments->removeElement($attachment);
// set the owning side to null (unless already changed)
if ($attachment->getProduct() === $this) {
$attachment->setProduct(null);
}
}
return $this;
}
public function getCollection(): ?ProductCollection
{
return $this->collection;
}
public function setCollection(?ProductCollection $collection): self
{
$this->collection = $collection;
return $this;
}
/**
* @return DoctrineCollection|Project[]
*/
public function getProjects(): DoctrineCollection
{
return $this->projects;
}
public function addProject(Project $project): self
{
if (!$this->projects->contains($project)) {
$this->projects[] = $project;
}
return $this;
}
public function removeProject(Project $project): self
{
if ($this->projects->contains($project)) {
$this->projects->removeElement($project);
}
return $this;
}
/**
* @return DoctrineCollection|Color[]
*/
public function getColors(): DoctrineCollection
{
return $this->colors;
}
public function addColor(Color $color): self
{
if (!$this->colors->contains($color)) {
$this->colors[] = $color;
$color->addProduct($this);
}
return $this;
}
public function removeColor(Color $color): self
{
if ($this->colors->contains($color)) {
$this->colors->removeElement($color);
$color->removeProduct($this);
}
return $this;
}
/**
* @return DoctrineCollection|Order[]
*/
public function getOrders(): DoctrineCollection
{
return $this->orders;
}
public function addOrder(Order $order): self
{
if (!$this->orders->contains($order)) {
$this->orders[] = $order;
$order->addProduct($this);
}
return $this;
}
public function removeOrder(Order $order): self
{
if ($this->orders->contains($order)) {
$this->orders->removeElement($order);
$order->removeProduct($this);
}
return $this;
}
public function getNew(): ?bool
{
return $this->new;
}
public function setNew(?bool $new): self
{
$this->new = $new;
return $this;
}
public function getMoq(): ?bool
{
return $this->moq;
}
public function setMoq(?bool $moq): self
{
$this->moq = $moq;
return $this;
}
public function getStock(): ?bool
{
return $this->stock;
}
public function setStock(?bool $stock): self
{
$this->stock = $stock;
return $this;
}
}