src/Entity/Cart.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CartRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. #[ORM\Entity(repositoryClassCartRepository::class)]
  8. class Cart
  9. {
  10.     #[ORM\Id]
  11.     #[ORM\GeneratedValue]
  12.     #[ORM\Column(type'integer')]
  13.     private $id;
  14.     #[ORM\OneToMany(mappedBy'cart'targetEntityCartItem::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  15.     private $items;
  16.     #[ORM\ManyToOne(targetEntityOrder::class, inversedBy'carts')]
  17.     #[ORM\JoinColumn(nullablefalse)]
  18.     private $order;
  19.     public function __construct()
  20.     {
  21.         $this->items = new ArrayCollection();
  22.     }
  23.     public function getId(): ?int
  24.     {
  25.         return $this->id;
  26.     }
  27.     /**
  28.      * @return Collection<int, CartItem>
  29.      */
  30.     public function getItems(): Collection
  31.     {
  32.         return $this->items;
  33.     }
  34.     public function addItem(CartItem $item): self
  35.     {
  36.         if (!$this->items->contains($item)) {
  37.             $this->items[] = $item;
  38.             $item->setCart($this);
  39.         }
  40.         return $this;
  41.     }
  42.     public function removeItem(CartItem $item): self
  43.     {
  44.         if ($this->items->removeElement($item)) {
  45.             // set the owning side to null (unless already changed)
  46.             if ($item->getCart() === $this) {
  47.                 $item->setCart(null);
  48.             }
  49.         }
  50.         return $this;
  51.     }
  52.     public function getOrder(): ?Order
  53.     {
  54.         return $this->order;
  55.     }
  56.     public function setOrder(?Order $order): self
  57.     {
  58.         $this->order $order;
  59.         return $this;
  60.     }
  61.     /**
  62.      * @param string|null $type
  63.      * @return Collection<int, CartItem>
  64.      */
  65.     public function getFilteredItems(?string $type CartItem::class): Collection
  66.     {
  67.         return $this->getItems()->filter(fn($item) => $item instanceof $type);
  68.     }
  69.     /**
  70.      * @return Collection|CartItemShopInterface[]
  71.      */
  72.     public function getShopItems(): Collection
  73.     {
  74.         return $this->getItems()->filter(fn($item) => $item instanceof CartItemShopInterface);
  75.     }
  76.     /**
  77.      * @return Collection|CartItemPurchase[]
  78.      */
  79.     public function getPurchaseItems(): Collection
  80.     {
  81.         return $this->getItems()->filter(fn($item) => $item instanceof CartItemPurchase);
  82.     }
  83.     /**
  84.      * @return Collection|CartItemRental[]
  85.      */
  86.     public function getRentalItems(): Collection
  87.     {
  88.         return $this->getItems()->filter(fn($item) => $item instanceof CartItemRental);
  89.     }
  90.     /**
  91.      * @param null|bool $aids
  92.      * @return Collection|CartItemDiscount[]
  93.      */
  94.     public function getDiscounts(?bool $aids null): Collection
  95.     {
  96.         $discounts $this->getItems()->filter(fn($item) => $item instanceof CartItemDiscount);
  97.         if ($aids === false) {
  98.             $discounts $discounts->filter(fn(CartItemDiscount $discount) => $discount->getDiscount() === null);
  99.         } elseif ($aids === true) {
  100.             $discounts $discounts->filter(fn(CartItemDiscount $discount) => $discount->getDiscount() !== null);
  101.         }
  102.         return $discounts;
  103.     }
  104.     /**
  105.      * @return Collection|CartItemLesson[]
  106.      */
  107.     public function getLessons(): Collection
  108.     {
  109.         return $this->getItems()->filter(fn($item) => $item instanceof CartItemLesson);
  110.     }
  111.     /**
  112.      * @return Collection|CartItemDonation[]
  113.      */
  114.     public function getDonations(): Collection
  115.     {
  116.         return $this->getItems()->filter(fn($item) => $item instanceof CartItemDonation);
  117.     }
  118.     public function getEntryFees(): ?CartItemEntryFees
  119.     {
  120.         $cartItem $this->getItems()->filter(fn($item) => $item instanceof CartItemEntryFees)->first();
  121.         return $cartItem === false null $cartItem;
  122.     }
  123.     public function getFamilyDiscount(): ?CartItemFamilyDiscout
  124.     {
  125.         $cartItem $this->getItems()->filter(fn($item) => $item instanceof CartItemFamilyDiscout)->first();
  126.         return $cartItem === false null $cartItem;
  127.     }
  128.     /**
  129.      * @return Collection|CartItemDebt[]
  130.      */
  131.     public function getDebts(): Collection
  132.     {
  133.         return $this->getItems()->filter(fn($item) => $item instanceof CartItemDebt);
  134.     }
  135.     public function getReceiptCartItems(): Collection
  136.     {
  137.         $items $this->getLessons();
  138.         if ($entryFees $this->getEntryFees()) {
  139.             $items->add($entryFees);
  140.         }
  141.         if ($familyDiscout $this->getFamilyDiscount()) {
  142.             $items->add($familyDiscout);
  143.         }
  144.         foreach ($this->getDiscounts(false) as $discount) {
  145.             $items->add($discount);
  146.         }
  147.         return $items;
  148.     }
  149. }