<?php
namespace App\Entity;
use App\Service\Helper\DateTimeHelper;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Exception;
/**
* Class CartElement
*
* @ORM\Table(name="carts__elements")
* @ORM\Entity(repositoryClass="App\Repository\CartElementRepository")
*/
class CartElement
{
use IdTrait;
/**
* @ORM\Column(name="created_at", type="datetime", nullable=false)
*/
private DateTime $createdAt;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Cart", cascade={"persist"})
* @ORM\JoinColumn(name="cart_id", referencedColumnName="id", onDelete="CASCADE")
*/
private Cart $cart;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Item")
* @ORM\JoinColumn(name="item_id", referencedColumnName="id", onDelete="CASCADE")
*/
private Item $item;
/**
* @ORM\Column(name="quantity", type="integer")
*/
private int $quantity;
/**
* @ORM\Column(name="price", type="integer")
*/
private int $price;
/**
* @throws Exception
*/
public function __construct(Cart $cart, Item $item, int $quantity, int $price)
{
$this->cart = $cart;
$this->item = $item;
$this->quantity = $quantity;
$this->price = $price;
$this->createdAt = DateTimeHelper::getNowDateTime();
}
public function getCart(): Cart
{
return $this->cart;
}
public function getItem(): Item
{
return $this->item;
}
public function getQuantity(): int
{
return $this->quantity;
}
public function getPrice(): int
{
return $this->price;
}
public function setQuantity(int $quantity): CartElement
{
$this->quantity = $quantity;
return $this;
}
public function getStringInfo(): string
{
return $this->getItem()
? $this->getItem()->getProduct()->getDescription()
. ' ' . $this->getItem()->getProduct()->getBrandName()
. ' / ' . mb_strtoupper($this->getItem()->getProduct()->getArticleSearch())
: ''
;
}
}