<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use App\Entity\ProductModel;
/**
* ProductsAttributes
*
* @ORM\Table(name="products_models_attributes")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
*/
class ProductModelAttribute extends TranslatedEntity {
/**
* @var int
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var ProductModel
*
* @ORM\ManyToOne(targetEntity="App\Entity\ProductModel", inversedBy="attributes", cascade={"persist"})
* @ORM\JoinColumn(name="products_models_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $productModel;
/**
* @var Attribute
*
* @ORM\ManyToOne(targetEntity="App\Entity\Attribute")
* @ORM\JoinColumn(name="attributes_id", referencedColumnName="attributes_id", onDelete="CASCADE")
*/
private $attribute;
/**
* @var ?string
*
* @ORM\Column(name="attributes_value", type="string", length=255, nullable=true)
*/
private $value;
/**
* @ORM\OneToMany(targetEntity="App\Entity\ProductModelAttributeDescription", mappedBy="productModelAttribute", cascade={"persist", "remove"})
*/
private $descriptions = [];
public function getId(): int {
return $this->id;
}
public function getProductModel(): ProductModel {
return $this->productModel;
}
public function getAttribute(): Attribute {
return $this->attribute;
}
public function getValue(): ?string {
return $this->value;
}
public function setId(int $id): void {
$this->id = $id;
}
public function setAttribute(Attribute $attribute): void {
$this->attribute = $attribute;
}
public function setValue(?string $value): void {
$this->value = $value;
}
public function setProductModel(ProductModel $productModel): void {
$this->productModel = $productModel;
}
public function getDisplayedValue(): ?string {
if ($this->value === null) {
return null;
}
if(strpos($this->getAttribute()->getCode(), '-unit')!==false) {
switch($this->value) {
case 'MILLIMETER' : return 'mm';
case 'MILLILITER' : return 'ml';
case 'GRAM' : return 'g';
case 'MONTH' : return '';
default:;
}
}else{
$unit = $this->product->getAttribute($this->getAttribute()->getCode() . '-unit');
if ($unit && $unit->getValue()) {
if($unit->getDisplayedValue() == 'ml') {
$value = intval($this->getValue());
if($value >= 1000) {
return intval($this->getValue()/1000) . 'l';
}
return intval($this->getValue()) . $unit->getDisplayedValue();
}
return $this->getValue() . $unit->getDisplayedValue();
}
}
return $this->getValue();
}
}