vendor/bitbag/wishlist-plugin/src/Entity/Wishlist.php line 21

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file was created by developers working at BitBag
  4.  * Do you need more information about us and what we do? Visit our https://bitbag.io website!
  5.  * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
  6. */
  7. declare(strict_types=1);
  8. namespace BitBag\SyliusWishlistPlugin\Entity;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Sylius\Component\Channel\Model\ChannelInterface;
  12. use Sylius\Component\Core\Model\ProductInterface;
  13. use Sylius\Component\Core\Model\ProductVariantInterface;
  14. use Sylius\Component\Core\Model\ShopUserInterface;
  15. use Sylius\Component\Resource\Model\TimestampableTrait;
  16. class Wishlist implements WishlistInterface
  17. {
  18.     use TimestampableTrait;
  19.     protected ?int $id null;
  20.     protected ?string $name;
  21.     /** @var Collection|WishlistProductInterface[] */
  22.     protected $wishlistProducts;
  23.     protected ?ShopUserInterface $shopUser null;
  24.     /** @var WishlistTokenInterface|null */
  25.     protected $token;
  26.     protected ?ChannelInterface $channel;
  27.     public function __construct()
  28.     {
  29.         $this->wishlistProducts = new ArrayCollection();
  30.         $this->token = new WishlistToken();
  31.         $this->id null;
  32.         $this->createdAt = new \DateTime();
  33.     }
  34.     public function getId(): ?int
  35.     {
  36.         return $this->id;
  37.     }
  38.     public function getName(): ?string
  39.     {
  40.         return $this->name;
  41.     }
  42.     public function setName(?string $name): void
  43.     {
  44.         $this->name $name;
  45.     }
  46.     public function getProducts(): Collection
  47.     {
  48.         $products = [];
  49.         foreach ($this->wishlistProducts as $wishlistProduct) {
  50.             $products[] = $wishlistProduct->getProduct();
  51.         }
  52.         return new ArrayCollection($products);
  53.     }
  54.     /**
  55.      * @return Collection<int,ProductVariantInterface|null>
  56.      */
  57.     public function getProductVariants(): Collection
  58.     {
  59.         $variants = [];
  60.         foreach ($this->wishlistProducts as $wishlistProduct) {
  61.             $variants[] = $wishlistProduct->getVariant();
  62.         }
  63.         return new ArrayCollection($variants);
  64.     }
  65.     public function hasProductVariant(ProductVariantInterface $productVariant): bool
  66.     {
  67.         foreach ($this->wishlistProducts as $wishlistProduct) {
  68.             if ($productVariant === $wishlistProduct->getVariant()) {
  69.                 return true;
  70.             }
  71.         }
  72.         return false;
  73.     }
  74.     public function getWishlistProducts(): Collection
  75.     {
  76.         return $this->wishlistProducts;
  77.     }
  78.     public function hasProduct(ProductInterface $product): bool
  79.     {
  80.         foreach ($this->wishlistProducts as $wishlistProduct) {
  81.             if ($product === $wishlistProduct->getProduct()) {
  82.                 return true;
  83.             }
  84.         }
  85.         return false;
  86.     }
  87.     public function setWishlistProducts(Collection $wishlistProducts): void
  88.     {
  89.         $this->wishlistProducts $wishlistProducts;
  90.     }
  91.     public function hasWishlistProduct(WishlistProductInterface $wishlistProduct): bool
  92.     {
  93.         return $this->wishlistProducts->contains($wishlistProduct);
  94.     }
  95.     public function addWishlistProduct(WishlistProductInterface $wishlistProduct): void
  96.     {
  97.         /** @var ProductVariantInterface $variant */
  98.         $variant $wishlistProduct->getVariant();
  99.         if (!$this->hasProductVariant($variant)) {
  100.             $wishlistProduct->setWishlist($this);
  101.             $this->wishlistProducts->add($wishlistProduct);
  102.         }
  103.     }
  104.     public function getShopUser(): ?ShopUserInterface
  105.     {
  106.         return $this->shopUser;
  107.     }
  108.     public function setShopUser(ShopUserInterface $shopUser): void
  109.     {
  110.         $this->shopUser $shopUser;
  111.     }
  112.     public function getToken(): string
  113.     {
  114.         return (string) $this->token;
  115.     }
  116.     public function setToken(string $token): void
  117.     {
  118.         $this->token = new WishlistToken($token);
  119.     }
  120.     public function removeProduct(WishlistProductInterface $product): self
  121.     {
  122.         if ($this->hasWishlistProduct($product)) {
  123.             $this->wishlistProducts->removeElement($product);
  124.         }
  125.         return $this;
  126.     }
  127.     public function removeProductVariant(ProductVariantInterface $variant): self
  128.     {
  129.         foreach ($this->wishlistProducts as $wishlistProduct) {
  130.             if ($wishlistProduct->getVariant() === $variant) {
  131.                 $this->wishlistProducts->removeElement($wishlistProduct);
  132.             }
  133.         }
  134.         return $this;
  135.     }
  136.     public function clear(): void
  137.     {
  138.         $this->wishlistProducts = new ArrayCollection();
  139.     }
  140.     public function getChannel(): ?ChannelInterface
  141.     {
  142.         return $this->channel;
  143.     }
  144.     public function setChannel(?ChannelInterface $channel): void
  145.     {
  146.         $this->channel $channel;
  147.     }
  148. }