<?php
namespace App\AtechNet\CoreBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\Mapping\ManyToMany;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\JoinTable;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\OrderBy;
use Doctrine\ORM\Mapping\Table as Table;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping\OneToMany;
/**
* FixedAttributeOptionCombination
*
* @Table(name="fixed_attribute_option_combinations")
* @Entity
*/
class FixedAttributeOptionCombination extends AbstractSignedEntity
{
public const array ATTRIBUTE_ORDERING_MAP = [
V2Attribute::ID_CPU,
V2Attribute::ID_GPU,
V2Attribute::ID_DISPLAY
];
/**
* @var PersistentCollection
*
* @ManyToMany(targetEntity="V2AttributeOption", inversedBy="fixedAttributeOptionCombinations")
* @JoinTable(
* name="fixed_attribute_option_combination_attribute_options",
* joinColumns={
* @JoinColumn(name="fixed_attribute_option_combination_id", referencedColumnName="id", onDelete="CASCADE")
* },
* inverseJoinColumns={
* @JoinColumn(name="v2_attribute_option_id", referencedColumnName="id")
* })
* @OrderBy({"created" = "DESC"})
*/
private $attributeOptions;
/**
* @var ArrayCollection
*
* @Assert\Valid
*
* @OneToMany(targetEntity="Product", mappedBy="fixedAttributeOptionCombination")
**/
private $products;
/**
* @var string
*
* @Column(name="code", type="string", unique=true, nullable=true)
*/
private string $code;
/**
* @var ArrayCollection
*
* @ManyToMany(targetEntity="ProductModel", mappedBy="fixedAttributeOptionCombinations")
* @OrderBy({"id" = "ASC"})
*/
private $productModels;
public function __construct()
{
parent::__construct();
$this->attributeOptions = new ArrayCollection();
$this->products = new ArrayCollection();
$this->productModels = new ArrayCollection();
}
/**
* @return PersistentCollection|null
*/
public function getAttributeOptions(): ?PersistentCollection
{
return $this->attributeOptions;
}
public function getSortedAttributeOptions(): ArrayCollection
{
$orderMap = array_flip(self::ATTRIBUTE_ORDERING_MAP);
$options = $this->getAttributeOptions()->toArray();
usort($options, function ($a, $b) use ($orderMap) {
$aKey = $orderMap[$a->getV2Attribute()->getId()] ?? PHP_INT_MAX;
$bKey = $orderMap[$b->getV2Attribute()->getId()] ?? PHP_INT_MAX;
return $aKey <=> $bKey;
});
return new ArrayCollection($options);
}
public function getSortedAttributeOptionsAsDisplayName(): string
{
$options = $this->getSortedAttributeOptions();
$displayNames = [];
/** @var V2AttributeOption $option */
foreach ($options as $option) {
$displayNames[] = $option->getDisplayFormattedValue();
}
return implode(' | ', $displayNames);
}
public function getSortedAttributeOptionIdList(): array
{
$orderMap = array_flip(self::ATTRIBUTE_ORDERING_MAP);
$options = $this->getAttributeOptions()->toArray();
usort($options, function ($a, $b) use ($orderMap) {
$aKey = $orderMap[$a->getV2Attribute()->getId()] ?? PHP_INT_MAX;
$bKey = $orderMap[$b->getV2Attribute()->getId()] ?? PHP_INT_MAX;
return $aKey <=> $bKey;
});
$sortedArray = [];
foreach ($options as $optionValue) {
$sortedArray[] = $optionValue->getId();
}
return $sortedArray;
}
public function getSortedAttributeOptionText(string $separator = ' | '): string
{
$orderMap = array_flip(self::ATTRIBUTE_ORDERING_MAP);
$options = $this->getAttributeOptions()->toArray();
usort($options, function ($a, $b) use ($orderMap) {
$aKey = $orderMap[$a->getV2Attribute()->getId()] ?? PHP_INT_MAX;
$bKey = $orderMap[$b->getV2Attribute()->getId()] ?? PHP_INT_MAX;
return $aKey <=> $bKey;
});
$sortedArray = [];
foreach ($options as $optionValue) {
$sortedArray[] = $optionValue->getValue();
}
return implode($separator, $sortedArray);
}
public function getOptionByAttributeId(int $v2Attribute): ?V2AttributeOption
{
$result = $this->getAttributeOptions()->filter(
fn (V2AttributeOption $vao) => $vao->getV2Attribute()->getId() === $v2Attribute
)->first();
return $result ?: null;
}
/**
* @param ArrayCollection $attributeOptions
*/
public function setAttributeOptions(ArrayCollection $attributeOptions)
{
$this->attributeOptions = $attributeOptions;
}
/**
* @param V2AttributeOption $attributeOption
*/
public function addAttributeOption($attributeOption)
{
if (!$this->attributeOptions->contains($attributeOption)) {
$this->attributeOptions->add($attributeOption);
}
}
/**
* @param V2AttributeOption $attributeOption
*/
public function removeItem($attributeOption)
{
$this->attributeOptions->removeElement($attributeOption);
}
/**
* @return PersistentCollection|null
*/
public function getProducts(): ?PersistentCollection
{
return $this->products;
}
/**
* @return string
*/
public function getCode(): string
{
return $this->code;
}
/**
* @param string $code
* @return void
*/
public function setCode(string $code): void
{
$this->code = $code;
}
public function getProductModels()
{
return $this->productModels;
}
public function setProductModels($productModels): void
{
$this->productModels = $productModels;
}
}