src/Entity/Shop/Product/Product.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Shop\Product;
  3. use App\Entity\BaseEntity;
  4. use App\Entity\Shop\Cart\CartItem;
  5. use App\Entity\Shop\Order\Discount;
  6. use App\Entity\Shop\Order\OrderItem;
  7. use App\Entity\Shop\Shop\Review;
  8. use App\Entity\Shop\Shop\Shop;
  9. use App\Repository\Shop\Product\ProductRepository;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\DBAL\Types\Types;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
  15. #[ORM\Entity(repositoryClassProductRepository::class)]
  16. class Product extends BaseEntity
  17. {
  18.     #[ORM\Id]
  19.     #[ORM\Column(type'guid'uniquetrue)]
  20.     #[ORM\GeneratedValue(strategy'CUSTOM')]
  21.     #[ORM\CustomIdGenerator(class: UuidGenerator::class)]
  22.     private ?string $id;
  23.     #[ORM\Column(typeTypes::BIGINTnullabletrue)]
  24.     private ?string $stock null;
  25.     #[ORM\OneToMany(mappedBy'product'targetEntityProductPrice::class, cascade: ['persist'], orphanRemovaltrue)]
  26.     private Collection $productPrices;
  27.     #[ORM\OneToMany(mappedBy'product'targetEntityProductAttribute::class, cascade: ['persist'], orphanRemovaltrue)]
  28.     private Collection $productAttributes;
  29.     #[ORM\Column(length255)]
  30.     private ?string $name null;
  31.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  32.     private ?string $description null;
  33.     #[ORM\Column(typeTypes::BIGINT)]
  34.     private ?string $price null;
  35.     #[ORM\Column(length255nullabletrue)]
  36.     private ?string $imageUrl null;
  37.     #[ORM\ManyToOne(inversedBy'products')]
  38.     #[ORM\JoinColumn(nullablefalse)]
  39.     private ?Shop $shop null;
  40.     #[ORM\OneToMany(mappedBy'product'targetEntityDiscount::class)]
  41.     private Collection $discounts;
  42.     #[ORM\OneToMany(mappedBy'product'targetEntityProductImage::class, cascade: ['persist'], orphanRemovaltrue)]
  43.     private Collection $productImages;
  44.     #[ORM\OneToMany(mappedBy'product'targetEntityOrderItem::class)]
  45.     private Collection $orderItems;
  46.     #[ORM\OneToMany(mappedBy'product'targetEntityCartItem::class)]
  47.     private Collection $cartItems;
  48.     #[ORM\OneToMany(mappedBy'product'targetEntityReview::class)]
  49.     private Collection $reviews;
  50.     #[ORM\Column(length255nullabletrue)]
  51.     private ?string $subName null;
  52.     #[ORM\ManyToMany(targetEntityProductCategory::class, mappedBy'products'cascade: ['persist'])]
  53.     private Collection $productCategories;
  54.     #[ORM\ManyToMany(targetEntityProductTag::class, mappedBy'products'cascade: ['persist'])]
  55.     private Collection $productTags;
  56.     #[ORM\OneToMany(mappedBy'product'targetEntityProductReview::class)]
  57.     private Collection $productReviews;
  58.     #[ORM\Column(nullabletrue)]
  59.     private ?bool $isSelling null;
  60.     public function __construct()
  61.     {
  62.         parent::__construct();
  63.         $this->setPrice(0);
  64.         $this->productAttributes = new ArrayCollection();
  65.         $this->discounts = new ArrayCollection();
  66.         $this->productImages = new ArrayCollection();
  67.         $this->orderItems = new ArrayCollection();
  68.         $this->cartItems = new ArrayCollection();
  69.         $this->reviews = new ArrayCollection();
  70.         $this->productPrices = new ArrayCollection();
  71.         $this->productCategories = new ArrayCollection();
  72.         $this->productTags = new ArrayCollection();
  73.         $this->productReviews = new ArrayCollection();
  74.     }
  75.     public function __toString()
  76.     {
  77.         return $this->getName();
  78.     }
  79.     public function getMinPrice(): ?int
  80.     {
  81.         if (!$this->isVariablePriced()) {
  82.             return $this->getPrice(); // قیمت ساده
  83.         }
  84.         $prices $this->getProductPrices()->map(fn($p) => $p->getPrice())->toArray();
  85.         return !empty($prices) ? min($prices) : null;
  86.     }
  87.     public function isVariablePriced(): bool
  88.     {
  89.         return !$this->productPrices->isEmpty();
  90.     }
  91.     public function getDisplayPrice(): ?int
  92.     {
  93.         return $this->isVariablePriced()
  94.             ? $this->getMinPrice()
  95.             : $this->getPrice();
  96.     }
  97.     public function getId(): ?string
  98.     {
  99.         return $this->id;
  100.     }
  101.     /**
  102.      * @return Collection<int, ProductAttribute>
  103.      */
  104.     public function getProductAttributes(): Collection
  105.     {
  106.         return $this->productAttributes;
  107.     }
  108.     public function addProductAttribute(ProductAttribute $productAttribute): static
  109.     {
  110.         if (!$this->productAttributes->contains($productAttribute)) {
  111.             $this->productAttributes->add($productAttribute);
  112.             $productAttribute->setProduct($this);
  113.         }
  114.         return $this;
  115.     }
  116.     public function removeProductAttribute(ProductAttribute $productAttribute): static
  117.     {
  118.         if ($this->productAttributes->removeElement($productAttribute)) {
  119.             // set the owning side to null (unless already changed)
  120.             if ($productAttribute->getProduct() === $this) {
  121.                 $productAttribute->setProduct(null);
  122.             }
  123.         }
  124.         return $this;
  125.     }
  126.     public function getName(): ?string
  127.     {
  128.         return $this->name;
  129.     }
  130.     public function setName(string $name): static
  131.     {
  132.         $this->name $name;
  133.         return $this;
  134.     }
  135.     public function getDescription(): ?string
  136.     {
  137.         return $this->description;
  138.     }
  139.     public function setDescription(?string $description): static
  140.     {
  141.         $this->description $description;
  142.         return $this;
  143.     }
  144.     public function getPrice(): ?string
  145.     {
  146.         return $this->price;
  147.     }
  148.     public function setPrice(string $price): static
  149.     {
  150.         $this->price $price;
  151.         return $this;
  152.     }
  153.     public function getStock(): ?string
  154.     {
  155.         return $this->stock;
  156.     }
  157.     public function setStock(string $stock): static
  158.     {
  159.         $this->stock $stock;
  160.         return $this;
  161.     }
  162.     public function getImageUrl(): ?string
  163.     {
  164.         return $this->imageUrl;
  165.     }
  166.     public function setImageUrl(?string $imageUrl): static
  167.     {
  168.         $this->imageUrl $imageUrl;
  169.         return $this;
  170.     }
  171.     public function getShop(): ?Shop
  172.     {
  173.         return $this->shop;
  174.     }
  175.     public function setShop(?Shop $shop): static
  176.     {
  177.         $this->shop $shop;
  178.         return $this;
  179.     }
  180.     /**
  181.      * @return Collection<int, Discount>
  182.      */
  183.     public function getDiscounts(): Collection
  184.     {
  185.         return $this->discounts;
  186.     }
  187.     public function addDiscount(Discount $discount): static
  188.     {
  189.         if (!$this->discounts->contains($discount)) {
  190.             $this->discounts->add($discount);
  191.             $discount->setProduct($this);
  192.         }
  193.         return $this;
  194.     }
  195.     public function removeDiscount(Discount $discount): static
  196.     {
  197.         if ($this->discounts->removeElement($discount)) {
  198.             // set the owning side to null (unless already changed)
  199.             if ($discount->getProduct() === $this) {
  200.                 $discount->setProduct(null);
  201.             }
  202.         }
  203.         return $this;
  204.     }
  205.     public function getCategory()
  206.     {
  207.         return $this->productCategories->first();
  208.     }
  209.     public function setCategory(?ProductCategory $category): static
  210.     {
  211.         $this->category $category;
  212.         return $this;
  213.     }
  214.     /**
  215.      * @return Collection<int, ProductImage>
  216.      */
  217.     public function getProductImages(): Collection
  218.     {
  219.         return $this->productImages;
  220.     }
  221.     public function addProductImage(ProductImage $productImage): static
  222.     {
  223.         if (!$this->productImages->contains($productImage)) {
  224.             $this->productImages->add($productImage);
  225.             $productImage->setProduct($this);
  226.         }
  227.         return $this;
  228.     }
  229.     public function removeProductImage(ProductImage $productImage): static
  230.     {
  231.         if ($this->productImages->removeElement($productImage)) {
  232.             // set the owning side to null (unless already changed)
  233.             if ($productImage->getProduct() === $this) {
  234.                 $productImage->setProduct(null);
  235.             }
  236.         }
  237.         return $this;
  238.     }
  239.     /**
  240.      * @return Collection<int, OrderItem>
  241.      */
  242.     public function getOrderItems(): Collection
  243.     {
  244.         return $this->orderItems;
  245.     }
  246.     public function addOrderItem(OrderItem $orderItem): static
  247.     {
  248.         if (!$this->orderItems->contains($orderItem)) {
  249.             $this->orderItems->add($orderItem);
  250.             $orderItem->setProduct($this);
  251.         }
  252.         return $this;
  253.     }
  254.     public function removeOrderItem(OrderItem $orderItem): static
  255.     {
  256.         if ($this->orderItems->removeElement($orderItem)) {
  257.             // set the owning side to null (unless already changed)
  258.             if ($orderItem->getProduct() === $this) {
  259.                 $orderItem->setProduct(null);
  260.             }
  261.         }
  262.         return $this;
  263.     }
  264.     /**
  265.      * @return Collection<int, CartItem>
  266.      */
  267.     public function getCartItems(): Collection
  268.     {
  269.         return $this->cartItems;
  270.     }
  271.     public function addCartItem(CartItem $cartItem): static
  272.     {
  273.         if (!$this->cartItems->contains($cartItem)) {
  274.             $this->cartItems->add($cartItem);
  275.             $cartItem->setProduct($this);
  276.         }
  277.         return $this;
  278.     }
  279.     public function removeCartItem(CartItem $cartItem): static
  280.     {
  281.         if ($this->cartItems->removeElement($cartItem)) {
  282.             // set the owning side to null (unless already changed)
  283.             if ($cartItem->getProduct() === $this) {
  284.                 $cartItem->setProduct(null);
  285.             }
  286.         }
  287.         return $this;
  288.     }
  289.     /**
  290.      * @return Collection<int, Review>
  291.      */
  292.     public function getReviews(): Collection
  293.     {
  294.         return $this->reviews;
  295.     }
  296.     public function addReview(Review $review): static
  297.     {
  298.         if (!$this->reviews->contains($review)) {
  299.             $this->reviews->add($review);
  300.             $review->setProduct($this);
  301.         }
  302.         return $this;
  303.     }
  304.     public function removeReview(Review $review): static
  305.     {
  306.         if ($this->reviews->removeElement($review)) {
  307.             // set the owning side to null (unless already changed)
  308.             if ($review->getProduct() === $this) {
  309.                 $review->setProduct(null);
  310.             }
  311.         }
  312.         return $this;
  313.     }
  314.     /**
  315.      * @return Collection<int, ProductPrice>
  316.      */
  317.     public function getProductPrices(): Collection
  318.     {
  319.         return $this->productPrices;
  320.     }
  321.     public function addProductPrice(ProductPrice $productPrice): static
  322.     {
  323.         if (!$this->productPrices->contains($productPrice)) {
  324.             $this->productPrices->add($productPrice);
  325.             $productPrice->setProduct($this);
  326.         }
  327.         return $this;
  328.     }
  329.     public function removeProductPrice(ProductPrice $productPrice): static
  330.     {
  331.         if ($this->productPrices->removeElement($productPrice)) {
  332.             // set the owning side to null (unless already changed)
  333.             if ($productPrice->getProduct() === $this) {
  334.                 $productPrice->setProduct(null);
  335.             }
  336.         }
  337.         return $this;
  338.     }
  339.     public function getSubName(): ?string
  340.     {
  341.         return $this->subName;
  342.     }
  343.     public function setSubName(?string $subName): static
  344.     {
  345.         $this->subName $subName;
  346.         return $this;
  347.     }
  348.     /**
  349.      * @return Collection<int, ProductCategory>
  350.      */
  351.     public function getProductCategories(): Collection
  352.     {
  353.         return $this->productCategories;
  354.     }
  355.     public function addProductCategory(ProductCategory $productCategory): static
  356.     {
  357.         if (!$this->productCategories->contains($productCategory)) {
  358.             $this->productCategories->add($productCategory);
  359.             $productCategory->addProduct($this);
  360.         }
  361.         return $this;
  362.     }
  363.     public function removeProductCategory(ProductCategory $productCategory): static
  364.     {
  365.         if ($this->productCategories->removeElement($productCategory)) {
  366.             $productCategory->removeProduct($this);
  367.         }
  368.         return $this;
  369.     }
  370.     /**
  371.      * @return Collection<int, ProductTag>
  372.      */
  373.     public function getProductTags(): Collection
  374.     {
  375.         return $this->productTags;
  376.     }
  377.     public function addProductTag(ProductTag $productTag): static
  378.     {
  379.         if (!$this->productTags->contains($productTag)) {
  380.             $this->productTags->add($productTag);
  381.             $productTag->addProduct($this);
  382.         }
  383.         return $this;
  384.     }
  385.     public function removeProductTag(ProductTag $productTag): static
  386.     {
  387.         if ($this->productTags->removeElement($productTag)) {
  388.             $productTag->removeProduct($this);
  389.         }
  390.         return $this;
  391.     }
  392.     /**
  393.      * @return Collection<int, ProductReview>
  394.      */
  395.     public function getProductReviews(): Collection
  396.     {
  397.         return $this->productReviews;
  398.     }
  399.     public function addProductReview(ProductReview $productReview): static
  400.     {
  401.         if (!$this->productReviews->contains($productReview)) {
  402.             $this->productReviews->add($productReview);
  403.             $productReview->setProduct($this);
  404.         }
  405.         return $this;
  406.     }
  407.     public function removeProductReview(ProductReview $productReview): static
  408.     {
  409.         if ($this->productReviews->removeElement($productReview)) {
  410.             // set the owning side to null (unless already changed)
  411.             if ($productReview->getProduct() === $this) {
  412.                 $productReview->setProduct(null);
  413.             }
  414.         }
  415.         return $this;
  416.     }
  417.     public function isIsSelling(): ?bool
  418.     {
  419.         return $this->isSelling;
  420.     }
  421.     public function setIsSelling(?bool $isSelling): static
  422.     {
  423.         $this->isSelling $isSelling;
  424.         return $this;
  425.     }
  426. }