src/Entity/TrImage.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\TrImageRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\Serializer\Annotation\Groups;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  8. /**
  9.  * @ORM\Entity(repositoryClass=TrImageRepository::class)
  10.  * @ORM\HasLifecycleCallbacks
  11.  */
  12. class TrImage
  13. {
  14.     public const MAX_IMAGES_PER_PRODUCT = 4;
  15.     /**
  16.      * @ORM\Id
  17.      * @ORM\GeneratedValue
  18.      * @ORM\Column(type="integer")
  19.      * @Groups({"produit:read","pm:read"})
  20.      */
  21.     private $id;
  22.     /**
  23.      * @ORM\ManyToOne(targetEntity=TrProduits::class, inversedBy="trImages")
  24.      * @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
  25.      */
  26.     private $produit;
  27.     /**
  28.      * @ORM\ManyToOne(targetEntity=TrProduitsMasse::class, inversedBy="trImages")
  29.      * @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
  30.      */
  31.     private $produitMasse;
  32.     /**
  33.      * @ORM\Column(type="string", length=255)
  34.      * @Groups({"produit:read","pm:read"})
  35.      */
  36.     private $imagePath;
  37.     /**
  38.      * @ORM\Column(type="string", length=255, nullable=true)
  39.      * @Groups({"produit:read","pm:read"})
  40.      */
  41.     private $imageName;
  42.     /**
  43.      * @ORM\Column(type="integer", options={"default": 1})
  44.      * @Groups({"produit:read","pm:read"})
  45.      */
  46.     private $ordre = 1;
  47.     /**
  48.      * @ORM\Column(type="boolean", options={"default": false})
  49.      * @Groups({"produit:read","pm:read"})
  50.      */
  51.     private $isPrincipale = false;
  52.     /**
  53.      * @ORM\Column(type="boolean", options={"default": true})
  54.      */
  55.     private $actif = true;
  56.     /**
  57.      * @ORM\Column(type="datetime")
  58.      */
  59.     private $createdAt;
  60.     /**
  61.      * @ORM\Column(type="datetime")
  62.      */
  63.     private $updatedAt;
  64.     public function getId(): ?int
  65.     {
  66.         return $this->id;
  67.     }
  68.     public function getProduit(): ?TrProduits
  69.     {
  70.         return $this->produit;
  71.     }
  72.     public function setProduit(?TrProduits $produit): self
  73.     {
  74.         $this->produit = $produit;
  75.         return $this;
  76.     }
  77.     public function getProduitMasse(): ?TrProduitsMasse
  78.     {
  79.         return $this->produitMasse;
  80.     }
  81.     public function setProduitMasse(?TrProduitsMasse $produitMasse): self
  82.     {
  83.         $this->produitMasse = $produitMasse;
  84.         return $this;
  85.     }
  86.     public function getImagePath(): ?string
  87.     {
  88.         return $this->imagePath;
  89.     }
  90.     public function setImagePath(string $imagePath): self
  91.     {
  92.         $this->imagePath = ltrim($imagePath, '/');
  93.         return $this;
  94.     }
  95.     public function getImageName(): ?string
  96.     {
  97.         return $this->imageName;
  98.     }
  99.     public function setImageName(?string $imageName): self
  100.     {
  101.         $this->imageName = $imageName;
  102.         return $this;
  103.     }
  104.     public function getOrdre(): ?int
  105.     {
  106.         return $this->ordre;
  107.     }
  108.     public function setOrdre(int $ordre): self
  109.     {
  110.         $this->ordre = max(1, min(self::MAX_IMAGES_PER_PRODUCT, $ordre));
  111.         return $this;
  112.     }
  113.     public function isPrincipale(): ?bool
  114.     {
  115.         return $this->isPrincipale;
  116.     }
  117.     public function getIsPrincipale(): ?bool
  118.     {
  119.         return $this->isPrincipale();
  120.     }
  121.     public function setIsPrincipale(bool $isPrincipale): self
  122.     {
  123.         $this->isPrincipale = $isPrincipale;
  124.         return $this;
  125.     }
  126.     public function isActif(): ?bool
  127.     {
  128.         return $this->actif;
  129.     }
  130.     public function getActif(): ?bool
  131.     {
  132.         return $this->isActif();
  133.     }
  134.     public function setActif(bool $actif): self
  135.     {
  136.         $this->actif = $actif;
  137.         return $this;
  138.     }
  139.     public function getCreatedAt(): ?\DateTimeInterface
  140.     {
  141.         return $this->createdAt;
  142.     }
  143.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  144.     {
  145.         $this->createdAt = $createdAt;
  146.         return $this;
  147.     }
  148.     public function getUpdatedAt(): ?\DateTimeInterface
  149.     {
  150.         return $this->updatedAt;
  151.     }
  152.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  153.     {
  154.         $this->updatedAt = $updatedAt;
  155.         return $this;
  156.     }
  157.     /**
  158.      * @Groups({"produit:read","pm:read"})
  159.      */
  160.     public function getUrl(): ?string
  161.     {
  162.         return $this->getPublicUrl();
  163.     }
  164.     public function getPublicUrl(): ?string
  165.     {
  166.         $path = trim((string) $this->imagePath);
  167.         if ('' === $path) {
  168.             return null;
  169.         }
  170.         if (preg_match('/^https?:\/\//i', $path)) {
  171.             return $path;
  172.         }
  173.         return '/'.ltrim($path, '/');
  174.     }
  175.     public function toApiArray(): array
  176.     {
  177.         return array(
  178.             'id' => $this->getId(),
  179.             'url' => $this->getPublicUrl(),
  180.             'imagePath' => $this->getImagePath(),
  181.             'imageName' => $this->getImageName(),
  182.             'ordre' => $this->getOrdre(),
  183.             'isPrincipale' => (bool) $this->isPrincipale(),
  184.         );
  185.     }
  186.     /**
  187.      * @ORM\PrePersist
  188.      */
  189.     public function setCreatedTimestamps(): void
  190.     {
  191.         $now = new \DateTimeImmutable();
  192.         if (null === $this->createdAt) {
  193.             $this->createdAt = $now;
  194.         }
  195.         $this->updatedAt = $now;
  196.     }
  197.     /**
  198.      * @ORM\PreUpdate
  199.      */
  200.     public function setUpdatedTimestamp(): void
  201.     {
  202.         $this->updatedAt = new \DateTimeImmutable();
  203.     }
  204.     /**
  205.      * @Assert\Callback
  206.      */
  207.     public function validateOwner(ExecutionContextInterface $context, $payload): void
  208.     {
  209.         $hasProduit = null !== $this->produit;
  210.         $hasProduitMasse = null !== $this->produitMasse;
  211.         if ($hasProduit === $hasProduitMasse) {
  212.             $context->buildViolation('Une image doit etre liee soit a un produit, soit a un produit de masse.')
  213.                 ->atPath('produit')
  214.                 ->addViolation();
  215.         }
  216.     }
  217. }