<?php
namespace App\Entity;
use App\Repository\TrProduitsMasseRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName;
/**
* @ORM\Entity(repositoryClass=TrProduitsMasseRepository::class)
* @ApiResource(
* routePrefix="/v1",
* normalizationContext={"groups"={"pm:read"}},
* denormalizationContext={"groups"={"pm:write"}},
* paginationEnabled=false,
* collectionOperations={
* "get"={"path"="/tr_produits_masse"},
* "post"={"path"="/tr_produits_masse"}
* },
* itemOperations={
* "get"={"path"="/tr_produits_masse/{id}"},
* "put"={"path"="/tr_produits_masse/{id}"},
* "patch"={"path"="/tr_produits_masse/{id}"},
* "delete"={"path"="/tr_produits_masse/{id}"}
* }
* )
* @ApiFilter(SearchFilter::class, properties={
* "Libelle": "partial",
* "Description": "partial",
* "CATEGORIES": "partial",
* "ALIAS": "partial"
* })
* @ApiFilter(OrderFilter::class, properties={"PRIX","id"})
*/
class TrProduitsMasse
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"pm:read"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"pm:read","pm:write"})
*/
private $Libelle;
/**
* @ORM\Column(type="float")
* @Groups({"pm:read","pm:write"})
*/
private $PRIX;
/**
* @ORM\ManyToOne(targetEntity=TrCategorie::class, inversedBy="trProduitsMasses")
* @ORM\JoinColumn(nullable=false)
* @Groups({"pm:read","pm:write"})
* @ApiProperty(readableLink=false, writableLink=true)
*/
private $CATEGORIE;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"pm:read","pm:write"})
*/
private $IMAGE;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"pm:read","pm:write"})
*/
private $Description;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"pm:read","pm:write"})
*/
private $CATEGORIES;
/**
* @ORM\Column(type="boolean")
* @Groups({"pm:read","pm:write"})
*/
private $VEDETTE;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"pm:read","pm:write"})
*/
private $ALIAS;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"pm:read","pm:write"})
*/
private $Description_2;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"pm:read","pm:write"})
*/
private $Description_3;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"pm:read","pm:write"})
*/
private $Description_4;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"pm:read","pm:write"})
*/
private $Description_5;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"pm:read","pm:write"})
*/
private $Description_6;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"pm:read","pm:write"})
*/
private $Description_7;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"pm:read","pm:write"})
*/
private $Description_8;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"pm:read","pm:write"})
*/
private $Description_9;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $Description_10;
/**
* @ORM\Column(type="integer")
* @Groups({"pm:read","pm:write"})
*/
private $Prix_1;
/**
* @ORM\Column(type="integer")
* @Groups({"pm:read","pm:write"})
*/
private $Prix_2;
/**
* @ORM\Column(type="integer")
* @Groups({"pm:read","pm:write"})
*/
private $Prix_3;
/**
* @ORM\Column(type="integer")
* @Groups({"pm:read","pm:write"})
*/
private $Prix_4;
/**
* @ORM\Column(type="integer")
* @Groups({"pm:read","pm:write"})
*/
private $Prix_5;
/**
* @ORM\ManyToOne(targetEntity=TrProduits::class, inversedBy="trProduitsMasses")
* @ORM\JoinColumn(nullable=false)
* @Groups({"pm:read","pm:write"})
*/
private $Produit_id;
/**
* @ORM\OneToMany(targetEntity=TrImage::class, mappedBy="produitMasse", cascade={"persist"})
* @ORM\OrderBy({"isPrincipale" = "DESC", "ordre" = "ASC", "id" = "ASC"})
*/
private $trImages;
public function __construct()
{
$this->trImages = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getLibelle(): ?string
{
return $this->Libelle;
}
public function setLibelle(string $Libelle): self
{
$this->Libelle = $Libelle;
return $this;
}
public function getPRIX(): ?float
{
return $this->PRIX;
}
public function setPRIX(float $PRIX): self
{
$this->PRIX = $PRIX;
return $this;
}
public function getCATEGORIE(): ?TrCategorie
{
return $this->CATEGORIE;
}
public function setCATEGORIE(?TrCategorie $CATEGORIE): self
{
$this->CATEGORIE = $CATEGORIE;
return $this;
}
public function getIMAGE(): ?string
{
return $this->IMAGE;
}
public function setIMAGE(string $IMAGE): self
{
$this->IMAGE = $IMAGE;
return $this;
}
public function getDescription(): ?string
{
return $this->Description;
}
public function setDescription(?string $Description): self
{
$this->Description = $Description;
return $this;
}
public function getCATEGORIES(): ?string
{
return $this->CATEGORIES;
}
public function setCATEGORIES(string $CATEGORIES): self
{
$this->CATEGORIES = $CATEGORIES;
return $this;
}
public function isVEDETTE(): ?bool
{
return $this->VEDETTE;
}
public function setVEDETTE(bool $VEDETTE): self
{
$this->VEDETTE = $VEDETTE;
return $this;
}
public function getALIAS(): ?string
{
return $this->ALIAS;
}
public function setALIAS(?string $ALIAS): self
{
$this->ALIAS = $ALIAS;
return $this;
}
public function getDescription2(): ?string
{
return $this->Description_2;
}
public function setDescription2(?string $Description_2): self
{
$this->Description_2 = $Description_2;
return $this;
}
public function getDescription3(): ?string
{
return $this->Description_3;
}
public function setDescription3(?string $Description_3): self
{
$this->Description_3 = $Description_3;
return $this;
}
public function getDescription4(): ?string
{
return $this->Description_4;
}
public function setDescription4(?string $Description_4): self
{
$this->Description_4 = $Description_4;
return $this;
}
public function getDescription5(): ?string
{
return $this->Description_5;
}
public function setDescription5(?string $Description_5): self
{
$this->Description_5 = $Description_5;
return $this;
}
public function getDescription6(): ?string
{
return $this->Description_6;
}
public function setDescription6(?string $Description_6): self
{
$this->Description_6 = $Description_6;
return $this;
}
public function getDescription7(): ?string
{
return $this->Description_7;
}
public function setDescription7(?string $Description_7): self
{
$this->Description_7 = $Description_7;
return $this;
}
public function getDescription8(): ?string
{
return $this->Description_8;
}
public function setDescription8(?string $Description_8): self
{
$this->Description_8 = $Description_8;
return $this;
}
public function getDescription9(): ?string
{
return $this->Description_9;
}
public function setDescription9(?string $Description_9): self
{
$this->Description_9 = $Description_9;
return $this;
}
public function getDescription10(): ?string
{
return $this->Description_10;
}
public function setDescription10(?string $Description_10): self
{
$this->Description_10 = $Description_10;
return $this;
}
public function getPrix1(): ?int
{
return $this->Prix_1;
}
public function setPrix1(int $Prix_1): self
{
$this->Prix_1 = $Prix_1;
return $this;
}
public function getPrix2(): ?int
{
return $this->Prix_2;
}
public function setPrix2(int $Prix_2): self
{
$this->Prix_2 = $Prix_2;
return $this;
}
public function getPrix3(): ?int
{
return $this->Prix_3;
}
public function setPrix3(int $Prix_3): self
{
$this->Prix_3 = $Prix_3;
return $this;
}
public function getPrix4(): ?int
{
return $this->Prix_4;
}
public function setPrix4(int $Prix_4): self
{
$this->Prix_4 = $Prix_4;
return $this;
}
public function getPrix5(): ?int
{
return $this->Prix_5;
}
public function setPrix5(int $Prix_5): self
{
$this->Prix_5 = $Prix_5;
return $this;
}
public function getProduitId(): ?TrProduits
{
return $this->Produit_id;
}
public function setProduitId(?TrProduits $Produit_id): self
{
$this->Produit_id = $Produit_id;
return $this;
}
/**
* @return Collection<int, TrImage>
*/
public function getTrImages(): Collection
{
return $this->trImages;
}
public function addTrImage(TrImage $trImage): self
{
if (!$this->trImages->contains($trImage)) {
$this->trImages[] = $trImage;
$trImage->setProduitMasse($this);
}
return $this;
}
public function removeTrImage(TrImage $trImage): self
{
if ($this->trImages->removeElement($trImage)) {
if ($trImage->getProduitMasse() === $this) {
$trImage->setProduitMasse(null);
}
}
return $this;
}
/**
* @Groups({"pm:read"})
* @SerializedName("images")
*/
public function getImages(): array
{
$images = array();
foreach ($this->trImages as $image) {
if ($image instanceof TrImage && $image->isActif()) {
$images[] = $image;
}
}
usort($images, static function (TrImage $a, TrImage $b): int {
if ((bool) $a->isPrincipale() !== (bool) $b->isPrincipale()) {
return $a->isPrincipale() ? -1 : 1;
}
$ordreCompare = ((int) $a->getOrdre()) <=> ((int) $b->getOrdre());
if (0 !== $ordreCompare) {
return $ordreCompare;
}
return ((int) $a->getId()) <=> ((int) $b->getId());
});
$payload = array();
foreach (array_slice($images, 0, TrImage::MAX_IMAGES_PER_PRODUCT) as $image) {
$payload[] = $image->toApiArray();
}
if ([] === $payload && $this->getIMAGE()) {
$payload[] = array(
'id' => null,
'url' => $this->buildLegacyImageUrl($this->getIMAGE()),
'imagePath' => $this->getIMAGE(),
'imageName' => $this->getIMAGE(),
'ordre' => 1,
'isPrincipale' => true,
);
}
return $payload;
}
private function buildLegacyImageUrl(string $image): string
{
$image = trim($image);
if (preg_match('/^https?:\/\//i', $image)) {
return $image;
}
if (0 === strpos($image, '/')) {
return $image;
}
if (0 === strpos($image, 'uploads/')) {
return '/'.$image;
}
return '/uploads/brochures/'.$image;
}
}