vendor/fri0z/mldev-catalog-bundle/src/Entity/ProductItem.php line 18

Open in your IDE?
  1. <?php
  2. namespace MLDev\CatalogBundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Criteria;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use JsonSerializable;
  7. use MLDev\BaseBundle\Contract\SeoSite\SeoSiteInfoInterface;
  8. use MLDev\CatalogBundle\Repository\ProductItemRepository;
  9. use phpDocumentor\Reflection\Types\Iterable_;
  10. /**
  11.  * @ORM\Table(name="MLDev_Product_Item")
  12.  * @ORM\Entity(repositoryClass=ProductItemRepository::class)
  13.  */
  14. class ProductItem implements JsonSerializable
  15. {
  16.     /**
  17.      * @ORM\Id
  18.      * @ORM\Column(name="id", type="integer")
  19.      * @ORM\GeneratedValue(strategy="AUTO")
  20.      */
  21.     private $id;
  22.     /**
  23.      * @ORM\Column(type="string", name="external_id", nullable=true)
  24.      */
  25.     private $externalId;
  26.     /**
  27.      * @ORM\ManyToOne(targetEntity=Product::class, inversedBy="items")
  28.      * @ORM\JoinColumn(name="product_id", referencedColumnName="id", onDelete="CASCADE")
  29.      */
  30.     private $product;
  31.     /**
  32.      * @ORM\Column(type="string", name="name", nullable=true)
  33.      */
  34.     private $name;
  35.     /**
  36.      * @ORM\Column(type="string", name="alterName", nullable=true)
  37.      */
  38.     private $alterName;
  39.     /**
  40.      * @ORM\Column(type="string", name="sku", nullable=true)
  41.      */
  42.     private $sku;
  43.     /**
  44.      * @ORM\Column(type="integer", name="amount", nullable=true)
  45.      */
  46.     private $amount 0;
  47.     /**
  48.      * @ORM\Column(type="boolean", name="is_active",nullable=false)
  49.      */
  50.     private $isActive true;
  51.     /**
  52.      * @ORM\Column(name="is_new", type="boolean", nullable=false)
  53.      */
  54.     private $isNew false;
  55.     /**
  56.      * @ORM\Column(name="is_sale", type="boolean", nullable=false)
  57.      */
  58.     private $isSale false;
  59.     /**
  60.      * @ORM\Column(type="boolean", name="use_on_yandex_market", nullable=false)
  61.      */
  62.     private $useOnYandexMarket false;
  63.     /**
  64.      * @ORM\ManyToOne(targetEntity=ProductImage::class)
  65.      * @ORM\JoinColumn(name="product_image_id", referencedColumnName="id", onDelete="SET NULL")
  66.      */
  67.     private $defaultImage;
  68.     /**
  69.      * @ORM\Column(type="json", name="available_image_list", nullable=true)
  70.      */
  71.     private $availableImageList;
  72.     /**
  73.      * @ORM\ManyToOne(targetEntity=Color::class)
  74.      * @ORM\JoinColumn(name="color_id", referencedColumnName="id", onDelete="SET NULL")
  75.      */
  76.     private $color;
  77.     /**
  78.      * @ORM\ManyToMany(targetEntity=SeoSiteInfoInterface::class, cascade={"persist"})
  79.      * @ORM\JoinTable(
  80.      *     name="MLDev_Product_Item_SeoInfo",
  81.      *     joinColumns={
  82.      *          @ORM\JoinColumn(name="product_item_id", referencedColumnName="id", onDelete="CASCADE")
  83.      *     },
  84.      *     inverseJoinColumns={
  85.      *          @ORM\JoinColumn(name="seo_info_id", referencedColumnName="id", onDelete="CASCADE")
  86.      *     }
  87.      * )
  88.      */
  89.     private $seoInfo;
  90.     /**
  91.      * @ORM\OneToMany(targetEntity=ProductItemPrice::class, mappedBy="productItem", cascade={"persist", "remove"})
  92.      */
  93.     private $prices;
  94.     /**
  95.      * @ORM\OneToMany(targetEntity="MLDev\CatalogBundle\Entity\ProductItemStock", mappedBy="productItem", cascade={"persist", "remove"})
  96.      */
  97.     private $stocks;
  98.     /**
  99.      * ProductItem constructor.
  100.      */
  101.     public function __construct($name null)
  102.     {
  103.         if ($name !== null) {
  104.             $this->setName($name);
  105.         }
  106.         $this->prices = new ArrayCollection();
  107.         $this->stocks = new ArrayCollection();
  108.         $this->seoInfo = new ArrayCollection();
  109.     }
  110.     public function getAvailableImages(): iterable
  111.     {
  112.         $availableImageList $this->availableImageList ?? [];
  113.         return $this->getProduct()->getImages()->filter(function ($value) use ($availableImageList) {
  114.             return in_array($value->getId(), $availableImageList);
  115.         });
  116.     }
  117.     public function getAvailableImageList(): ?array
  118.     {
  119.         return $this->availableImageList;
  120.     }
  121.     public function setAvailableImageList(?array $availableImageList): void
  122.     {
  123.         $this->availableImageList $availableImageList;
  124.     }
  125.     public function getSeoInfo(): iterable
  126.     {
  127.         return $this->seoInfo;
  128.     }
  129.     public function setSeoInfo(iterable $seoInfo): void
  130.     {
  131.         $this->seoInfo $seoInfo;
  132.     }
  133.     public function getId(): ?int
  134.     {
  135.         return $this->id;
  136.     }
  137.     public function getExternalId(): ?string
  138.     {
  139.         return $this->externalId;
  140.     }
  141.     public function setExternalId(?string $externalId): void
  142.     {
  143.         $this->externalId $externalId;
  144.     }
  145.     public function getProduct(): ?Product
  146.     {
  147.         return $this->product;
  148.     }
  149.     public function setProduct(?Product $product): void
  150.     {
  151.         $this->product $product;
  152.     }
  153.     public function getName(): ?string
  154.     {
  155.         return $this->name;
  156.     }
  157.     public function setName(?string $name): void
  158.     {
  159.         $this->name $name;
  160.     }
  161.     public function getAlterName(): ?string
  162.     {
  163.         return $this->alterName;
  164.     }
  165.     public function setAlterName(?string $alterName): void
  166.     {
  167.         $this->alterName $alterName;
  168.     }
  169.     public function getSku(): ?string
  170.     {
  171.         return $this->sku;
  172.     }
  173.     public function setSku(?string $sku): void
  174.     {
  175.         $this->sku $sku;
  176.     }
  177.     public function getAmount(): ?int
  178.     {
  179.         return $this->amount;
  180.     }
  181.     public function setAmount(?int $amount): void
  182.     {
  183.         $this->amount $amount;
  184.     }
  185.     public function isActive(): bool
  186.     {
  187.         return $this->isActive;
  188.     }
  189.     public function setIsActive(bool $isActive): void
  190.     {
  191.         $this->isActive $isActive;
  192.     }
  193.     public function isNew(): bool
  194.     {
  195.         return $this->isNew;
  196.     }
  197.     public function setIsNew(bool $isNew): self
  198.     {
  199.         $this->isNew $isNew;
  200.         return $this;
  201.     }
  202.     public function isSale(): bool
  203.     {
  204.         return $this->isSale;
  205.     }
  206.     public function setIsSale(bool $isSale): self
  207.     {
  208.         $this->isSale $isSale;
  209.         return $this;
  210.     }
  211.     public function isUseOnYandexMarket(): ?bool
  212.     {
  213.         return $this->useOnYandexMarket;
  214.     }
  215.     public function setUseOnYandexMarket(?bool $useOnYandexMarket): void
  216.     {
  217.         $this->useOnYandexMarket $useOnYandexMarket;
  218.     }
  219.     public function getDefaultImage(): ?ProductImage
  220.     {
  221.         if (!$this->defaultImage) {
  222.             if ($this->getProduct() && $this->getProduct()->getImages()->count()) {
  223.                 return $this->getProduct()->getImages()->first();
  224.             }
  225.         }
  226.         return $this->defaultImage;
  227.     }
  228.     public function setDefaultImage(?ProductImage $defaultImage): void
  229.     {
  230.         $this->defaultImage $defaultImage;
  231.     }
  232.     public function getColor(): ?Color
  233.     {
  234.         return $this->color;
  235.     }
  236.     public function setColor(?Color $color): void
  237.     {
  238.         $this->color $color;
  239.     }
  240.     public function getPrices(): iterable
  241.     {
  242.         return $this->prices;
  243.     }
  244.     public function setPrices(iterable $prices): void
  245.     {
  246.         $this->prices $prices;
  247.     }
  248.     public function addPrice(ProductItemPrice $price): void
  249.     {
  250.         $this->prices->add($price);
  251.     }
  252.     public function removePrice(ProductItemPrice $price): void
  253.     {
  254.         $this->prices->removeElement($price);
  255.     }
  256.     public function getPrice(): ?float
  257.     {
  258.         if ($this->prices->count() > 0) {
  259.             if (($prices $this->getPricesIsBasic()) && $prices->count() > 0) {
  260.                 return $prices->first()->getValue();
  261.             }
  262.         }
  263.         return null;
  264.     }
  265.     public function getPriceOld(): ?float
  266.     {
  267.         if ($this->prices->count() > 0) {
  268.             if (($prices $this->getPricesIsBasic()) && $prices->count() > 0) {
  269.                 return $prices->first()->getOldValue();
  270.             }
  271.         }
  272.         return null;
  273.     }
  274.     public function getPriceByAlias(?string $alias): ?float
  275.     {
  276.         if (($prices $this->getPricesByAlias($alias)) && $prices->count() > 0) {
  277.             return $prices->first()->getValue();
  278.         }
  279.         return null;
  280.     }
  281.     public function getPriceOldByAlias(?string $alias): ?float
  282.     {
  283.         if (($prices $this->getPricesByAlias($alias)) && $prices->count() > 0) {
  284.             return $prices->first()->getOldValue();
  285.         }
  286.         return null;
  287.     }
  288.     private function getPricesByAlias(?string $alias): iterable
  289.     {
  290.         return $this->prices->matching(
  291.             new Criteria(
  292.                 Criteria::expr()->eq('priceAlias'$alias)
  293.             )
  294.         );
  295.     }
  296.     private function getPricesIsBasic(): iterable
  297.     {
  298.         return $this->prices->matching(
  299.             new Criteria(
  300.                 Criteria::expr()->eq('priceBasic'true)
  301.             )
  302.         );
  303.     }
  304.     public function getStocks(): iterable
  305.     {
  306.         return $this->stocks;
  307.     }
  308.     public function setStocks(iterable $stocks): void
  309.     {
  310.         $this->stocks $stocks;
  311.     }
  312.     public function addStock(ProductItemStock $stock): void
  313.     {
  314.         $this->stocks->add($stock);
  315.     }
  316.     public function removeStock(ProductItemStock $stock): void
  317.     {
  318.         $this->stocks->removeElement($stock);
  319.     }
  320.     public function jsonSerialize(): array
  321.     {
  322.         return [
  323.             'id' => $this->getId(),
  324.             'sku' => $this->getSku(),
  325.             'name' => $this->getName(),
  326.             'alter_name' => $this->getAlterName(),
  327.             'price' => $this->getPrice(),
  328.             'price_old' => $this->getPriceOld(),
  329.             'prices' => $this->getPrices(),
  330.             'image' => $this->getDefaultImage(),
  331.             'images' => $this->getAvailableImages(),
  332.             'color' => $this->getColor(),
  333.             'stocks' => $this->getStocks(),
  334.             'is_sale' => $this->isSale(),
  335.             'is_new' => $this->isNew(),
  336.             'unit' => $this->getProduct()->getUnit(),
  337.         ];
  338.     }
  339. }