modules/XC/ProductVariations/src/Model/VariationBase.php line 21

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace XC\ProductVariations\Model;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Ramsey\Uuid\Uuid;
  11. use XLite\Core\Database;
  12. /**
  13.  * @ORM\Entity
  14.  * @ORM\HasLifecycleCallbacks
  15.  * @ORM\Table  (name="product_variation_base")
  16.  */
  17. class VariationBase extends \XLite\Model\AEntity
  18. {
  19.     /**
  20.      * @ORM\Id
  21.      * @ORM\GeneratedValue (strategy="AUTO")
  22.      * @ORM\Column         (type="integer", options={ "unsigned": true })
  23.      */
  24.     protected ?int $id;
  25.     /**
  26.      * @ORM\Column         (type="string")
  27.      */
  28.     protected string $name '';
  29.     /**
  30.      * @var Collection
  31.      *
  32.      * @ORM\OneToMany (targetEntity="\XC\ProductVariations\Model\Variation", mappedBy="base", cascade={"all"})
  33.      */
  34.     protected Collection $variations;
  35.     public function __construct(array $data = [])
  36.     {
  37.         parent::__construct($data);
  38.         $this->variations = new ArrayCollection();
  39.     }
  40.     public function getId(): ?int
  41.     {
  42.         return $this->id;
  43.     }
  44.     public function isOrphaned(): bool
  45.     {
  46.         return $this->isEmpty() || $this->getVariations()->count() < 2;
  47.     }
  48.     public function isEmpty(): bool
  49.     {
  50.         return $this->getVariations()->isEmpty();
  51.     }
  52.     /**
  53.      * @return Collection|iterable<Variation>
  54.      */
  55.     public function getVariations(): Collection
  56.     {
  57.         return $this->variations;
  58.     }
  59.     public function setVariations(Collection $variations): VariationBase
  60.     {
  61.         $this->variations $variations;
  62.         return $this;
  63.     }
  64.     public function addVariation(Variation $newVariation): VariationBase
  65.     {
  66.         $newVariation->setBase($this);
  67.         $this->getVariations()->add($newVariation);
  68.         return $this;
  69.     }
  70.     /**
  71.      * @param array $variations
  72.      *
  73.      * @return VariationBase
  74.      */
  75.     public function setVariationsByArray(array $variations): VariationBase
  76.     {
  77.         $this->variations = new ArrayCollection($variations);
  78.         return $this;
  79.     }
  80.     /**
  81.      * @param Variation[] $variations
  82.      *
  83.      * @return void
  84.      */
  85.     public function addVariations(array $variations): void
  86.     {
  87.         foreach ($variations as $variation) {
  88.             $this->addVariation($variation);
  89.         }
  90.     }
  91.     public function getProductIds(): Collection
  92.     {
  93.         return $this->getVariations()
  94.             ->map(static fn (Variation $variation) => $variation->getProduct()->getProductId());
  95.     }
  96.     public function getProducts(): Collection
  97.     {
  98.         return $this->getVariations()
  99.             ->map(static fn (Variation $variation) => $variation->getProduct());
  100.     }
  101.     public function buildVariationsForSiblingIds(array $productIds): void
  102.     {
  103.         foreach ($productIds as $siblingIdToAdd) {
  104.             $variation $this->buildVariationForSiblingId($siblingIdToAdd);
  105.             $this->addVariation($variation);
  106.             Database::getEM()->persist($variation);
  107.         }
  108.     }
  109.     public function buildVariationForSiblingId(int $siblingIdToAdd): Variation
  110.     {
  111.         $product Database::getEM()->getReference(\XLite\Model\Product::class, $siblingIdToAdd);
  112.         $variation = new Variation();
  113.         $variation->setProduct($product);
  114.         return $variation;
  115.     }
  116.     public function getName(): string
  117.     {
  118.         return $this->name;
  119.     }
  120.     public function setName(string $name): void
  121.     {
  122.         $this->name $name;
  123.     }
  124.     /**
  125.      * @ORM\PrePersist
  126.      */
  127.     public function setDefaultVariationBaseName(): void
  128.     {
  129.         if (empty($this->name)) {
  130.             $this->name substr(Uuid::uuid4(), 08);
  131.         }
  132.     }
  133. }