src/AtechNet/CoreBundle/Entity/ConfigurableAttributeOptionCombination.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\AtechNet\CoreBundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\ORM\Mapping\Column;
  5. use Doctrine\ORM\Mapping\Entity;
  6. use Doctrine\ORM\Mapping\JoinColumn;
  7. use Doctrine\ORM\Mapping\JoinTable;
  8. use Doctrine\ORM\Mapping\ManyToMany;
  9. use Doctrine\ORM\Mapping\OrderBy;
  10. use Doctrine\ORM\Mapping\Table as Table;
  11. use Doctrine\ORM\PersistentCollection;
  12. /**
  13.  * ConfigurableAttributeOptionCombination
  14.  *
  15.  * @Table(name="configurable_attribute_option_combinations")
  16.  * @Entity
  17.  */
  18. class ConfigurableAttributeOptionCombination extends AbstractSignedEntity
  19. {
  20.     public const array ATTRIBUTE_ORDERING_MAP = [
  21.         V2Attribute::ID_RAM_INSTALLED_SIZE,
  22.         V2Attribute::ID_STORAGE_INSTALLED_SIZE,
  23.         V2Attribute::ID_STORAGE_INSTALLED_SIZE_SSD_HDD,
  24.         V2Attribute::ID_NEW_BATTERY,
  25.         V2Attribute::ID_GRADE
  26.     ];
  27.     /**
  28.      * @var PersistentCollection
  29.      *
  30.      * @ManyToMany(targetEntity="V2AttributeOption", inversedBy="configurableAttributeOptionCombinations")
  31.      * @JoinTable(
  32.      *  name="configurable_attribute_option_combinations_attribute_options",
  33.      *  joinColumns={
  34.      *      @JoinColumn(name="configurable_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.      * @ManyToMany(targetEntity="ProductModel", mappedBy="configurableAttributeOptionCombinations")
  46.      * @OrderBy({"id" = "ASC"})
  47.      */
  48.     private $productModels;
  49.     public function __construct()
  50.     {
  51.         parent::__construct();
  52.         $this->attributeOptions = new ArrayCollection();
  53.     }
  54.     /**
  55.      * @var string
  56.      *
  57.      * @Column(name="code", type="string", unique=true, nullable=true)
  58.      */
  59.     private string $code;
  60.     /**
  61.      * @return PersistentCollection|null
  62.      */
  63.     public function getAttributeOptions(): ?PersistentCollection
  64.     {
  65.         return $this->attributeOptions;
  66.     }
  67.     /**
  68.      * @param ArrayCollection $attributeOptions
  69.      */
  70.     public function setAttributeOptions(ArrayCollection $attributeOptions)
  71.     {
  72.         $this->attributeOptions = $attributeOptions;
  73.     }
  74.     /**
  75.      * @param V2AttributeOption $attributeOption
  76.      */
  77.     public function addAttributeOption($attributeOption)
  78.     {
  79.         if (!$this->attributeOptions->contains($attributeOption)) {
  80.             $this->attributeOptions->add($attributeOption);
  81.         }
  82.     }
  83.     /**
  84.      * @param V2AttributeOption $attributeOption
  85.      */
  86.     public function removeItem($attributeOption)
  87.     {
  88.         $this->attributeOptions->removeElement($attributeOption);
  89.     }
  90.     /**
  91.      * @return ArrayCollection
  92.      */
  93.     public function getSortedAttributeOptions(): ArrayCollection
  94.     {
  95.         $orderMap = array_flip(self::ATTRIBUTE_ORDERING_MAP);
  96.         $options = $this->getAttributeOptions()->toArray();
  97.         usort($options, function ($a, $b) use ($orderMap) {
  98.             $aKey = $orderMap[$a->getV2Attribute()->getId()] ?? null;
  99.             $bKey = $orderMap[$b->getV2Attribute()->getId()] ?? null;
  100.             if ($aKey !== null && $bKey !== null) {
  101.                 return $aKey <=> $bKey;
  102.             }
  103.             if ($aKey !== null) {
  104.                 return -1;
  105.             }
  106.             if ($bKey !== null) {
  107.                 return 1;
  108.             }
  109.             return $a->getV2Attribute()->getId() <=> $b->getV2Attribute()->getId();
  110.         });
  111.         return new ArrayCollection($options);
  112.     }
  113.     /**
  114.      * @param string $separator
  115.      *
  116.      * @return string
  117.      */
  118.     public function getSortedAttributeOptionsAsDisplayName(string $separator = '|'): string
  119.     {
  120.         $options = $this->getSortedAttributeOptions();
  121.         $displayNames = [];
  122.         /** @var V2AttributeOption $option */
  123.         foreach ($options as $option) {
  124.             $displayNames[] = $option->getDisplayFormattedValue();
  125.         }
  126.         return implode(" $separator ", $displayNames);
  127.     }
  128.     /**
  129.      * @return string
  130.      */
  131.     public function getCode(): string
  132.     {
  133.         return $this->code;
  134.     }
  135.     /**
  136.      * @param string $code
  137.      * @return void
  138.      */
  139.     public function setCode(string $code): void
  140.     {
  141.         $this->code = $code;
  142.     }
  143.     public function getOptionByAttributeId(int $v2AttributeId): ?V2AttributeOption
  144.     {
  145.         $result = $this->getAttributeOptions()->filter(
  146.             fn (V2AttributeOption $vao) => $vao->getV2Attribute()->getId() === $v2AttributeId
  147.         )->first();
  148.         return $result ?: null;
  149.     }
  150.     /**
  151.      * Returns options that differ between this and another combination, keyed by attribute ID.
  152.      * Each entry: ['self' => V2AttributeOption|null, 'other' => V2AttributeOption|null]
  153.      *
  154.      * @return array<int, array{self: V2AttributeOption|null, other: V2AttributeOption|null}>
  155.      */
  156.     public function getDifferingOptions(ConfigurableAttributeOptionCombination $other): array
  157.     {
  158.         $selfMap  = [];
  159.         $otherMap = [];
  160.         /** @var V2AttributeOption $option */
  161.         foreach ($this->getAttributeOptions() ?? [] as $option) {
  162.             $selfMap[$option->getV2Attribute()->getId()] = $option;
  163.         }
  164.         /** @var V2AttributeOption $option */
  165.         foreach ($other->getAttributeOptions() ?? [] as $option) {
  166.             $otherMap[$option->getV2Attribute()->getId()] = $option;
  167.         }
  168.         $allAttributeIds = array_unique(array_merge(array_keys($selfMap), array_keys($otherMap)));
  169.         $differences = [];
  170.         foreach ($allAttributeIds as $attrId) {
  171.             /** @var V2AttributeOption|null $selfOption */
  172.             $selfOption  = $selfMap[$attrId]  ?? null;
  173.             /** @var V2AttributeOption|null $otherOption */
  174.             $otherOption = $otherMap[$attrId] ?? null;
  175.             if ($selfOption?->getId() !== $otherOption?->getId()) {
  176.                 $differences[$attrId] = [
  177.                     'self'  => $selfOption,
  178.                     'other' => $otherOption,
  179.                 ];
  180.             }
  181.         }
  182.         return $differences;
  183.     }
  184.     /**
  185.      * @return array
  186.      */
  187.     public function getAttributeOptionIds(): array
  188.     {
  189.         return $this->getAttributeOptions()->map(function ($option) {
  190.             return $option->getId();
  191.         })->toArray();
  192.     }
  193.     public function getProductModels()
  194.     {
  195.         return $this->productModels;
  196.     }
  197.     public function setProductModels($productModels): void
  198.     {
  199.         $this->productModels = $productModels;
  200.     }
  201. }