src/Entity/CustomProduct.php line 27

Open in your IDE?
  1. <?php
  2.     namespace App\Entity;
  3.     use App\Repository\CustomProductRepository;
  4.     use App\Traits\DateTrait;
  5.     use DateTime;
  6.     use Doctrine\Common\Collections\ArrayCollection;
  7.     use Doctrine\Common\Collections\Collection;
  8.     use Doctrine\ORM\Mapping as ORM;
  9.     use JMS\Serializer\Annotation as Serializer;
  10.     use JMS\Serializer\Annotation\Expose;
  11.     use JMS\Serializer\Annotation\Groups;
  12.     use Symfony\Component\HttpFoundation\File\File;
  13.     use Symfony\Component\HttpFoundation\File\UploadedFile;
  14.     use Symfony\Component\Validator\Constraints as Assert;
  15.     use Vich\UploaderBundle\Mapping\Annotation as Vich;
  16.     /**
  17.      * Produit personnalisé
  18.      *
  19.      * @ORM\Entity(repositoryClass=CustomProductRepository::class)
  20.      *
  21.      * @Serializer\ExclusionPolicy("ALL")
  22.      * @Vich\Uploadable
  23.      */
  24.     class CustomProduct
  25.     {
  26.         /**
  27.          * @ORM\Id
  28.          * @ORM\GeneratedValue
  29.          * @ORM\Column(type="integer")
  30.          *
  31.          * @Expose
  32.          * @Groups({"customProduct:list", "customProduct:item"})
  33.          */
  34.         private ?int $id NULL;
  35.         /**
  36.          * Label du produit
  37.          *
  38.          * @ORM\Column(type="string", length=255)
  39.          *
  40.          * @Assert\Length(
  41.          *      min = 2,
  42.          *      max = 255,
  43.          * )
  44.          *
  45.          * @Expose
  46.          * @Groups({"customProduct:list", "customProduct:item"})
  47.          */
  48.         private ?string $label NULL;
  49.         /**
  50.          * Description du produit
  51.          *
  52.          * @ORM\Column(type="text", nullable=true)
  53.          *
  54.          * @Expose
  55.          * @Groups({"customProduct:item"})
  56.          */
  57.         private ?string $description NULL;
  58.         /**
  59.          * Valeur (prix) du produit
  60.          *
  61.          * @ORM\Column(type="float", nullable=true)
  62.          *
  63.          * @Assert\PositiveOrZero
  64.          *
  65.          * @Expose
  66.          * @Groups({"customProduct:list", "customProduct:item"})
  67.          */
  68.         private ?float $value NULL;
  69.         /**
  70.          * Indique si le produit est actif
  71.          *
  72.          * @ORM\Column(type="boolean")
  73.          *
  74.          * @Expose
  75.          * @Groups({"customProduct:list", "customProduct:item"})
  76.          */
  77.         private bool $enabled TRUE;
  78.         /**
  79.          * Image du produit
  80.          *
  81.          * @ORM\Column(type="string", length=255, nullable=true)
  82.          *
  83.          * @Expose
  84.          * @Groups({"customProduct:list", "customProduct:item"})
  85.          */
  86.         private ?string $image NULL;
  87.         /**
  88.          * Vich Uploader
  89.          *
  90.          * @Vich\UploadableField(mapping="custom_product_images", fileNameProperty="image")
  91.          * @var null|File
  92.          */
  93.         private ?File $imageFile NULL;
  94.         /**
  95.          * Liste des contacts (email) quand il y a une demande pour ce produit
  96.          *
  97.          * @ORM\Column(type="array", nullable=true)
  98.          *
  99.          * @Expose
  100.          * @Groups({"customProduct:item"})
  101.          */
  102.         private array $contacts = [];
  103.         /**
  104.          * Liste des champs pour commander ce produit
  105.          *
  106.          * @ORM\OneToMany(
  107.          *     targetEntity=CustomProductField::class,
  108.          *     mappedBy="product",
  109.          *     orphanRemoval=true,
  110.          *     cascade={"persist"}
  111.          * )
  112.          */
  113.         private $fields;
  114.         /**
  115.          * Type de produit
  116.          *
  117.          * @ORM\ManyToOne(targetEntity=CustomProductType::class, inversedBy="templates")
  118.          * @ORM\JoinColumn(nullable=false, name="type", referencedColumnName="slug")
  119.          *
  120.          * @Expose
  121.          * @Groups({"customProduct:list", "customProduct:item"})
  122.          */
  123.         private ?CustomProductType $type NULL;
  124.         /**
  125.          * @ORM\Column(type="string", length=10, nullable=true)
  126.          *
  127.          * @Expose
  128.          * @Groups({"customProduct:list", "customProduct:item"})
  129.          */
  130.         private $sku;
  131.         /**
  132.          * @ORM\Column(type="integer", nullable=true)
  133.          */
  134.         private $stockAlert;
  135.         /**
  136.          * @ORM\Column(type="string", length=255, nullable=true)
  137.          */
  138.         private $reference;
  139.         /**
  140.          * @ORM\ManyToOne(targetEntity=User::class, inversedBy="customProducts")
  141.          */
  142.         private $createdBy;
  143.         /**
  144.          * @ORM\Column(type="text", nullable=true)
  145.          */
  146.         private $categoryValues;
  147.         use DateTrait;
  148.         public function __construct()
  149.         {
  150.             $this->fields = new ArrayCollection();
  151.         }
  152.         /**
  153.          * @Serializer\VirtualProperty()
  154.          * @Serializer\SerializedName("array_category_values")
  155.          *
  156.          * @Expose
  157.          * @Groups({"customProduct:list"})
  158.          *
  159.          * @return mixed
  160.          */
  161.         public function getArrayCategoryValues()
  162.         {
  163.             return json_decode($this->categoryValuesTRUE) ?? [];
  164.         }
  165.         public function modifyValueToCategory(string $slugint $value): self
  166.         {
  167.             $newArray $this->getArrayCategoryValues();
  168.             $newArray[$slug] = $value;
  169.             $this->categoryValues json_encode($newArray);
  170.             return $this;
  171.         }
  172.         public function getId(): ?int
  173.         {
  174.             return $this->id;
  175.         }
  176.         public function getImageFile()
  177.         {
  178.             return $this->imageFile;
  179.         }
  180.         /**
  181.          * @param   File|UploadedFile|null  $imageFile
  182.          */
  183.         public function setImageFile(File $imageFile): void
  184.         {
  185.             $this->imageFile $imageFile;
  186.             $this->updatedAt = new DateTime();
  187.         }
  188.         public function getLabel(): ?string
  189.         {
  190.             return $this->label;
  191.         }
  192.         public function setLabel(string $label): self
  193.         {
  194.             $this->label $label;
  195.             return $this;
  196.         }
  197.         public function getDescription(): ?string
  198.         {
  199.             return $this->description;
  200.         }
  201.         public function setDescription(?string $description): self
  202.         {
  203.             $this->description $description;
  204.             return $this;
  205.         }
  206.         public function getValue(): ?float
  207.         {
  208.             return $this->value;
  209.         }
  210.         public function setValue(?float $value): self
  211.         {
  212.             $this->value $value;
  213.             return $this;
  214.         }
  215.         public function isEnabled(): ?bool
  216.         {
  217.             return $this->enabled;
  218.         }
  219.         public function setEnabled(bool $enabled): self
  220.         {
  221.             $this->enabled $enabled;
  222.             return $this;
  223.         }
  224.         public function getImage(): ?string
  225.         {
  226.             return $this->image;
  227.         }
  228.         public function setImage(?string $image): self
  229.         {
  230.             $this->image $image;
  231.             return $this;
  232.         }
  233.         public function getContacts(): ?array
  234.         {
  235.             return $this->contacts;
  236.         }
  237.         public function setContacts(array $contacts): self
  238.         {
  239.             $this->contacts $contacts;
  240.             return $this;
  241.         }
  242.         /**
  243.          * @return Collection<int, CustomProductField>
  244.          */
  245.         public function getFields(): Collection
  246.         {
  247.             return $this->fields;
  248.         }
  249.         public function addField(CustomProductField $field): self
  250.         {
  251.             if (!$this->fields->contains($field)) {
  252.                 $this->fields[] = $field;
  253.                 $field->setProduct($this);
  254.             }
  255.             return $this;
  256.         }
  257.         public function removeField(CustomProductField $field): self
  258.         {
  259.             if ($this->fields->removeElement($field)) {
  260.                 // set the owning side to null (unless already changed)
  261.                 if ($field->getProduct() === $this) {
  262.                     $field->setProduct(NULL);
  263.                 }
  264.             }
  265.             return $this;
  266.         }
  267.         public function getType(): ?CustomProductType
  268.         {
  269.             return $this->type;
  270.         }
  271.         public function setType(?CustomProductType $type): self
  272.         {
  273.             $this->type $type;
  274.             return $this;
  275.         }
  276.         public function getSku(): ?string
  277.         {
  278.             return $this->sku;
  279.         }
  280.         public function setSku(?string $sku): self
  281.         {
  282.             $this->sku $sku;
  283.             return $this;
  284.         }
  285.         public function getStockAlert(): ?int
  286.         {
  287.             return $this->stockAlert;
  288.         }
  289.         public function setStockAlert(?int $stockAlert): self
  290.         {
  291.             $this->stockAlert $stockAlert;
  292.             return $this;
  293.         }
  294.         public function getReference(): ?string
  295.         {
  296.             return $this->reference;
  297.         }
  298.         public function setReference(?string $reference): self
  299.         {
  300.             $this->reference $reference;
  301.             return $this;
  302.         }
  303.         public function getCreatedBy(): ?User
  304.         {
  305.             return $this->createdBy;
  306.         }
  307.         public function setCreatedBy(?User $createdBy): self
  308.         {
  309.             $this->createdBy $createdBy;
  310.             return $this;
  311.         }
  312.         public function getCategoryValues(): ?string
  313.         {
  314.             return $this->categoryValues;
  315.         }
  316.         public function setCategoryValues(?string $categoryValues): self
  317.         {
  318.             $this->categoryValues $categoryValues;
  319.             return $this;
  320.         }
  321.     }