<?php
namespace App\Entity;
use App\Service\Helper\NormalizerHelper;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use \Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Table(name="products__products", uniqueConstraints={
* @ORM\UniqueConstraint(name="unique_product", columns={"article_search", "brand_id"})}
* )
* @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
*/
class Product
{
use IdTrait;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Brand")
* @ORM\JoinColumn(name="brand_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
private Brand $brand;
/**
* @ORM\Column(name="article_search", type="string", nullable=false)
*/
private string $articleSearch;
/**
* @ORM\Column(name="article_original", type="string", nullable=false)
*/
private string $articleOriginal;
/**
* @ORM\Column(name="description", type="text", nullable=false)
*/
private string $description;
/**
* @var Item[]|Collection
* @ORM\OneToMany(targetEntity="App\Entity\Item", mappedBy="product")
*/
private $items;
/**
* @var SupplierItem[]|Collection
* @ORM\OneToMany(targetEntity="SupplierItem", mappedBy="product")
*/
private $supplierItems;
/**
* @var ProductImage[]|Collection
* @ORM\OneToMany(targetEntity="App\Entity\ProductImage", mappedBy="product")
*/
private $images;
/**
* @var Collection|Analog[]
* @ORM\OneToMany(targetEntity="App\Entity\Analog", mappedBy="owner", cascade={"all"})
*/
private $analogs;
/**
* @ORM\OneToOne(targetEntity="Storage", mappedBy="product")
*/
private ?Storage $storage = null;
public function __construct(Brand $brand, string $article, string $description)
{
$this->brand = $brand;
$this->articleOriginal = $article;
$this->description = $description;
$this->articleSearch = NormalizerHelper::transformToArticleSearch($article);
$this->items = new ArrayCollection();
$this->supplierItems = new ArrayCollection();
$this->images = new ArrayCollection();
$this->analogs = new ArrayCollection();
}
/**
* @return Collection|ProductImage[]
*/
public function getImages()
{
return $this->images;
}
public function getDefaultImageSrc(): ?string
{
$src = $this->getImages()->count() ? $this->getImages()->first()->getHash() : null;
return !$src || !file_exists($src) ? '/assets/img/nopic.jpg' : $src;
}
public function addImage(ProductImage $image): Product
{
$this->images->add($image);
return $this;
}
/**
* @return Collection|Item[]
*/
public function getItems()
{
return $this->items;
}
/**
* @return Collection|SupplierItem[]
*/
public function getSupplierItems()
{
return $this->supplierItems;
}
public function addItem(Item $item): Product
{
$this->items->add($item);
return $this;
}
public function getArticleSearch(): string
{
return $this->articleSearch;
}
public function getBrand(): Brand
{
return $this->brand;
}
public function getBrandName(): string
{
return $this->getBrand()->getName();
}
public function setBrand(Brand $brand): Product
{
$this->brand = $brand;
return $this;
}
public function getArticleOriginal(): string
{
return $this->articleOriginal;
}
public function getDescription(): string
{
return $this->description;
}
public function setDescription(string $description): Product
{
$this->description = $description;
return $this;
}
/**
* @return Analog[]|Collection
*/
public function getAnalogs()
{
return $this->analogs;
}
public function getStorage(): ?Storage
{
return $this->storage;
}
}