<?php
namespace App\Entity;
use App\Repository\BaseProductVariantRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\String\Slugger\AsciiSlugger;
#[ORM\Entity(repositoryClass: BaseProductVariantRepository::class)]
class BaseProductVariant
{
use TimestampableEntity;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'baseProductVariants')]
#[ORM\JoinColumn(nullable: false)]
private ?BaseProduct $baseProduct = null;
#[ORM\ManyToMany(targetEntity: BaseProductVariantAttribute::class, inversedBy: 'baseProductVariants')]
#[ORM\OrderBy(["variantAttribute" => "ASC"])]
private Collection $baseProductVariantAttributes;
#[ORM\Column(length: 255, nullable: true)]
private ?string $sku = null;
#[ORM\Column(nullable: true)]
private ?float $price = 0;
#[ORM\Column]
private ?bool $active = true;
#[ORM\OneToMany(mappedBy: 'baseProductVariant', targetEntity: BaseProductSide::class)]
private Collection $baseProductSides;
#[ORM\Column(length: 255, nullable: true)]
private ?string $name = null;
#[ORM\Column(type:"integer", nullable:true)]
private ?int $weight = null;
#[ORM\Column(type:"integer", nullable:true)]
private ?int $length = null;
#[ORM\Column(type:"integer", nullable:true)]
private ?int $width = null;
#[ORM\Column(type:"integer", nullable:true)]
private ?int $height = null;
public function __construct()
{
$this->baseProductVariantAttributes = new ArrayCollection();
$this->baseProductSides = new ArrayCollection();
}
public function __toString(): string
{
if ($this->name) {
return $this->getName();
}
return $this->getGeneratedName();
}
public function getGeneratedSku(): string {
$slugger = new AsciiSlugger();
return $slugger->slug($this->getGeneratedName(), '_');
}
public function getGeneratedPrice(): float {
return 0;
}
public function getGeneratedName(): string
{
$nameArray = [];
$attributes = $this->getBaseProductVariantAttributes();
foreach ($attributes as $attribute) {
$name = $attribute->getVariantAttribute()->getName();
$nameArray[$name] = $attribute->getValue();
}
ksort($nameArray);
$name = implode(' ', $nameArray);
$name = trim($name);
if ($name === "") {
$name = "New Variant";
}
return $name;
}
public function getId(): ?int
{
return $this->id;
}
public function getBaseProduct(): ?BaseProduct
{
return $this->baseProduct;
}
public function setBaseProduct(?BaseProduct $baseProduct): self
{
$this->baseProduct = $baseProduct;
return $this;
}
/**
* @return Collection<int, BaseProductVariantAttribute>
*/
public function getBaseProductVariantAttributes(): Collection
{
return $this->baseProductVariantAttributes;
}
public function addBaseProductVariantAttribute(BaseProductVariantAttribute $baseProductVariantAttribute): self
{
if (!$this->baseProductVariantAttributes->contains($baseProductVariantAttribute)) {
$this->baseProductVariantAttributes->add($baseProductVariantAttribute);
}
return $this;
}
public function removeBaseProductVariantAttribute(BaseProductVariantAttribute $baseProductVariantAttribute): self
{
$this->baseProductVariantAttributes->removeElement($baseProductVariantAttribute);
return $this;
}
public function getSku(): ?string
{
return $this->sku;
}
public function setSku(?string $sku): self
{
$this->sku = $sku;
return $this;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(?float $price): self
{
$this->price = $price;
return $this;
}
public function isActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
/**
* @return Collection<int, BaseProductSide>
*/
public function getBaseProductSides(): Collection
{
return $this->baseProductSides;
}
public function addBaseProductSide(BaseProductSide $baseProductSide): self
{
if (!$this->baseProductSides->contains($baseProductSide)) {
$this->baseProductSides->add($baseProductSide);
$baseProductSide->setBaseProductVariant($this);
}
return $this;
}
public function removeBaseProductSide(BaseProductSide $baseProductSide): self
{
if ($this->baseProductSides->removeElement($baseProductSide)) {
// set the owning side to null (unless already changed)
if ($baseProductSide->getBaseProductVariant() === $this) {
$baseProductSide->setBaseProductVariant(null);
}
}
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getAttributeByValue($value) {
foreach ( $this->baseProductVariantAttributes as $bpvAttribute) {
if (htmlspecialchars(ucfirst($bpvAttribute->getValue())) === $value ||
$bpvAttribute->getValue() === $value
) {
return $bpvAttribute;
}
}
return null;
}
public function getAttributesAssoc($encode = false) {
$arr = [];
foreach ($this->baseProductVariantAttributes as $bpvAttribute) {
$arr[$bpvAttribute->getVariantAttribute()->getId()] = $bpvAttribute->getValue();
}
return $encode ? json_encode($arr) : $arr;
}
public function getWeight(): ?int
{
return $this->weight ?? $this->baseProduct->getDefaultWeight();
}
public function setWeight(?int $weight): self
{
$this->weight = $weight ?: $this->baseProduct->getDefaultWeight();
return $this;
}
public function getLength(): ?int
{
return $this->length ?? $this->baseProduct->getDefaultLength();
}
public function setLength(?int $length): self
{
$this->length = $length;
return $this;
}
public function getWidth(): ?int
{
return $this->width ?? $this->baseProduct->getDefaultWidth();
}
public function setWidth(?int $width): self
{
$this->width = $width;
return $this;
}
public function getHeight(): ?int
{
return $this->height ?? $this->baseProduct->getDefaultHeight();
}
public function setHeight(?int $height): self
{
$this->height = $height;
return $this;
}
}