vendor/sylius/sylius/src/Sylius/Component/Core/Model/ProductVariant.php line 23

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Component\Core\Model;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\Common\Comparable;
  15. use Sylius\Component\Product\Model\ProductVariant as BaseVariant;
  16. use Sylius\Component\Shipping\Model\ShippingCategoryInterface;
  17. use Sylius\Component\Taxation\Model\TaxCategoryInterface;
  18. class ProductVariant extends BaseVariant implements ProductVariantInterfaceComparable\Stringable
  19. {
  20.     /** @var int */
  21.     protected $version 1;
  22.     /** @var int */
  23.     protected $onHold 0;
  24.     /** @var int */
  25.     protected $onHand 0;
  26.     /** @var bool */
  27.     protected $tracked false;
  28.     /** @var float|null */
  29.     protected $weight;
  30.     /** @var float|null */
  31.     protected $width;
  32.     /** @var float|null */
  33.     protected $height;
  34.     /** @var float|null */
  35.     protected $depth;
  36.     /** @var TaxCategoryInterface|null */
  37.     protected $taxCategory;
  38.     /** @var ShippingCategoryInterface|null */
  39.     protected $shippingCategory;
  40.     /** @var Collection */
  41.     protected $channelPricings;
  42.     /** @var bool */
  43.     protected $shippingRequired true;
  44.     /**
  45.      * @var Collection|ProductImageInterface[]
  46.      *
  47.      * @psalm-var Collection<array-key, ProductImageInterface>
  48.      */
  49.     protected $images;
  50.     public function __construct()
  51.     {
  52.         parent::__construct();
  53.         /** @var ArrayCollection<array-key, ChannelPricingInterface> $this->channelPricings */
  54.         $this->channelPricings = new ArrayCollection();
  55.         /** @var ArrayCollection<array-key, ProductImageInterface> $this->images */
  56.         $this->images = new ArrayCollection();
  57.     }
  58.     public function __toString(): string
  59.     {
  60.         $string = (string) $this->getProduct()->getName();
  61.         if (!$this->getOptionValues()->isEmpty()) {
  62.             $string .= '(';
  63.             foreach ($this->getOptionValues() as $option) {
  64.                 $string .= $option->getOption()->getName() . ': ' $option->getValue() . ', ';
  65.             }
  66.             $string substr($string0, -2) . ')';
  67.         }
  68.         return $string;
  69.     }
  70.     public function getVersion(): ?int
  71.     {
  72.         return $this->version;
  73.     }
  74.     public function setVersion(?int $version): void
  75.     {
  76.         $this->version $version;
  77.     }
  78.     public function isInStock(): bool
  79.     {
  80.         return $this->onHand;
  81.     }
  82.     public function getOnHold(): ?int
  83.     {
  84.         return $this->onHold;
  85.     }
  86.     public function setOnHold(?int $onHold): void
  87.     {
  88.         $this->onHold $onHold;
  89.     }
  90.     public function getOnHand(): ?int
  91.     {
  92.         return $this->onHand;
  93.     }
  94.     public function setOnHand(?int $onHand): void
  95.     {
  96.         $this->onHand = ($onHand) ? $onHand;
  97.     }
  98.     public function isTracked(): bool
  99.     {
  100.         return $this->tracked;
  101.     }
  102.     public function setTracked(bool $tracked): void
  103.     {
  104.         $this->tracked $tracked;
  105.     }
  106.     public function getInventoryName(): ?string
  107.     {
  108.         return $this->getProduct()->getName();
  109.     }
  110.     public function getShippingCategory(): ?ShippingCategoryInterface
  111.     {
  112.         return $this->shippingCategory;
  113.     }
  114.     public function setShippingCategory(?ShippingCategoryInterface $shippingCategory): void
  115.     {
  116.         $this->shippingCategory $shippingCategory;
  117.     }
  118.     public function getWeight(): ?float
  119.     {
  120.         return $this->weight;
  121.     }
  122.     public function setWeight(?float $weight): void
  123.     {
  124.         $this->weight $weight;
  125.     }
  126.     public function getWidth(): ?float
  127.     {
  128.         return $this->width;
  129.     }
  130.     public function setWidth(?float $width): void
  131.     {
  132.         $this->width $width;
  133.     }
  134.     public function getHeight(): ?float
  135.     {
  136.         return $this->height;
  137.     }
  138.     public function setHeight(?float $height): void
  139.     {
  140.         $this->height $height;
  141.     }
  142.     public function getDepth(): ?float
  143.     {
  144.         return $this->depth;
  145.     }
  146.     public function setDepth(?float $depth): void
  147.     {
  148.         $this->depth $depth;
  149.     }
  150.     public function getShippingWeight(): ?float
  151.     {
  152.         return $this->getWeight();
  153.     }
  154.     public function getShippingWidth(): ?float
  155.     {
  156.         return $this->getWidth();
  157.     }
  158.     public function getShippingHeight(): ?float
  159.     {
  160.         return $this->getHeight();
  161.     }
  162.     public function getShippingDepth(): ?float
  163.     {
  164.         return $this->getDepth();
  165.     }
  166.     public function getShippingVolume(): ?float
  167.     {
  168.         return $this->depth $this->height $this->width;
  169.     }
  170.     public function getTaxCategory(): ?TaxCategoryInterface
  171.     {
  172.         return $this->taxCategory;
  173.     }
  174.     public function setTaxCategory(?TaxCategoryInterface $category): void
  175.     {
  176.         $this->taxCategory $category;
  177.     }
  178.     public function getChannelPricings(): Collection
  179.     {
  180.         return $this->channelPricings;
  181.     }
  182.     public function getChannelPricingForChannel(ChannelInterface $channel): ?ChannelPricingInterface
  183.     {
  184.         if ($this->channelPricings->containsKey($channel->getCode())) {
  185.             return $this->channelPricings->get($channel->getCode());
  186.         }
  187.         return null;
  188.     }
  189.     public function hasChannelPricingForChannel(ChannelInterface $channel): bool
  190.     {
  191.         return null !== $this->getChannelPricingForChannel($channel);
  192.     }
  193.     public function hasChannelPricing(ChannelPricingInterface $channelPricing): bool
  194.     {
  195.         return $this->channelPricings->contains($channelPricing);
  196.     }
  197.     public function addChannelPricing(ChannelPricingInterface $channelPricing): void
  198.     {
  199.         if (!$this->hasChannelPricing($channelPricing)) {
  200.             $channelPricing->setProductVariant($this);
  201.             $this->channelPricings->set($channelPricing->getChannelCode(), $channelPricing);
  202.         }
  203.     }
  204.     public function removeChannelPricing(ChannelPricingInterface $channelPricing): void
  205.     {
  206.         if ($this->hasChannelPricing($channelPricing)) {
  207.             $channelPricing->setProductVariant(null);
  208.             $this->channelPricings->remove($channelPricing->getChannelCode());
  209.         }
  210.     }
  211.     public function isShippingRequired(): bool
  212.     {
  213.         return $this->shippingRequired;
  214.     }
  215.     public function setShippingRequired(bool $shippingRequired): void
  216.     {
  217.         $this->shippingRequired $shippingRequired;
  218.     }
  219.     public function getAppliedPromotionsForChannel(ChannelInterface $channel): Collection
  220.     {
  221.         $channelPricing $this->getChannelPricingForChannel($channel);
  222.         return ($channelPricing !== null) ? $channelPricing->getAppliedPromotions() : new ArrayCollection();
  223.     }
  224.     /**
  225.      * @psalm-suppress InvalidReturnType https://github.com/doctrine/collections/pull/220
  226.      * @psalm-suppress InvalidReturnStatement https://github.com/doctrine/collections/pull/220
  227.      */
  228.     public function getImages(): Collection
  229.     {
  230.         return $this->images;
  231.     }
  232.     /**
  233.      * @psalm-suppress InvalidReturnType https://github.com/doctrine/collections/pull/220
  234.      * @psalm-suppress InvalidReturnStatement https://github.com/doctrine/collections/pull/220
  235.      */
  236.     public function getImagesByType(string $type): Collection
  237.     {
  238.         return $this->images->filter(function (ProductImageInterface $image) use ($type): bool {
  239.             return $type === $image->getType();
  240.         });
  241.     }
  242.     public function hasImages(): bool
  243.     {
  244.         return !$this->images->isEmpty();
  245.     }
  246.     public function hasImage(ProductImageInterface $image): bool
  247.     {
  248.         return $this->images->contains($image);
  249.     }
  250.     public function addImage(ProductImageInterface $image): void
  251.     {
  252.         if ($this->hasImage($image)) {
  253.             return;
  254.         }
  255.         $image->setOwner($this->getProduct());
  256.         $image->addProductVariant($this);
  257.         $this->images->add($image);
  258.     }
  259.     public function removeImage(ProductImageInterface $image): void
  260.     {
  261.         if ($this->hasImage($image)) {
  262.             $this->images->removeElement($image);
  263.         }
  264.     }
  265.     public function compareTo($other): int
  266.     {
  267.         return $this->code === $other->getCode() ? 1;
  268.     }
  269. }