src/Entity/ProductRanking.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ProductRankingRepository;
  4. use Doctrine\DBAL\Types\Types;
  5. use Doctrine\ORM\Mapping as ORM;
  6. #[ORM\Entity(repositoryClassProductRankingRepository::class)]
  7. #[ORM\Table(name'product_rankings')]
  8. #[ORM\UniqueConstraint(name'unique_product_category_ranking'columns: ['product_id''category_id'])]
  9. class ProductRanking
  10. {
  11.     #[ORM\Id]
  12.     #[ORM\GeneratedValue]
  13.     #[ORM\Column]
  14.     private ?int $id null;
  15.     #[ORM\ManyToOne(targetEntityProduct::class, inversedBy'rankings')]
  16.     #[ORM\JoinColumn(nullablefalse)]
  17.     private ?Product $product null;
  18.     #[ORM\ManyToOne(targetEntityCategory::class)]
  19.     #[ORM\JoinColumn(nullablefalse)]
  20.     private ?Category $category null;
  21.     #[ORM\Column(typeTypes::INTEGER)]
  22.     private ?int $rank null;
  23.     #[ORM\Column(typeTypes::INTEGER)]
  24.     private ?int $previousRank null;
  25.     #[ORM\Column(typeTypes::FLOAT)]
  26.     private ?float $score null;
  27.     #[ORM\Column(typeTypes::JSON)]
  28.     private ?array $rankingFactors null;
  29.     #[ORM\Column(typeTypes::DATETIME_IMMUTABLE)]
  30.     private ?\DateTimeImmutable $calculatedAt null;
  31.     #[ORM\Column(typeTypes::DATETIME_IMMUTABLE)]
  32.     private ?\DateTimeImmutable $updatedAt null;
  33.     public function __construct()
  34.     {
  35.         $this->calculatedAt = new \DateTimeImmutable();
  36.         $this->updatedAt = new \DateTimeImmutable();
  37.         $this->rankingFactors = [];
  38.     }
  39.     public function getId(): ?int
  40.     {
  41.         return $this->id;
  42.     }
  43.     public function getProduct(): ?Product
  44.     {
  45.         return $this->product;
  46.     }
  47.     public function setProduct(?Product $product): static
  48.     {
  49.         $this->product $product;
  50.         return $this;
  51.     }
  52.     public function getCategory(): ?Category
  53.     {
  54.         return $this->category;
  55.     }
  56.     public function setCategory(?Category $category): static
  57.     {
  58.         $this->category $category;
  59.         return $this;
  60.     }
  61.     public function getRank(): ?int
  62.     {
  63.         return $this->rank;
  64.     }
  65.     public function setRank(int $rank): static
  66.     {
  67.         $this->rank $rank;
  68.         return $this;
  69.     }
  70.     public function getPreviousRank(): ?int
  71.     {
  72.         return $this->previousRank;
  73.     }
  74.     public function setPreviousRank(?int $previousRank): static
  75.     {
  76.         $this->previousRank $previousRank;
  77.         return $this;
  78.     }
  79.     public function getScore(): ?float
  80.     {
  81.         return $this->score;
  82.     }
  83.     public function setScore(float $score): static
  84.     {
  85.         $this->score $score;
  86.         return $this;
  87.     }
  88.     public function getRankingFactors(): ?array
  89.     {
  90.         return $this->rankingFactors;
  91.     }
  92.     public function setRankingFactors(array $rankingFactors): static
  93.     {
  94.         $this->rankingFactors $rankingFactors;
  95.         return $this;
  96.     }
  97.     public function addRankingFactor(string $factorfloat $value): static
  98.     {
  99.         $this->rankingFactors[$factor] = $value;
  100.         return $this;
  101.     }
  102.     public function getCalculatedAt(): ?\DateTimeImmutable
  103.     {
  104.         return $this->calculatedAt;
  105.     }
  106.     public function setCalculatedAt(\DateTimeImmutable $calculatedAt): static
  107.     {
  108.         $this->calculatedAt $calculatedAt;
  109.         return $this;
  110.     }
  111.     public function getUpdatedAt(): ?\DateTimeImmutable
  112.     {
  113.         return $this->updatedAt;
  114.     }
  115.     public function setUpdatedAt(\DateTimeImmutable $updatedAt): static
  116.     {
  117.         $this->updatedAt $updatedAt;
  118.         return $this;
  119.     }
  120.     /**
  121.      * Calculer le changement de rang
  122.      */
  123.     public function getRankChange(): ?int
  124.     {
  125.         if ($this->previousRank === null) {
  126.             return null;
  127.         }
  128.         return $this->previousRank $this->rank;
  129.     }
  130.     /**
  131.      * Obtenir le symbole de changement de rang
  132.      */
  133.     public function getRankChangeSymbol(): string
  134.     {
  135.         $change $this->getRankChange();
  136.         if ($change === null) {
  137.             return 'new';
  138.         } elseif ($change 0) {
  139.             return 'up';
  140.         } elseif ($change 0) {
  141.             return 'down';
  142.         } else {
  143.             return 'same';
  144.         }
  145.     }
  146.     /**
  147.      * Obtenir la couleur du changement de rang
  148.      */
  149.     public function getRankChangeColor(): string
  150.     {
  151.         $change $this->getRankChange();
  152.         if ($change === null) {
  153.             return 'info';
  154.         } elseif ($change 0) {
  155.             return 'success';
  156.         } elseif ($change 0) {
  157.             return 'danger';
  158.         } else {
  159.             return 'secondary';
  160.         }
  161.     }
  162. }