src/AtechNet/CoreBundle/Entity/V2AttributeOption.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\AtechNet\CoreBundle\Entity;
  3. use App\AtechNet\CoreBundle\Enum\UnitList;
  4. use App\AtechNet\CoreBundle\Repository\V2AttributeOptionRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\ORM\Mapping\Column;
  7. use Doctrine\ORM\Mapping\Entity;
  8. use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
  9. use Doctrine\ORM\Mapping\JoinColumn;
  10. use Doctrine\ORM\Mapping\ManyToMany;
  11. use Doctrine\ORM\Mapping\ManyToOne;
  12. use Doctrine\ORM\Mapping\OneToMany;
  13. use Doctrine\ORM\Mapping\OrderBy;
  14. use Doctrine\ORM\Mapping\Table as Table;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. /**
  17.  * @Table(name="v2_attribute_options")
  18.  * @Entity(repositoryClass=V2AttributeOptionRepository::class)
  19.  * @HasLifecycleCallbacks
  20.  */
  21. class V2AttributeOption extends AbstractSignedEntity
  22. {
  23.     public const int OPTION_ID_INTEGRATED_GPU = 6;
  24.     public const int OPTION_ID_GRADE_NEW = 116;
  25.     public const int OPTION_ID_GRADE_A = 117;
  26.     public const int OPTION_ID_GRADE_B = 124;
  27.     public const int OPTION_ID_GRADE_C = 125;
  28.     public const int OPTION_ID_GRADE_D = 126;
  29.     public const int OPTION_ID_STORAGE_TYPE_SDD = 207;
  30.     public const int OPTION_ID_STORAGE_TYPE_HDD = 208;
  31.     public const int OPTION_ID_STORAGE_TYPE_SDD_HDD = 209;
  32.     public const int OPTION_ID_OS_NONE = 210;
  33.     public const int OPTION_ID_OS_LANGUAGE_NONE = 212;
  34.     public const int OPTION_ID_KEYBOARD_REPRINT_NONE = 211;
  35.     public const int OPTION_ID_SKIN_NONE = 213;
  36.     public const array PRODUCT_GRADE_LIST = [
  37.         self::OPTION_ID_GRADE_NEW   => "New",
  38.         self::OPTION_ID_GRADE_A     => "A",
  39.         self::OPTION_ID_GRADE_B     => "B",
  40.         self::OPTION_ID_GRADE_C     => "C",
  41.         self::OPTION_ID_GRADE_D     => "D"
  42.     ];
  43.     public const array PERSONALIZED_OPTION_NONE_VALUE_LIST = [
  44.         V2Attribute::ID_OS                  => self::OPTION_ID_OS_NONE,
  45.         V2Attribute::ID_OS_LANGUAGE         => self::OPTION_ID_OS_LANGUAGE_NONE,
  46.         V2Attribute::ID_KEYBOARD_REPRINT    => self::OPTION_ID_KEYBOARD_REPRINT_NONE,
  47.         V2Attribute::ID_SKIN                => self::OPTION_ID_SKIN_NONE
  48.     ];
  49.     /**
  50.      * @var V2Attribute
  51.      *
  52.      * @Assert\NotNull
  53.      *
  54.      * @ManyToOne(targetEntity="V2Attribute", inversedBy="v2AttributeOptions")
  55.      * @JoinColumn(name="v2_attribute_id", referencedColumnName="id")
  56.      */
  57.     private $v2Attribute;
  58.     /**
  59.      * @var string
  60.      *
  61.      * @Assert\NotNull
  62.      *
  63.      * @Column(name="value", type="text")
  64.      */
  65.     private $value;
  66.     /**
  67.      * @var int
  68.      *
  69.      * @Column(name="unit", type="integer", nullable=true)
  70.      */
  71.     private $unit;
  72.     /**
  73.      * @var integer
  74.      *
  75.      * @Assert\NotNull
  76.      *
  77.      * @Column(name="sort_index", type="integer")
  78.      */
  79.     private $sortIndex = -1;
  80.     /**
  81.      * @var ArrayCollection
  82.      *
  83.      * @OneToMany(targetEntity="AttributeOptionAdditionalFieldValue", mappedBy="v2AttributeOption")
  84.      **/
  85.     private $additionalFieldValues;
  86.     /**
  87.      * @var ArrayCollection
  88.      *
  89.      * @ManyToMany(targetEntity="FixedAttributeOptionCombination", mappedBy="attributeOptions")
  90.      * @OrderBy({"id" = "ASC"})
  91.      */
  92.     private $fixedAttributeOptionCombinations;
  93.     /**
  94.      * @var ArrayCollection
  95.      *
  96.      * @ManyToMany(targetEntity="ConfigurableAttributeOptionCombination", mappedBy="attributeOptions")
  97.      * @OrderBy({"id" = "ASC"})
  98.      */
  99.     private $configurableAttributeOptionCombinations;
  100.     /**
  101.      * @var ArrayCollection
  102.      *
  103.      * @ManyToMany(targetEntity="PersonalizationAttributeOptionCombination", mappedBy="attributeOptions")
  104.      * @OrderBy({"id" = "ASC"})
  105.      */
  106.     private $personalizationAttributeOptionCombinations;
  107.     /**
  108.      * @var bool
  109.      *
  110.      * @Column(name="archived", type="boolean")
  111.      */
  112.     private $archived = 0;
  113.     public function __construct()
  114.     {
  115.         parent::__construct();
  116.         $this->additionalFieldValues = new ArrayCollection();
  117.         $this->fixedAttributeOptionCombinations = new ArrayCollection();
  118.         $this->configurableAttributeOptionCombinations = new ArrayCollection();
  119.         $this->personalizationAttributeOptionCombinations = new ArrayCollection();
  120.     }
  121.     /**
  122.      * @return string
  123.      */
  124.     public function getValue()
  125.     {
  126.         return $this->value;
  127.     }
  128.     /**
  129.      * @param string $value
  130.      */
  131.     public function setValue($value)
  132.     {
  133.         $this->value = $value;
  134.     }
  135.     /**
  136.      * @return V2Attribute
  137.      */
  138.     public function getV2Attribute()
  139.     {
  140.         return $this->v2Attribute;
  141.     }
  142.     /**
  143.      * @param V2Attribute $v2Attribute
  144.      */
  145.     public function setV2Attribute($v2Attribute)
  146.     {
  147.         $this->v2Attribute = $v2Attribute;
  148.         $v2Attribute->addOption($this);
  149.     }
  150.     /**
  151.      * @return int
  152.      */
  153.     public function getSortIndex()
  154.     {
  155.         return $this->sortIndex;
  156.     }
  157.     /**
  158.      * @param int $sortIndex
  159.      */
  160.     public function setSortIndex($sortIndex)
  161.     {
  162.         $this->sortIndex = $sortIndex;
  163.     }
  164.     /**
  165.      * @return string[]
  166.      */
  167.     public static function getUnitList(): array
  168.     {
  169.         return UnitList::getChoices();
  170.     }
  171.     public function getUnit(): ?UnitList
  172.     {
  173.         return $this->unit ? UnitList::from($this->unit) : null;
  174.     }
  175.     public function setUnit(?UnitList $unit)
  176.     {
  177.         $this->unit = $unit->value;
  178.     }
  179.     /**
  180.      * @return ArrayCollection
  181.      */
  182.     public function getAdditionalFieldValues()
  183.     {
  184.         return $this->additionalFieldValues;
  185.     }
  186.     public function getFixedAttributeOptionCombinations()
  187.     {
  188.         return $this->fixedAttributeOptionCombinations;
  189.     }
  190.     public function getConfigurableAttributeOptionCombinations()
  191.     {
  192.         return $this->configurableAttributeOptionCombinations;
  193.     }
  194.     public function getPersonalizationAttributeOptionCombinations()
  195.     {
  196.         return $this->personalizationAttributeOptionCombinations;
  197.     }
  198.     /**
  199.      * @return string
  200.      */
  201.     public function getDisplayFormattedValue(bool $showOnlyGbMb = false): string
  202.     {
  203.         $fieldType = $this->getV2Attribute()->getFieldType();
  204.         $unit = $this->getUnit()
  205.             ? $this->getUnit()->getDisplayName()
  206.             : null;
  207.         $attribute = !$showOnlyGbMb && in_array($fieldType, [V2Attribute::FIELD_TYPE_YES_NO, V2Attribute::FIELD_TYPE_NUMBER])
  208.             ? $this->getV2Attribute()->getTitle()
  209.             : null;
  210.         return trim("$attribute {$this->getValue()} $unit");
  211.     }
  212.     /**
  213.      * @return boolean
  214.      */
  215.     public function isArchived()
  216.     {
  217.         return $this->archived;
  218.     }
  219.     /**
  220.      * @param boolean $archived
  221.      */
  222.     public function setArchived($archived)
  223.     {
  224.         $this->archived = $archived;
  225.     }
  226.     /**
  227.      * @return int
  228.      */
  229.     public function getSelectedCpuGeneration(): int
  230.     {
  231.         $values = $this->getAdditionalFieldValues();
  232.         /** @var ?AttributeOptionAdditionalFieldValue $cpuGeneration */
  233.         $cpuGeneration = $values->filter(fn(AttributeOptionAdditionalFieldValue $value) => $value->getAdditionalField()->getV2AttributeDataset()?->getId() === V2Attribute::ID_CPU_GENERATION)->first();
  234.         return $cpuGeneration ? $cpuGeneration->getFieldValue() : 0;
  235.     }
  236. }