<?phpnamespace App\Entity;use App\Repository\CartRepository;use Doctrine\Common\Collections\ArrayCollection;use Doctrine\Common\Collections\Collection;use Doctrine\ORM\Mapping as ORM;#[ORM\Entity(repositoryClass: CartRepository::class)]class Cart{ #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: 'integer')] private $id; #[ORM\OneToMany(mappedBy: 'cart', targetEntity: CartItem::class, cascade: ['persist', 'remove'], orphanRemoval: true)] private $items; #[ORM\ManyToOne(targetEntity: Order::class, inversedBy: 'carts')] #[ORM\JoinColumn(nullable: false)] private $order; public function __construct() { $this->items = new ArrayCollection(); } public function getId(): ?int { return $this->id; } /** * @return Collection<int, CartItem> */ public function getItems(): Collection { return $this->items; } public function addItem(CartItem $item): self { if (!$this->items->contains($item)) { $this->items[] = $item; $item->setCart($this); } return $this; } public function removeItem(CartItem $item): self { if ($this->items->removeElement($item)) { // set the owning side to null (unless already changed) if ($item->getCart() === $this) { $item->setCart(null); } } return $this; } public function getOrder(): ?Order { return $this->order; } public function setOrder(?Order $order): self { $this->order = $order; return $this; } /** * @param string|null $type * @return Collection<int, CartItem> */ public function getFilteredItems(?string $type = CartItem::class): Collection { return $this->getItems()->filter(fn($item) => $item instanceof $type); } /** * @return Collection|CartItemShopInterface[] */ public function getShopItems(): Collection { return $this->getItems()->filter(fn($item) => $item instanceof CartItemShopInterface); } /** * @return Collection|CartItemPurchase[] */ public function getPurchaseItems(): Collection { return $this->getItems()->filter(fn($item) => $item instanceof CartItemPurchase); } /** * @return Collection|CartItemRental[] */ public function getRentalItems(): Collection { return $this->getItems()->filter(fn($item) => $item instanceof CartItemRental); } /** * @param null|bool $aids * @return Collection|CartItemDiscount[] */ public function getDiscounts(?bool $aids = null): Collection { $discounts = $this->getItems()->filter(fn($item) => $item instanceof CartItemDiscount); if ($aids === false) { $discounts = $discounts->filter(fn(CartItemDiscount $discount) => $discount->getDiscount() === null); } elseif ($aids === true) { $discounts = $discounts->filter(fn(CartItemDiscount $discount) => $discount->getDiscount() !== null); } return $discounts; } /** * @return Collection|CartItemLesson[] */ public function getLessons(): Collection { return $this->getItems()->filter(fn($item) => $item instanceof CartItemLesson); } /** * @return Collection|CartItemDonation[] */ public function getDonations(): Collection { return $this->getItems()->filter(fn($item) => $item instanceof CartItemDonation); } public function getEntryFees(): ?CartItemEntryFees { $cartItem = $this->getItems()->filter(fn($item) => $item instanceof CartItemEntryFees)->first(); return $cartItem === false ? null : $cartItem; } public function getFamilyDiscount(): ?CartItemFamilyDiscout { $cartItem = $this->getItems()->filter(fn($item) => $item instanceof CartItemFamilyDiscout)->first(); return $cartItem === false ? null : $cartItem; } /** * @return Collection|CartItemDebt[] */ public function getDebts(): Collection { return $this->getItems()->filter(fn($item) => $item instanceof CartItemDebt); } public function getReceiptCartItems(): Collection { $items = $this->getLessons(); if ($entryFees = $this->getEntryFees()) { $items->add($entryFees); } if ($familyDiscout = $this->getFamilyDiscount()) { $items->add($familyDiscout); } foreach ($this->getDiscounts(false) as $discount) { $items->add($discount); } return $items; }}