<?php
namespace MLDev\CatalogBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
use MLDev\BaseBundle\Contract\SeoSite\SeoSiteInfoInterface;
use MLDev\CatalogBundle\Repository\ProductItemRepository;
use phpDocumentor\Reflection\Types\Iterable_;
/**
* @ORM\Table(name="MLDev_Product_Item")
* @ORM\Entity(repositoryClass=ProductItemRepository::class)
*/
class ProductItem implements JsonSerializable
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", name="external_id", nullable=true)
*/
private $externalId;
/**
* @ORM\ManyToOne(targetEntity=Product::class, inversedBy="items")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $product;
/**
* @ORM\Column(type="string", name="name", nullable=true)
*/
private $name;
/**
* @ORM\Column(type="string", name="alterName", nullable=true)
*/
private $alterName;
/**
* @ORM\Column(type="string", name="sku", nullable=true)
*/
private $sku;
/**
* @ORM\Column(type="integer", name="amount", nullable=true)
*/
private $amount = 0;
/**
* @ORM\Column(type="boolean", name="is_active",nullable=false)
*/
private $isActive = true;
/**
* @ORM\Column(name="is_new", type="boolean", nullable=false)
*/
private $isNew = false;
/**
* @ORM\Column(name="is_sale", type="boolean", nullable=false)
*/
private $isSale = false;
/**
* @ORM\Column(type="boolean", name="use_on_yandex_market", nullable=false)
*/
private $useOnYandexMarket = false;
/**
* @ORM\ManyToOne(targetEntity=ProductImage::class)
* @ORM\JoinColumn(name="product_image_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $defaultImage;
/**
* @ORM\Column(type="json", name="available_image_list", nullable=true)
*/
private $availableImageList;
/**
* @ORM\ManyToOne(targetEntity=Color::class)
* @ORM\JoinColumn(name="color_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $color;
/**
* @ORM\ManyToMany(targetEntity=SeoSiteInfoInterface::class, cascade={"persist"})
* @ORM\JoinTable(
* name="MLDev_Product_Item_SeoInfo",
* joinColumns={
* @ORM\JoinColumn(name="product_item_id", referencedColumnName="id", onDelete="CASCADE")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="seo_info_id", referencedColumnName="id", onDelete="CASCADE")
* }
* )
*/
private $seoInfo;
/**
* @ORM\OneToMany(targetEntity=ProductItemPrice::class, mappedBy="productItem", cascade={"persist", "remove"})
*/
private $prices;
/**
* @ORM\OneToMany(targetEntity="MLDev\CatalogBundle\Entity\ProductItemStock", mappedBy="productItem", cascade={"persist", "remove"})
*/
private $stocks;
/**
* ProductItem constructor.
*/
public function __construct($name = null)
{
if ($name !== null) {
$this->setName($name);
}
$this->prices = new ArrayCollection();
$this->stocks = new ArrayCollection();
$this->seoInfo = new ArrayCollection();
}
public function getAvailableImages(): iterable
{
$availableImageList = $this->availableImageList ?? [];
return $this->getProduct()->getImages()->filter(function ($value) use ($availableImageList) {
return in_array($value->getId(), $availableImageList);
});
}
public function getAvailableImageList(): ?array
{
return $this->availableImageList;
}
public function setAvailableImageList(?array $availableImageList): void
{
$this->availableImageList = $availableImageList;
}
public function getSeoInfo(): iterable
{
return $this->seoInfo;
}
public function setSeoInfo(iterable $seoInfo): void
{
$this->seoInfo = $seoInfo;
}
public function getId(): ?int
{
return $this->id;
}
public function getExternalId(): ?string
{
return $this->externalId;
}
public function setExternalId(?string $externalId): void
{
$this->externalId = $externalId;
}
public function getProduct(): ?Product
{
return $this->product;
}
public function setProduct(?Product $product): void
{
$this->product = $product;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): void
{
$this->name = $name;
}
public function getAlterName(): ?string
{
return $this->alterName;
}
public function setAlterName(?string $alterName): void
{
$this->alterName = $alterName;
}
public function getSku(): ?string
{
return $this->sku;
}
public function setSku(?string $sku): void
{
$this->sku = $sku;
}
public function getAmount(): ?int
{
return $this->amount;
}
public function setAmount(?int $amount): void
{
$this->amount = $amount;
}
public function isActive(): bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): void
{
$this->isActive = $isActive;
}
public function isNew(): bool
{
return $this->isNew;
}
public function setIsNew(bool $isNew): self
{
$this->isNew = $isNew;
return $this;
}
public function isSale(): bool
{
return $this->isSale;
}
public function setIsSale(bool $isSale): self
{
$this->isSale = $isSale;
return $this;
}
public function isUseOnYandexMarket(): ?bool
{
return $this->useOnYandexMarket;
}
public function setUseOnYandexMarket(?bool $useOnYandexMarket): void
{
$this->useOnYandexMarket = $useOnYandexMarket;
}
public function getDefaultImage(): ?ProductImage
{
if (!$this->defaultImage) {
if ($this->getProduct() && $this->getProduct()->getImages()->count()) {
return $this->getProduct()->getImages()->first();
}
}
return $this->defaultImage;
}
public function setDefaultImage(?ProductImage $defaultImage): void
{
$this->defaultImage = $defaultImage;
}
public function getColor(): ?Color
{
return $this->color;
}
public function setColor(?Color $color): void
{
$this->color = $color;
}
public function getPrices(): iterable
{
return $this->prices;
}
public function setPrices(iterable $prices): void
{
$this->prices = $prices;
}
public function addPrice(ProductItemPrice $price): void
{
$this->prices->add($price);
}
public function removePrice(ProductItemPrice $price): void
{
$this->prices->removeElement($price);
}
public function getPrice(): ?float
{
if ($this->prices->count() > 0) {
if (($prices = $this->getPricesIsBasic()) && $prices->count() > 0) {
return $prices->first()->getValue();
}
}
return null;
}
public function getPriceOld(): ?float
{
if ($this->prices->count() > 0) {
if (($prices = $this->getPricesIsBasic()) && $prices->count() > 0) {
return $prices->first()->getOldValue();
}
}
return null;
}
public function getPriceByAlias(?string $alias): ?float
{
if (($prices = $this->getPricesByAlias($alias)) && $prices->count() > 0) {
return $prices->first()->getValue();
}
return null;
}
public function getPriceOldByAlias(?string $alias): ?float
{
if (($prices = $this->getPricesByAlias($alias)) && $prices->count() > 0) {
return $prices->first()->getOldValue();
}
return null;
}
private function getPricesByAlias(?string $alias): iterable
{
return $this->prices->matching(
new Criteria(
Criteria::expr()->eq('priceAlias', $alias)
)
);
}
private function getPricesIsBasic(): iterable
{
return $this->prices->matching(
new Criteria(
Criteria::expr()->eq('priceBasic', true)
)
);
}
public function getStocks(): iterable
{
return $this->stocks;
}
public function setStocks(iterable $stocks): void
{
$this->stocks = $stocks;
}
public function addStock(ProductItemStock $stock): void
{
$this->stocks->add($stock);
}
public function removeStock(ProductItemStock $stock): void
{
$this->stocks->removeElement($stock);
}
public function jsonSerialize(): array
{
return [
'id' => $this->getId(),
'sku' => $this->getSku(),
'name' => $this->getName(),
'alter_name' => $this->getAlterName(),
'price' => $this->getPrice(),
'price_old' => $this->getPriceOld(),
'prices' => $this->getPrices(),
'image' => $this->getDefaultImage(),
'images' => $this->getAvailableImages(),
'color' => $this->getColor(),
'stocks' => $this->getStocks(),
'is_sale' => $this->isSale(),
'is_new' => $this->isNew(),
'unit' => $this->getProduct()->getUnit(),
];
}
}