<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Persistence\Event\LifecycleEventArgs;
/**
* ProductsAttributes
*
* @ORM\Table(name="products_attributes")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
*/
class ProductAttribute extends TranslatedEntity {
protected $tranlatedEntity = 'ProductAttributeDescription';
/**
* @var int
*
* @ORM\Column(name="products_attributes_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var Product
*
* @ORM\ManyToOne(targetEntity="App\Entity\Product", inversedBy="attributes")
* @ORM\JoinColumn(name="products_id", referencedColumnName="products_id", onDelete="CASCADE")
*/
private $product;
/**
* @var Attribute
*
* @ORM\ManyToOne(targetEntity="App\Entity\Attribute")
* @ORM\JoinColumn(name="attributes_id", referencedColumnName="attributes_id", onDelete="CASCADE")
*/
private $attribute;
/**
* @ORM\OneToMany(targetEntity="App\Entity\ProductAttributeDescription", mappedBy="productAttribute", cascade={"persist", "remove"})
*/
private $descriptions = [];
/**
* @var ?string
*
* @ORM\Column(name="attributes_value", type="string", length=255, nullable=true)
*/
private $value;
public function getId(): int {
return $this->id;
}
public function getProduct(): Product {
return $this->product;
}
public function getAttribute(): Attribute {
return $this->attribute;
}
public function getValue(): ?string {
return $this->value;
}
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();
}
public function setId(int $id): void {
$this->id = $id;
}
public function setProduct(Product $product): void {
$this->product = $product;
}
public function setAttribute(Attribute $attribute): void {
$this->attribute = $attribute;
}
public function setValue(?string $value): void {
$this->value = $value;
}
public function getDescriptions() {
return $this->descriptions;
}
public function setDescriptions($descriptions): void {
$this->descriptions = $descriptions;
}
public function addDescription(ProductAttributeDescription $description) {
$this->descriptions->add($description);
}
public function getDescription($lang = 'fr') {
foreach($this->getDescriptions() as $desc){
if($desc->getLanguage()->getCode() == $lang)
return $desc;
}
return null;
}
/**
* @ORM\PostLoad
*/
public function postLoad(LifecycleEventArgs $args): void {
if ($this->getAttribute()->getCode() === 'main_color') {
$this->entity = $args->getEntityManager()->getRepository(Color::class)->findOneByCode($this->getValue());
}else if($this->getAttribute()->getCode() === 'precise_color'){
$this->entity = $args->getEntityManager()->getRepository(Color::class)->findOneByCode($this->getValue());
}
}
}