src/AtechNet/CoreBundle/Entity/PersonalizationAttributeOptionCombination.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\AtechNet\CoreBundle\Entity;
  3. use App\AtechNet\CoreBundle\Repository\PersonalizationAttributeOptionCombinationRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\ORM\Mapping\Column;
  6. use Doctrine\ORM\Mapping\Entity;
  7. use Doctrine\ORM\Mapping\JoinColumn;
  8. use Doctrine\ORM\Mapping\JoinTable;
  9. use Doctrine\ORM\Mapping\ManyToMany;
  10. use Doctrine\ORM\Mapping\OneToMany;
  11. use Doctrine\ORM\Mapping\OneToOne;
  12. use Doctrine\ORM\Mapping\OrderBy;
  13. use Doctrine\ORM\Mapping\Table as Table;
  14. use Doctrine\ORM\PersistentCollection;
  15. /**
  16.  * @Table(name="personalization_attribute_option_combinations")
  17.  * @Entity(repositoryClass=PersonalizationAttributeOptionCombinationRepository::class)
  18.  */
  19. class PersonalizationAttributeOptionCombination extends AbstractSignedEntity
  20. {
  21.     public const TOTAL_NONE_COMBINATION_ID = 22080;
  22.     public const array ATTRIBUTE_ORDERING_MAP = [
  23.         V2Attribute::ID_OS,
  24.         V2Attribute::ID_OS_LANGUAGE,
  25.         V2Attribute::ID_KEYBOARD_REPRINT,
  26.         V2Attribute::ID_SKIN,
  27.     ];
  28.     public const array NONE_ATTRIBUTE_OPTIONS = [
  29.         V2AttributeOption::OPTION_ID_OS_NONE,
  30.         V2AttributeOption::OPTION_ID_OS_LANGUAGE_NONE,
  31.         V2AttributeOption::OPTION_ID_KEYBOARD_REPRINT_NONE,
  32.         V2AttributeOption::OPTION_ID_SKIN_NONE,
  33.     ];
  34.     /**
  35.      * @var PersistentCollection
  36.      *
  37.      * @ManyToMany(targetEntity="V2AttributeOption", inversedBy="personalizationAttributeOptionCombinations")
  38.      * @JoinTable(
  39.      *  name="personalization_attribute_option_combination_attribute_options",
  40.      *  joinColumns={
  41.      *      @JoinColumn(name="personalization_attribute_option_combination_id", referencedColumnName="id", onDelete="CASCADE")
  42.      *  },
  43.      *  inverseJoinColumns={
  44.      *      @JoinColumn(name="v2_attribute_option_id", referencedColumnName="id")
  45.      *  })
  46.      * @OrderBy({"created" = "DESC"})
  47.      */
  48.     private $attributeOptions;
  49.     /**
  50.      * @var string
  51.      *
  52.      * @Column(name="code", type="string", unique=true, nullable=true)
  53.      */
  54.     private string $code;
  55.     /**
  56.      * @var PersistentCollection
  57.      *
  58.      * @OneToMany(targetEntity="StockMovementItem", mappedBy="personalizationAttributeOptionCombination")
  59.      */
  60.     private $stockMovementItems;
  61.     /**
  62.      * @var PersistentCollection
  63.      *
  64.      * @OneToMany(targetEntity="OrderItem", mappedBy="personalizationAttributeOptionCombination")
  65.      */
  66.     private $orderItems;
  67.     public function __construct()
  68.     {
  69.         parent::__construct();
  70.         $this->attributeOptions   = new ArrayCollection();
  71.         $this->stockMovementItems = new ArrayCollection();
  72.         $this->orderItems         = new ArrayCollection();
  73.     }
  74.     public function getAttributeOptions(): ?PersistentCollection
  75.     {
  76.         return $this->attributeOptions;
  77.     }
  78.     public function setAttributeOptions(ArrayCollection $attributeOptions): void
  79.     {
  80.         $this->attributeOptions = $attributeOptions;
  81.     }
  82.     public function addAttributeOption(V2AttributeOption $attributeOption): void
  83.     {
  84.         if (!$this->attributeOptions->contains($attributeOption)) {
  85.             $this->attributeOptions->add($attributeOption);
  86.         }
  87.     }
  88.     public function removeItem(V2AttributeOption $attributeOption): void
  89.     {
  90.         $this->attributeOptions->removeElement($attributeOption);
  91.     }
  92.     public function getCode(): string
  93.     {
  94.         return $this->code;
  95.     }
  96.     public function setCode(string $code): void
  97.     {
  98.         $this->code = $code;
  99.     }
  100.     public function getSortedAttributeOptions(): ArrayCollection
  101.     {
  102.         $orderMap = array_flip(self::ATTRIBUTE_ORDERING_MAP);
  103.         $options = $this->getAttributeOptions()->toArray();
  104.         usort($options, function ($a, $b) use ($orderMap) {
  105.             $aKey = $orderMap[$a->getV2Attribute()->getId()] ?? PHP_INT_MAX;
  106.             $bKey = $orderMap[$b->getV2Attribute()->getId()] ?? PHP_INT_MAX;
  107.             return $aKey <=> $bKey;
  108.         });
  109.         return new ArrayCollection($options);
  110.     }
  111.     public function getSortedAttributeOptionsAsDisplayName(string $separator = '|'): string
  112.     {
  113.         $options  = $this->getSortedAttributeOptions();
  114.         $names    = [];
  115.         /** @var V2AttributeOption $option */
  116.         foreach ($options as $option) {
  117.             $names[] = $option->getDisplayFormattedValue();
  118.         }
  119.         return implode(" $separator ", $names);
  120.     }
  121.     /**
  122.      * @param string $separator
  123.      * @return string
  124.      */
  125.     public function getSortedAttributeOptionsWithAttributeNameAsDisplayName(string $separator = '|'): string
  126.     {
  127.         $options = $this->getSortedAttributeOptions();
  128.         $names   = [];
  129.         /** @var V2AttributeOption $option */
  130.         foreach ($options as $option) {
  131.             $unit = $option->getUnit()?->getDisplayName();
  132.             $names[] = trim($option->getV2Attribute()->getTitle() . ' ' . $option->getValue() . ' ' . $unit);
  133.         }
  134.         return implode(" $separator ", $names);
  135.     }
  136.     public function getAttributeOptionIds(): array
  137.     {
  138.         return $this->getAttributeOptions()
  139.             ->map(fn (V2AttributeOption $o) => $o->getId())
  140.             ->toArray();
  141.     }
  142.     public function getOptionByAttributeId(int $v2AttributeId): ?V2AttributeOption
  143.     {
  144.         $result = $this->getAttributeOptions()->filter(
  145.             fn (V2AttributeOption $vao) => $vao->getV2Attribute()->getId() === $v2AttributeId
  146.         )->first();
  147.         return $result ?: null;
  148.     }
  149.     public function getStockMovementItems(): ArrayCollection
  150.     {
  151.         return $this->stockMovementItems;
  152.     }
  153.     public function addStockMovementItem(StockMovementItem $stockMovementItem): void
  154.     {
  155.         if (!$this->stockMovementItems->contains($stockMovementItem)) {
  156.             $this->stockMovementItems->add($stockMovementItem);
  157.         }
  158.     }
  159.     public function removeStockMovementItem(StockMovementItem $stockMovementItem): void
  160.     {
  161.         $this->stockMovementItems->removeElement($stockMovementItem);
  162.     }
  163.     public function getOrderItems(): ArrayCollection
  164.     {
  165.         return $this->orderItems;
  166.     }
  167.     public function addOrderItem(OrderItem $orderItem): void
  168.     {
  169.         if (!$this->orderItems->contains($orderItem)) {
  170.             $this->orderItems->add($orderItem);
  171.         }
  172.     }
  173.     public function removeOrderItem(OrderItem $orderItem): void
  174.     {
  175.         $this->orderItems->removeElement($orderItem);
  176.     }
  177.     /**
  178.      * Returns options that differ between this and another combination, keyed by attribute ID.
  179.      * Each entry: ['self' => V2AttributeOption|null, 'other' => V2AttributeOption|null]
  180.      *
  181.      * @return array<int, array{self: V2AttributeOption|null, other: V2AttributeOption|null}>
  182.      */
  183.     public function getDifferingOptions(?PersonalizationAttributeOptionCombination $other): array
  184.     {
  185.         $selfMap  = [];
  186.         $otherMap = [];
  187.         if (!$other) {
  188.             return [];
  189.         }
  190.         /** @var V2AttributeOption $option */
  191.         foreach ($this->getAttributeOptions() ?? [] as $option) {
  192.             $selfMap[$option->getV2Attribute()->getId()] = $option;
  193.         }
  194.         /** @var V2AttributeOption $option */
  195.         foreach ($other->getAttributeOptions() ?? [] as $option) {
  196.             $otherMap[$option->getV2Attribute()->getId()] = $option;
  197.         }
  198.         $allAttributeIds = array_unique(array_merge(array_keys($selfMap), array_keys($otherMap)));
  199.         $differences = [];
  200.         foreach ($allAttributeIds as $attrId) {
  201.             /** @var V2AttributeOption|null $selfOption */
  202.             $selfOption  = $selfMap[$attrId]  ?? null;
  203.             /** @var V2AttributeOption|null $otherOption */
  204.             $otherOption = $otherMap[$attrId] ?? null;
  205.             if ($selfOption?->getId() !== $otherOption?->getId()) {
  206.                 $differences[$attrId] = [
  207.                     'self'  => $selfOption,
  208.                     'other' => $otherOption,
  209.                 ];
  210.             }
  211.         }
  212.         return $differences;
  213.     }
  214. }