src/Entity/CartItem.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CartItemRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use DateTimeImmutable;
  6. #[ORM\Entity(repositoryClassCartItemRepository::class)]
  7. #[ORM\Table(name'`cart_item`')]
  8. class CartItem
  9. {
  10.     #[ORM\Id]
  11.     #[ORM\GeneratedValue]
  12.     #[ORM\Column]
  13.     private ?int $id null;
  14.     #[ORM\ManyToOne(targetEntityCart::class, inversedBy'items')]
  15.     #[ORM\JoinColumn(nullablefalse)]
  16.     private ?Cart $cart null;
  17.     #[ORM\ManyToOne(targetEntityProduct::class)]
  18.     #[ORM\JoinColumn(nullablefalse)]
  19.     private ?Product $product null;
  20.     #[ORM\Column]
  21.     private ?int $quantity null;
  22.     #[ORM\Column(type'decimal'precision10scale2)]
  23.     private ?string $unitPrice null;
  24.     #[ORM\Column(type'decimal'precision10scale2)]
  25.     private ?string $totalPrice null;
  26.     #[ORM\Column(nullabletrue)]
  27.     private ?DateTimeImmutable $addedAt null;
  28.     public function __construct()
  29.     {
  30.         $this->addedAt = new DateTimeImmutable();
  31.     }
  32.     public function getId(): ?int
  33.     {
  34.         return $this->id;
  35.     }
  36.     public function getCart(): ?Cart
  37.     {
  38.         return $this->cart;
  39.     }
  40.     public function setCart(?Cart $cart): self
  41.     {
  42.         $this->cart $cart;
  43.         return $this;
  44.     }
  45.     public function getProduct(): ?Product
  46.     {
  47.         return $this->product;
  48.     }
  49.     public function setProduct(?Product $product): self
  50.     {
  51.         $this->product $product;
  52.         if ($product) {
  53.             $this->unitPrice $product->getPrice();
  54.             $this->updateTotalPrice();
  55.         }
  56.         return $this;
  57.     }
  58.     public function getQuantity(): ?int
  59.     {
  60.         return $this->quantity;
  61.     }
  62.     public function setQuantity(int $quantity): self
  63.     {
  64.         $this->quantity max(1$quantity);
  65.         $this->updateTotalPrice();
  66.         return $this;
  67.     }
  68.     public function getUnitPrice(): ?string
  69.     {
  70.         return $this->unitPrice;
  71.     }
  72.     public function setUnitPrice(string $unitPrice): self
  73.     {
  74.         $this->unitPrice $unitPrice;
  75.         $this->updateTotalPrice();
  76.         return $this;
  77.     }
  78.     public function getTotalPrice(): ?string
  79.     {
  80.         return $this->totalPrice;
  81.     }
  82.     public function setTotalPrice(string $totalPrice): self
  83.     {
  84.         $this->totalPrice $totalPrice;
  85.         return $this;
  86.     }
  87.     public function getAddedAt(): ?DateTimeImmutable
  88.     {
  89.         return $this->addedAt;
  90.     }
  91.     public function setAddedAt(DateTimeImmutable $addedAt): self
  92.     {
  93.         $this->addedAt $addedAt;
  94.         return $this;
  95.     }
  96.     private function updateTotalPrice(): void
  97.     {
  98.         if ($this->unitPrice && $this->quantity) {
  99.             $this->totalPrice number_format($this->unitPrice $this->quantity2'.''');
  100.         }
  101.     }
  102.     public function incrementQuantity(int $amount 1): self
  103.     {
  104.         $this->quantity += $amount;
  105.         $this->updateTotalPrice();
  106.         return $this;
  107.     }
  108.     public function decrementQuantity(int $amount 1): self
  109.     {
  110.         $this->quantity max(1$this->quantity $amount);
  111.         $this->updateTotalPrice();
  112.         return $this;
  113.     }
  114. }