<?php
namespace App\Entity;
use App\Repository\CustomProductRepository;
use App\Traits\DateTrait;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Groups;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* Produit personnalisé
*
* @ORM\Entity(repositoryClass=CustomProductRepository::class)
*
* @Serializer\ExclusionPolicy("ALL")
* @Vich\Uploadable
*/
class CustomProduct
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
private ?int $id = NULL;
/**
* Label du produit
*
* @ORM\Column(type="string", length=255)
*
* @Assert\Length(
* min = 2,
* max = 255,
* )
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
private ?string $label = NULL;
/**
* Description du produit
*
* @ORM\Column(type="text", nullable=true)
*
* @Expose
* @Groups({"customProduct:item"})
*/
private ?string $description = NULL;
/**
* Valeur (prix) du produit
*
* @ORM\Column(type="float", nullable=true)
*
* @Assert\PositiveOrZero
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
private ?float $value = NULL;
/**
* Indique si le produit est actif
*
* @ORM\Column(type="boolean")
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
private bool $enabled = TRUE;
/**
* Image du produit
*
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
private ?string $image = NULL;
/**
* Vich Uploader
*
* @Vich\UploadableField(mapping="custom_product_images", fileNameProperty="image")
* @var null|File
*/
private ?File $imageFile = NULL;
/**
* Liste des contacts (email) quand il y a une demande pour ce produit
*
* @ORM\Column(type="array", nullable=true)
*
* @Expose
* @Groups({"customProduct:item"})
*/
private array $contacts = [];
/**
* Liste des champs pour commander ce produit
*
* @ORM\OneToMany(
* targetEntity=CustomProductField::class,
* mappedBy="product",
* orphanRemoval=true,
* cascade={"persist"}
* )
*/
private $fields;
/**
* Type de produit
*
* @ORM\ManyToOne(targetEntity=CustomProductType::class, inversedBy="templates")
* @ORM\JoinColumn(nullable=false, name="type", referencedColumnName="slug")
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
private ?CustomProductType $type = NULL;
/**
* @ORM\Column(type="string", length=10, nullable=true)
*
* @Expose
* @Groups({"customProduct:list", "customProduct:item"})
*/
private $sku;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $stockAlert;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $reference;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="customProducts")
*/
private $createdBy;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $categoryValues;
use DateTrait;
public function __construct()
{
$this->fields = new ArrayCollection();
}
/**
* @Serializer\VirtualProperty()
* @Serializer\SerializedName("array_category_values")
*
* @Expose
* @Groups({"customProduct:list"})
*
* @return mixed
*/
public function getArrayCategoryValues()
{
return json_decode($this->categoryValues, TRUE) ?? [];
}
public function modifyValueToCategory(string $slug, int $value): self
{
$newArray = $this->getArrayCategoryValues();
$newArray[$slug] = $value;
$this->categoryValues = json_encode($newArray);
return $this;
}
public function getId(): ?int
{
return $this->id;
}
public function getImageFile()
{
return $this->imageFile;
}
/**
* @param File|UploadedFile|null $imageFile
*/
public function setImageFile(File $imageFile): void
{
$this->imageFile = $imageFile;
$this->updatedAt = new DateTime();
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getValue(): ?float
{
return $this->value;
}
public function setValue(?float $value): self
{
$this->value = $value;
return $this;
}
public function isEnabled(): ?bool
{
return $this->enabled;
}
public function setEnabled(bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
public function getContacts(): ?array
{
return $this->contacts;
}
public function setContacts(array $contacts): self
{
$this->contacts = $contacts;
return $this;
}
/**
* @return Collection<int, CustomProductField>
*/
public function getFields(): Collection
{
return $this->fields;
}
public function addField(CustomProductField $field): self
{
if (!$this->fields->contains($field)) {
$this->fields[] = $field;
$field->setProduct($this);
}
return $this;
}
public function removeField(CustomProductField $field): self
{
if ($this->fields->removeElement($field)) {
// set the owning side to null (unless already changed)
if ($field->getProduct() === $this) {
$field->setProduct(NULL);
}
}
return $this;
}
public function getType(): ?CustomProductType
{
return $this->type;
}
public function setType(?CustomProductType $type): self
{
$this->type = $type;
return $this;
}
public function getSku(): ?string
{
return $this->sku;
}
public function setSku(?string $sku): self
{
$this->sku = $sku;
return $this;
}
public function getStockAlert(): ?int
{
return $this->stockAlert;
}
public function setStockAlert(?int $stockAlert): self
{
$this->stockAlert = $stockAlert;
return $this;
}
public function getReference(): ?string
{
return $this->reference;
}
public function setReference(?string $reference): self
{
$this->reference = $reference;
return $this;
}
public function getCreatedBy(): ?User
{
return $this->createdBy;
}
public function setCreatedBy(?User $createdBy): self
{
$this->createdBy = $createdBy;
return $this;
}
public function getCategoryValues(): ?string
{
return $this->categoryValues;
}
public function setCategoryValues(?string $categoryValues): self
{
$this->categoryValues = $categoryValues;
return $this;
}
}