<?php
namespace App\AtechNet\CoreBundle\Entity;
use App\AtechNet\CoreBundle\Enum\UnitList;
use App\AtechNet\CoreBundle\Repository\V2AttributeOptionRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToMany;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\OrderBy;
use Doctrine\ORM\Mapping\Table as Table;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Table(name="v2_attribute_options")
* @Entity(repositoryClass=V2AttributeOptionRepository::class)
* @HasLifecycleCallbacks
*/
class V2AttributeOption extends AbstractSignedEntity
{
public const int OPTION_ID_INTEGRATED_GPU = 6;
public const int OPTION_ID_GRADE_NEW = 116;
public const int OPTION_ID_GRADE_A = 117;
public const int OPTION_ID_GRADE_B = 124;
public const int OPTION_ID_GRADE_C = 125;
public const int OPTION_ID_GRADE_D = 126;
public const int OPTION_ID_STORAGE_TYPE_SDD = 207;
public const int OPTION_ID_STORAGE_TYPE_HDD = 208;
public const int OPTION_ID_STORAGE_TYPE_SDD_HDD = 209;
public const int OPTION_ID_OS_NONE = 210;
public const int OPTION_ID_OS_LANGUAGE_NONE = 212;
public const int OPTION_ID_KEYBOARD_REPRINT_NONE = 211;
public const int OPTION_ID_SKIN_NONE = 213;
public const array PRODUCT_GRADE_LIST = [
self::OPTION_ID_GRADE_NEW => "New",
self::OPTION_ID_GRADE_A => "A",
self::OPTION_ID_GRADE_B => "B",
self::OPTION_ID_GRADE_C => "C",
self::OPTION_ID_GRADE_D => "D"
];
public const array PERSONALIZED_OPTION_NONE_VALUE_LIST = [
V2Attribute::ID_OS => self::OPTION_ID_OS_NONE,
V2Attribute::ID_OS_LANGUAGE => self::OPTION_ID_OS_LANGUAGE_NONE,
V2Attribute::ID_KEYBOARD_REPRINT => self::OPTION_ID_KEYBOARD_REPRINT_NONE,
V2Attribute::ID_SKIN => self::OPTION_ID_SKIN_NONE
];
/**
* @var V2Attribute
*
* @Assert\NotNull
*
* @ManyToOne(targetEntity="V2Attribute", inversedBy="v2AttributeOptions")
* @JoinColumn(name="v2_attribute_id", referencedColumnName="id")
*/
private $v2Attribute;
/**
* @var string
*
* @Assert\NotNull
*
* @Column(name="value", type="text")
*/
private $value;
/**
* @var int
*
* @Column(name="unit", type="integer", nullable=true)
*/
private $unit;
/**
* @var integer
*
* @Assert\NotNull
*
* @Column(name="sort_index", type="integer")
*/
private $sortIndex = -1;
/**
* @var ArrayCollection
*
* @OneToMany(targetEntity="AttributeOptionAdditionalFieldValue", mappedBy="v2AttributeOption")
**/
private $additionalFieldValues;
/**
* @var ArrayCollection
*
* @ManyToMany(targetEntity="FixedAttributeOptionCombination", mappedBy="attributeOptions")
* @OrderBy({"id" = "ASC"})
*/
private $fixedAttributeOptionCombinations;
/**
* @var ArrayCollection
*
* @ManyToMany(targetEntity="ConfigurableAttributeOptionCombination", mappedBy="attributeOptions")
* @OrderBy({"id" = "ASC"})
*/
private $configurableAttributeOptionCombinations;
/**
* @var ArrayCollection
*
* @ManyToMany(targetEntity="PersonalizationAttributeOptionCombination", mappedBy="attributeOptions")
* @OrderBy({"id" = "ASC"})
*/
private $personalizationAttributeOptionCombinations;
/**
* @var bool
*
* @Column(name="archived", type="boolean")
*/
private $archived = 0;
public function __construct()
{
parent::__construct();
$this->additionalFieldValues = new ArrayCollection();
$this->fixedAttributeOptionCombinations = new ArrayCollection();
$this->configurableAttributeOptionCombinations = new ArrayCollection();
$this->personalizationAttributeOptionCombinations = new ArrayCollection();
}
/**
* @return string
*/
public function getValue()
{
return $this->value;
}
/**
* @param string $value
*/
public function setValue($value)
{
$this->value = $value;
}
/**
* @return V2Attribute
*/
public function getV2Attribute()
{
return $this->v2Attribute;
}
/**
* @param V2Attribute $v2Attribute
*/
public function setV2Attribute($v2Attribute)
{
$this->v2Attribute = $v2Attribute;
$v2Attribute->addOption($this);
}
/**
* @return int
*/
public function getSortIndex()
{
return $this->sortIndex;
}
/**
* @param int $sortIndex
*/
public function setSortIndex($sortIndex)
{
$this->sortIndex = $sortIndex;
}
/**
* @return string[]
*/
public static function getUnitList(): array
{
return UnitList::getChoices();
}
public function getUnit(): ?UnitList
{
return $this->unit ? UnitList::from($this->unit) : null;
}
public function setUnit(?UnitList $unit)
{
$this->unit = $unit->value;
}
/**
* @return ArrayCollection
*/
public function getAdditionalFieldValues()
{
return $this->additionalFieldValues;
}
public function getFixedAttributeOptionCombinations()
{
return $this->fixedAttributeOptionCombinations;
}
public function getConfigurableAttributeOptionCombinations()
{
return $this->configurableAttributeOptionCombinations;
}
public function getPersonalizationAttributeOptionCombinations()
{
return $this->personalizationAttributeOptionCombinations;
}
/**
* @return string
*/
public function getDisplayFormattedValue(bool $showOnlyGbMb = false): string
{
$fieldType = $this->getV2Attribute()->getFieldType();
$unit = $this->getUnit()
? $this->getUnit()->getDisplayName()
: null;
$attribute = !$showOnlyGbMb && in_array($fieldType, [V2Attribute::FIELD_TYPE_YES_NO, V2Attribute::FIELD_TYPE_NUMBER])
? $this->getV2Attribute()->getTitle()
: null;
return trim("$attribute {$this->getValue()} $unit");
}
/**
* @return boolean
*/
public function isArchived()
{
return $this->archived;
}
/**
* @param boolean $archived
*/
public function setArchived($archived)
{
$this->archived = $archived;
}
/**
* @return int
*/
public function getSelectedCpuGeneration(): int
{
$values = $this->getAdditionalFieldValues();
/** @var ?AttributeOptionAdditionalFieldValue $cpuGeneration */
$cpuGeneration = $values->filter(fn(AttributeOptionAdditionalFieldValue $value) => $value->getAdditionalField()->getV2AttributeDataset()?->getId() === V2Attribute::ID_CPU_GENERATION)->first();
return $cpuGeneration ? $cpuGeneration->getFieldValue() : 0;
}
}