src/AtechNet/CoreBundle/Entity/FixedAttributeOptionCombination.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\AtechNet\CoreBundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\ORM\PersistentCollection;
  5. use Doctrine\ORM\Mapping\ManyToMany;
  6. use Doctrine\ORM\Mapping\Entity;
  7. use Doctrine\ORM\Mapping\JoinTable;
  8. use Doctrine\ORM\Mapping\JoinColumn;
  9. use Doctrine\ORM\Mapping\Column;
  10. use Doctrine\ORM\Mapping\OrderBy;
  11. use Doctrine\ORM\Mapping\Table as Table;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. use Doctrine\ORM\Mapping\OneToMany;
  14. /**
  15.  * FixedAttributeOptionCombination
  16.  *
  17.  * @Table(name="fixed_attribute_option_combinations")
  18.  * @Entity
  19.  */
  20. class FixedAttributeOptionCombination extends AbstractSignedEntity
  21. {
  22.     public const array ATTRIBUTE_ORDERING_MAP = [
  23.         V2Attribute::ID_CPU,
  24.         V2Attribute::ID_GPU,
  25.         V2Attribute::ID_DISPLAY
  26.     ];
  27.     /**
  28.      * @var PersistentCollection
  29.      *
  30.      * @ManyToMany(targetEntity="V2AttributeOption", inversedBy="fixedAttributeOptionCombinations")
  31.      * @JoinTable(
  32.      *  name="fixed_attribute_option_combination_attribute_options",
  33.      *  joinColumns={
  34.      *      @JoinColumn(name="fixed_attribute_option_combination_id", referencedColumnName="id", onDelete="CASCADE")
  35.      *  },
  36.      *  inverseJoinColumns={
  37.      *      @JoinColumn(name="v2_attribute_option_id", referencedColumnName="id")
  38.      *  })
  39.      * @OrderBy({"created" = "DESC"})
  40.      */
  41.     private $attributeOptions;
  42.     /**
  43.      * @var ArrayCollection
  44.      *
  45.      * @Assert\Valid
  46.      *
  47.      * @OneToMany(targetEntity="Product", mappedBy="fixedAttributeOptionCombination")
  48.      **/
  49.     private $products;
  50.     /**
  51.      * @var string
  52.      *
  53.      * @Column(name="code", type="string", unique=true, nullable=true)
  54.      */
  55.     private string $code;
  56.     /**
  57.      * @var ArrayCollection
  58.      *
  59.      * @ManyToMany(targetEntity="ProductModel", mappedBy="fixedAttributeOptionCombinations")
  60.      * @OrderBy({"id" = "ASC"})
  61.      */
  62.     private $productModels;
  63.     public function __construct()
  64.     {
  65.         parent::__construct();
  66.         $this->attributeOptions = new ArrayCollection();
  67.         $this->products = new ArrayCollection();
  68.         $this->productModels = new ArrayCollection();
  69.     }
  70.     /**
  71.      * @return PersistentCollection|null
  72.      */
  73.     public function getAttributeOptions(): ?PersistentCollection
  74.     {
  75.         return $this->attributeOptions;
  76.     }
  77.     public function getSortedAttributeOptions(): ArrayCollection
  78.     {
  79.         $orderMap = array_flip(self::ATTRIBUTE_ORDERING_MAP);
  80.         $options = $this->getAttributeOptions()->toArray();
  81.         usort($options, function ($a, $b) use ($orderMap) {
  82.             $aKey = $orderMap[$a->getV2Attribute()->getId()] ?? PHP_INT_MAX;
  83.             $bKey = $orderMap[$b->getV2Attribute()->getId()] ?? PHP_INT_MAX;
  84.             return $aKey <=> $bKey;
  85.         });
  86.         return new ArrayCollection($options);
  87.     }
  88.     public function getSortedAttributeOptionsAsDisplayName(): string
  89.     {
  90.         $options = $this->getSortedAttributeOptions();
  91.         $displayNames = [];
  92.         /** @var V2AttributeOption $option */
  93.         foreach ($options as $option) {
  94.             $displayNames[] = $option->getDisplayFormattedValue();
  95.         }
  96.         return implode(' | ', $displayNames);
  97.     }
  98.     public function getSortedAttributeOptionIdList(): array
  99.     {
  100.         $orderMap = array_flip(self::ATTRIBUTE_ORDERING_MAP);
  101.         $options = $this->getAttributeOptions()->toArray();
  102.         usort($options, function ($a, $b) use ($orderMap) {
  103.             $aKey = $orderMap[$a->getV2Attribute()->getId()] ?? PHP_INT_MAX;
  104.             $bKey = $orderMap[$b->getV2Attribute()->getId()] ?? PHP_INT_MAX;
  105.             return $aKey <=> $bKey;
  106.         });
  107.         $sortedArray = [];
  108.         foreach ($options as $optionValue) {
  109.             $sortedArray[] = $optionValue->getId();
  110.         }
  111.         return $sortedArray;
  112.     }
  113.     public function getSortedAttributeOptionText(string $separator = ' | '): string
  114.     {
  115.         $orderMap = array_flip(self::ATTRIBUTE_ORDERING_MAP);
  116.         $options = $this->getAttributeOptions()->toArray();
  117.         usort($options, function ($a, $b) use ($orderMap) {
  118.             $aKey = $orderMap[$a->getV2Attribute()->getId()] ?? PHP_INT_MAX;
  119.             $bKey = $orderMap[$b->getV2Attribute()->getId()] ?? PHP_INT_MAX;
  120.             return $aKey <=> $bKey;
  121.         });
  122.         $sortedArray = [];
  123.         foreach ($options as $optionValue) {
  124.             $sortedArray[] = $optionValue->getValue();
  125.         }
  126.         return implode($separator, $sortedArray);
  127.     }
  128.     public function getOptionByAttributeId(int $v2Attribute): ?V2AttributeOption
  129.     {
  130.         $result = $this->getAttributeOptions()->filter(
  131.             fn (V2AttributeOption $vao) => $vao->getV2Attribute()->getId() === $v2Attribute
  132.         )->first();
  133.         return $result ?: null;
  134.     }
  135.     /**
  136.      * @param ArrayCollection $attributeOptions
  137.      */
  138.     public function setAttributeOptions(ArrayCollection $attributeOptions)
  139.     {
  140.         $this->attributeOptions = $attributeOptions;
  141.     }
  142.     /**
  143.      * @param V2AttributeOption $attributeOption
  144.      */
  145.     public function addAttributeOption($attributeOption)
  146.     {
  147.         if (!$this->attributeOptions->contains($attributeOption)) {
  148.             $this->attributeOptions->add($attributeOption);
  149.         }
  150.     }
  151.     /**
  152.      * @param V2AttributeOption $attributeOption
  153.      */
  154.     public function removeItem($attributeOption)
  155.     {
  156.         $this->attributeOptions->removeElement($attributeOption);
  157.     }
  158.     /**
  159.      * @return PersistentCollection|null
  160.      */
  161.     public function getProducts(): ?PersistentCollection
  162.     {
  163.         return $this->products;
  164.     }
  165.     /**
  166.      * @return string
  167.      */
  168.     public function getCode(): string
  169.     {
  170.         return $this->code;
  171.     }
  172.     /**
  173.      * @param string $code
  174.      * @return void
  175.      */
  176.     public function setCode(string $code): void
  177.     {
  178.         $this->code = $code;
  179.     }
  180.     public function getProductModels()
  181.     {
  182.         return $this->productModels;
  183.     }
  184.     public function setProductModels($productModels): void
  185.     {
  186.         $this->productModels = $productModels;
  187.     }
  188. }