vendor/bitbag/elasticsearch-plugin/src/Form/Type/ChoiceMapper/ProductAttributesMapper.php line 92

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file has been created by developers from BitBag.
  4.  * Feel free to contact us once you face any issues or want to start
  5.  * another great project.
  6.  * You can find more information about us on https://bitbag.io and write us
  7.  * an email on hello@bitbag.io.
  8.  */
  9. declare(strict_types=1);
  10. namespace BitBag\SyliusElasticsearchPlugin\Form\Type\ChoiceMapper;
  11. use BitBag\SyliusElasticsearchPlugin\Context\TaxonContextInterface;
  12. use BitBag\SyliusElasticsearchPlugin\Form\Type\ChoiceMapper\AttributesMapper\AttributesMapperCollectorInterface;
  13. use BitBag\SyliusElasticsearchPlugin\Formatter\StringFormatterInterface;
  14. use BitBag\SyliusElasticsearchPlugin\Repository\ProductAttributeValueRepositoryInterface;
  15. use Sylius\Component\Attribute\AttributeType\SelectAttributeType;
  16. use Sylius\Component\Locale\Context\LocaleContextInterface;
  17. use Sylius\Component\Product\Model\ProductAttributeInterface;
  18. final class ProductAttributesMapper implements ProductAttributesMapperInterface
  19. {
  20.     private ProductAttributeValueRepositoryInterface $productAttributeValueRepository;
  21.     private LocaleContextInterface $localeContext;
  22.     private StringFormatterInterface $stringFormatter;
  23.     private TaxonContextInterface $taxonContext;
  24.     /** @var AttributesMapperCollectorInterface[] */
  25.     private iterable $attributeMapper;
  26.     public function __construct(
  27.         ProductAttributeValueRepositoryInterface $productAttributeValueRepository,
  28.         LocaleContextInterface $localeContext,
  29.         StringFormatterInterface $stringFormatter,
  30.         TaxonContextInterface $taxonContext,
  31.         iterable $attributeMapper
  32.     ) {
  33.         $this->productAttributeValueRepository $productAttributeValueRepository;
  34.         $this->localeContext $localeContext;
  35.         $this->stringFormatter $stringFormatter;
  36.         $this->taxonContext $taxonContext;
  37.         $this->attributeMapper $attributeMapper;
  38.     }
  39.     public function mapToChoices(ProductAttributeInterface $productAttribute): array
  40.     {
  41.         $configuration $productAttribute->getConfiguration();
  42.         if (isset($configuration['choices']) && is_array($configuration['choices'])) {
  43.             $choices = [];
  44.             foreach ($configuration['choices'] as $singleValue => $val) {
  45.                 $label $configuration['choices'][$singleValue][$this->localeContext->getLocaleCode()];
  46.                 $singleValue SelectAttributeType::TYPE === $productAttribute->getType() ? $label $singleValue;
  47.                 $choice $this->stringFormatter->formatToLowercaseWithoutSpaces($singleValue);
  48.                 $choices[$label] = $choice;
  49.             }
  50.             return $choices;
  51.         }
  52.         $taxon $this->taxonContext->getTaxon();
  53.         $attributeValues $this->productAttributeValueRepository->getUniqueAttributeValues($productAttribute$taxon);
  54.         foreach ($this->attributeMapper as $mapper) {
  55.             if ($mapper->supports($productAttribute->getType())) {
  56.                 return $mapper->map($attributeValues);
  57.             }
  58.         }
  59.         $choices = [];
  60.         array_walk($attributeValues, function ($productAttributeValue) use (&$choices$productAttribute): void {
  61.             $value $productAttributeValue['value'];
  62.             $configuration $productAttribute->getConfiguration();
  63.             if (is_array($value)
  64.                 && isset($configuration['choices'])
  65.                 && is_array($configuration['choices'])
  66.             ) {
  67.                 foreach ($value as $singleValue) {
  68.                     $choice $this->stringFormatter->formatToLowercaseWithoutSpaces($singleValue);
  69.                     $label $configuration['choices'][$singleValue][$this->localeContext->getLocaleCode()];
  70.                     $choices[$label] = $choice;
  71.                 }
  72.             } else {
  73.                 $choice is_string($value) ? $this->stringFormatter->formatToLowercaseWithoutSpaces($value) : $value;
  74.                 $choice is_bool($value) ? var_export($valuetrue) : $choice;
  75.                 $choices[$value] = $choice;
  76.             }
  77.         });
  78.         unset($attributeValues);
  79.         return $choices;
  80.     }
  81. }