src/Entity/TrProduitsMasse.php line 44

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\TrProduitsMasseRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use ApiPlatform\Core\Annotation\ApiResource;
  8. use ApiPlatform\Core\Annotation\ApiProperty;
  9. use ApiPlatform\Core\Annotation\ApiFilter;
  10. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  11. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  12. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
  13. use Symfony\Component\Serializer\Annotation\Groups;
  14. use Symfony\Component\Serializer\Annotation\SerializedName;
  15. /**
  16.  * @ORM\Entity(repositoryClass=TrProduitsMasseRepository::class)
  17.  * @ApiResource(
  18.  *   routePrefix="/v1",
  19.  *   normalizationContext={"groups"={"pm:read"}},
  20.  *   denormalizationContext={"groups"={"pm:write"}},
  21.  *   paginationEnabled=false,
  22.  *   collectionOperations={
  23.  *     "get"={"path"="/tr_produits_masse"},
  24.  *     "post"={"path"="/tr_produits_masse"}
  25.  *   },
  26.  *   itemOperations={
  27.  *     "get"={"path"="/tr_produits_masse/{id}"},
  28.  *     "put"={"path"="/tr_produits_masse/{id}"},
  29.  *     "patch"={"path"="/tr_produits_masse/{id}"},
  30.  *     "delete"={"path"="/tr_produits_masse/{id}"}
  31.  *   }
  32.  * )
  33.  * @ApiFilter(SearchFilter::class, properties={
  34.  *   "Libelle": "partial",
  35.  *   "Description": "partial",
  36.  *   "CATEGORIES": "partial",
  37.  *   "ALIAS": "partial"
  38.  * })
  39.  * @ApiFilter(OrderFilter::class, properties={"PRIX","id"})
  40.  */
  41. class TrProduitsMasse
  42. {
  43.     /**
  44.      * @ORM\Id
  45.      * @ORM\GeneratedValue
  46.      * @ORM\Column(type="integer")
  47.      * @Groups({"pm:read"})
  48.      */
  49.     private $id;
  50.     /**
  51.      * @ORM\Column(type="string", length=255)
  52.      * @Groups({"pm:read","pm:write"})
  53.      */
  54.     private $Libelle;
  55.     /**
  56.      * @ORM\Column(type="float")
  57.      * @Groups({"pm:read","pm:write"})
  58.      */
  59.     private $PRIX;
  60.     /**
  61.      * @ORM\ManyToOne(targetEntity=TrCategorie::class, inversedBy="trProduitsMasses")
  62.      * @ORM\JoinColumn(nullable=false)
  63.      * @Groups({"pm:read","pm:write"})
  64.      * @ApiProperty(readableLink=false, writableLink=true)
  65.      */
  66.     private $CATEGORIE;
  67.     /**
  68.      * @ORM\Column(type="string", length=255)
  69.      * @Groups({"pm:read","pm:write"})
  70.      */
  71.     private $IMAGE;
  72.     /**
  73.      * @ORM\Column(type="string", length=255, nullable=true)
  74.      * @Groups({"pm:read","pm:write"})
  75.      */
  76.     private $Description;
  77.     /**
  78.      * @ORM\Column(type="string", length=255)
  79.      * @Groups({"pm:read","pm:write"})
  80.      */
  81.     private $CATEGORIES;
  82.     /**
  83.      * @ORM\Column(type="boolean")
  84.      * @Groups({"pm:read","pm:write"})
  85.      */
  86.     private $VEDETTE;
  87.     /**
  88.      * @ORM\Column(type="string", length=255, nullable=true)
  89.      * @Groups({"pm:read","pm:write"})
  90.      */
  91.     private $ALIAS;
  92.     /**
  93.      * @ORM\Column(type="string", length=255, nullable=true)
  94.      * @Groups({"pm:read","pm:write"})
  95.      */
  96.     private $Description_2;
  97.     /**
  98.      * @ORM\Column(type="string", length=255, nullable=true)
  99.      * @Groups({"pm:read","pm:write"})
  100.      */
  101.     private $Description_3;
  102.     /**
  103.      * @ORM\Column(type="string", length=255, nullable=true)
  104.      * @Groups({"pm:read","pm:write"})
  105.      */
  106.     private $Description_4;
  107.     /**
  108.      * @ORM\Column(type="string", length=255, nullable=true)
  109.      * @Groups({"pm:read","pm:write"})
  110.      */
  111.     private $Description_5;
  112.     /**
  113.      * @ORM\Column(type="string", length=255, nullable=true)
  114.      * @Groups({"pm:read","pm:write"})
  115.      */
  116.     private $Description_6;
  117.     /**
  118.      * @ORM\Column(type="string", length=255, nullable=true)
  119.      * @Groups({"pm:read","pm:write"})
  120.      */
  121.     private $Description_7;
  122.     /**
  123.      * @ORM\Column(type="string", length=255, nullable=true)
  124.      * @Groups({"pm:read","pm:write"})
  125.      */
  126.     private $Description_8;
  127.     /**
  128.      * @ORM\Column(type="string", length=255, nullable=true)
  129.      * @Groups({"pm:read","pm:write"})
  130.      */
  131.     private $Description_9;
  132.     /**
  133.      * @ORM\Column(type="string", length=255, nullable=true)
  134.      */
  135.     private $Description_10;
  136.     /**
  137.      * @ORM\Column(type="integer")
  138.      * @Groups({"pm:read","pm:write"})
  139.      */
  140.     private $Prix_1;
  141.     /**
  142.      * @ORM\Column(type="integer")
  143.      * @Groups({"pm:read","pm:write"})
  144.      */
  145.     private $Prix_2;
  146.     /**
  147.      * @ORM\Column(type="integer")
  148.      * @Groups({"pm:read","pm:write"})
  149.      */
  150.     private $Prix_3;
  151.     /**
  152.      * @ORM\Column(type="integer")
  153.      * @Groups({"pm:read","pm:write"})
  154.      */
  155.     private $Prix_4;
  156.     /**
  157.      * @ORM\Column(type="integer")
  158.      * @Groups({"pm:read","pm:write"})
  159.      */
  160.     private $Prix_5;
  161.     /**
  162.      * @ORM\ManyToOne(targetEntity=TrProduits::class, inversedBy="trProduitsMasses")
  163.      * @ORM\JoinColumn(nullable=false)
  164.      * @Groups({"pm:read","pm:write"})
  165.      */
  166.     private $Produit_id;
  167.     /**
  168.      * @ORM\OneToMany(targetEntity=TrImage::class, mappedBy="produitMasse", cascade={"persist"})
  169.      * @ORM\OrderBy({"isPrincipale" = "DESC", "ordre" = "ASC", "id" = "ASC"})
  170.      */
  171.     private $trImages;
  172.     public function __construct()
  173.     {
  174.         $this->trImages = new ArrayCollection();
  175.     }
  176.     public function getId(): ?int
  177.     {
  178.         return $this->id;
  179.     }
  180.     public function getLibelle(): ?string
  181.     {
  182.         return $this->Libelle;
  183.     }
  184.     public function setLibelle(string $Libelle): self
  185.     {
  186.         $this->Libelle = $Libelle;
  187.         return $this;
  188.     }
  189.     public function getPRIX(): ?float
  190.     {
  191.         return $this->PRIX;
  192.     }
  193.     public function setPRIX(float $PRIX): self
  194.     {
  195.         $this->PRIX = $PRIX;
  196.         return $this;
  197.     }
  198.     public function getCATEGORIE(): ?TrCategorie
  199.     {
  200.         return $this->CATEGORIE;
  201.     }
  202.     public function setCATEGORIE(?TrCategorie $CATEGORIE): self
  203.     {
  204.         $this->CATEGORIE = $CATEGORIE;
  205.         return $this;
  206.     }
  207.     public function getIMAGE(): ?string
  208.     {
  209.         return $this->IMAGE;
  210.     }
  211.     public function setIMAGE(string $IMAGE): self
  212.     {
  213.         $this->IMAGE = $IMAGE;
  214.         return $this;
  215.     }
  216.     public function getDescription(): ?string
  217.     {
  218.         return $this->Description;
  219.     }
  220.     public function setDescription(?string $Description): self
  221.     {
  222.         $this->Description = $Description;
  223.         return $this;
  224.     }
  225.     public function getCATEGORIES(): ?string
  226.     {
  227.         return $this->CATEGORIES;
  228.     }
  229.     public function setCATEGORIES(string $CATEGORIES): self
  230.     {
  231.         $this->CATEGORIES = $CATEGORIES;
  232.         return $this;
  233.     }
  234.     public function isVEDETTE(): ?bool
  235.     {
  236.         return $this->VEDETTE;
  237.     }
  238.     public function setVEDETTE(bool $VEDETTE): self
  239.     {
  240.         $this->VEDETTE = $VEDETTE;
  241.         return $this;
  242.     }
  243.     public function getALIAS(): ?string
  244.     {
  245.         return $this->ALIAS;
  246.     }
  247.     public function setALIAS(?string $ALIAS): self
  248.     {
  249.         $this->ALIAS = $ALIAS;
  250.         return $this;
  251.     }
  252.     public function getDescription2(): ?string
  253.     {
  254.         return $this->Description_2;
  255.     }
  256.     public function setDescription2(?string $Description_2): self
  257.     {
  258.         $this->Description_2 = $Description_2;
  259.         return $this;
  260.     }
  261.     public function getDescription3(): ?string
  262.     {
  263.         return $this->Description_3;
  264.     }
  265.     public function setDescription3(?string $Description_3): self
  266.     {
  267.         $this->Description_3 = $Description_3;
  268.         return $this;
  269.     }
  270.     public function getDescription4(): ?string
  271.     {
  272.         return $this->Description_4;
  273.     }
  274.     public function setDescription4(?string $Description_4): self
  275.     {
  276.         $this->Description_4 = $Description_4;
  277.         return $this;
  278.     }
  279.     public function getDescription5(): ?string
  280.     {
  281.         return $this->Description_5;
  282.     }
  283.     public function setDescription5(?string $Description_5): self
  284.     {
  285.         $this->Description_5 = $Description_5;
  286.         return $this;
  287.     }
  288.     public function getDescription6(): ?string
  289.     {
  290.         return $this->Description_6;
  291.     }
  292.     public function setDescription6(?string $Description_6): self
  293.     {
  294.         $this->Description_6 = $Description_6;
  295.         return $this;
  296.     }
  297.     public function getDescription7(): ?string
  298.     {
  299.         return $this->Description_7;
  300.     }
  301.     public function setDescription7(?string $Description_7): self
  302.     {
  303.         $this->Description_7 = $Description_7;
  304.         return $this;
  305.     }
  306.     public function getDescription8(): ?string
  307.     {
  308.         return $this->Description_8;
  309.     }
  310.     public function setDescription8(?string $Description_8): self
  311.     {
  312.         $this->Description_8 = $Description_8;
  313.         return $this;
  314.     }
  315.     public function getDescription9(): ?string
  316.     {
  317.         return $this->Description_9;
  318.     }
  319.     public function setDescription9(?string $Description_9): self
  320.     {
  321.         $this->Description_9 = $Description_9;
  322.         return $this;
  323.     }
  324.     public function getDescription10(): ?string
  325.     {
  326.         return $this->Description_10;
  327.     }
  328.     public function setDescription10(?string $Description_10): self
  329.     {
  330.         $this->Description_10 = $Description_10;
  331.         return $this;
  332.     }
  333.     public function getPrix1(): ?int
  334.     {
  335.         return $this->Prix_1;
  336.     }
  337.     public function setPrix1(int $Prix_1): self
  338.     {
  339.         $this->Prix_1 = $Prix_1;
  340.         return $this;
  341.     }
  342.     public function getPrix2(): ?int
  343.     {
  344.         return $this->Prix_2;
  345.     }
  346.     public function setPrix2(int $Prix_2): self
  347.     {
  348.         $this->Prix_2 = $Prix_2;
  349.         return $this;
  350.     }
  351.     public function getPrix3(): ?int
  352.     {
  353.         return $this->Prix_3;
  354.     }
  355.     public function setPrix3(int $Prix_3): self
  356.     {
  357.         $this->Prix_3 = $Prix_3;
  358.         return $this;
  359.     }
  360.     public function getPrix4(): ?int
  361.     {
  362.         return $this->Prix_4;
  363.     }
  364.     public function setPrix4(int $Prix_4): self
  365.     {
  366.         $this->Prix_4 = $Prix_4;
  367.         return $this;
  368.     }
  369.     public function getPrix5(): ?int
  370.     {
  371.         return $this->Prix_5;
  372.     }
  373.     public function setPrix5(int $Prix_5): self
  374.     {
  375.         $this->Prix_5 = $Prix_5;
  376.         return $this;
  377.     }
  378.     public function getProduitId(): ?TrProduits
  379.     {
  380.         return $this->Produit_id;
  381.     }
  382.     public function setProduitId(?TrProduits $Produit_id): self
  383.     {
  384.         $this->Produit_id = $Produit_id;
  385.         return $this;
  386.     }
  387.     /**
  388.      * @return Collection<int, TrImage>
  389.      */
  390.     public function getTrImages(): Collection
  391.     {
  392.         return $this->trImages;
  393.     }
  394.     public function addTrImage(TrImage $trImage): self
  395.     {
  396.         if (!$this->trImages->contains($trImage)) {
  397.             $this->trImages[] = $trImage;
  398.             $trImage->setProduitMasse($this);
  399.         }
  400.         return $this;
  401.     }
  402.     public function removeTrImage(TrImage $trImage): self
  403.     {
  404.         if ($this->trImages->removeElement($trImage)) {
  405.             if ($trImage->getProduitMasse() === $this) {
  406.                 $trImage->setProduitMasse(null);
  407.             }
  408.         }
  409.         return $this;
  410.     }
  411.     /**
  412.      * @Groups({"pm:read"})
  413.      * @SerializedName("images")
  414.      */
  415.     public function getImages(): array
  416.     {
  417.         $images = array();
  418.         foreach ($this->trImages as $image) {
  419.             if ($image instanceof TrImage && $image->isActif()) {
  420.                 $images[] = $image;
  421.             }
  422.         }
  423.         usort($images, static function (TrImage $a, TrImage $b): int {
  424.             if ((bool) $a->isPrincipale() !== (bool) $b->isPrincipale()) {
  425.                 return $a->isPrincipale() ? -1 : 1;
  426.             }
  427.             $ordreCompare = ((int) $a->getOrdre()) <=> ((int) $b->getOrdre());
  428.             if (0 !== $ordreCompare) {
  429.                 return $ordreCompare;
  430.             }
  431.             return ((int) $a->getId()) <=> ((int) $b->getId());
  432.         });
  433.         $payload = array();
  434.         foreach (array_slice($images, 0, TrImage::MAX_IMAGES_PER_PRODUCT) as $image) {
  435.             $payload[] = $image->toApiArray();
  436.         }
  437.         if ([] === $payload && $this->getIMAGE()) {
  438.             $payload[] = array(
  439.                 'id' => null,
  440.                 'url' => $this->buildLegacyImageUrl($this->getIMAGE()),
  441.                 'imagePath' => $this->getIMAGE(),
  442.                 'imageName' => $this->getIMAGE(),
  443.                 'ordre' => 1,
  444.                 'isPrincipale' => true,
  445.             );
  446.         }
  447.         return $payload;
  448.     }
  449.     private function buildLegacyImageUrl(string $image): string
  450.     {
  451.         $image = trim($image);
  452.         if (preg_match('/^https?:\/\//i', $image)) {
  453.             return $image;
  454.         }
  455.         if (0 === strpos($image, '/')) {
  456.             return $image;
  457.         }
  458.         if (0 === strpos($image, 'uploads/')) {
  459.             return '/'.$image;
  460.         }
  461.         return '/uploads/brochures/'.$image;
  462.     }
  463. }