vendor/bitbag/elasticsearch-plugin/src/PropertyBuilder/AttributeBuilder.php line 45

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\PropertyBuilder;
  11. use BitBag\SyliusElasticsearchPlugin\Formatter\StringFormatterInterface;
  12. use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
  13. use Elastica\Document;
  14. use FOS\ElasticaBundle\Event\PostTransformEvent;
  15. use function sprintf;
  16. use Sylius\Component\Attribute\AttributeType\DateAttributeType;
  17. use Sylius\Component\Attribute\AttributeType\DatetimeAttributeType;
  18. use Sylius\Component\Attribute\AttributeType\SelectAttributeType;
  19. use Sylius\Component\Attribute\Model\AttributeInterface;
  20. use Sylius\Component\Core\Model\ProductInterface;
  21. use Sylius\Component\Product\Model\ProductAttributeValue;
  22. final class AttributeBuilder extends AbstractBuilder
  23. {
  24.     public const DEFAULT_DATE_TIME_FORMAT 'Y-m-d H:i:s';
  25.     public const DEFAULT_DATE_FORMAT 'Y-m-d';
  26.     private ConcatedNameResolverInterface $attributeNameResolver;
  27.     private StringFormatterInterface $stringFormatter;
  28.     public function __construct(
  29.         ConcatedNameResolverInterface $attributeNameResolver,
  30.         StringFormatterInterface $stringFormatter
  31.     ) {
  32.         $this->attributeNameResolver $attributeNameResolver;
  33.         $this->stringFormatter $stringFormatter;
  34.     }
  35.     public function consumeEvent(PostTransformEvent $event): void
  36.     {
  37.         $this->buildProperty(
  38.             $event,
  39.             ProductInterface::class,
  40.             function (ProductInterface $productDocument $document): void {
  41.                 $this->resolveProductAttributes($product$document);
  42.             }
  43.         );
  44.     }
  45.     private function resolveProductAttributes(ProductInterface $productDocument $document): void
  46.     {
  47.         foreach ($product->getAttributes() as $productAttribute) {
  48.             $attribute $productAttribute->getAttribute();
  49.             if (!$attribute) {
  50.                 continue;
  51.             }
  52.             $this->processAttribute($attribute$productAttribute$document);
  53.         }
  54.     }
  55.     private function resolveProductAttribute(
  56.         array $attributeConfiguration,
  57.         $attributeValue,
  58.         ProductAttributeValue $productAttribute
  59.     ): array {
  60.         if (SelectAttributeType::TYPE === $productAttribute->getAttribute()->getType()) {
  61.             $choices $attributeConfiguration['choices'];
  62.             if (is_array($attributeValue)) {
  63.                 foreach ($attributeValue as $i => $item) {
  64.                     $attributeValue[$i] = $choices[$item][$productAttribute->getLocaleCode()] ?? $item;
  65.                 }
  66.             } else {
  67.                 $attributeValue $choices[$attributeValue][$productAttribute->getLocaleCode()] ?? $attributeValue;
  68.             }
  69.         }
  70.         $attributes = [];
  71.         if (is_array($attributeValue)) {
  72.             foreach ($attributeValue as $singleElement) {
  73.                 $attributes[] = $this->stringFormatter->formatToLowercaseWithoutSpaces((string) $singleElement);
  74.             }
  75.         } else {
  76.             switch (true) {
  77.                 case is_string($attributeValue):
  78.                     $attributes[] = $this->stringFormatter->formatToLowercaseWithoutSpaces($attributeValue);
  79.                     break;
  80.                 case $attributeValue instanceof \DateTime:
  81.                     $attributeFormat $productAttribute->getAttribute()->getConfiguration()['format'] ?? null;
  82.                     $defaultFormat DateAttributeType::TYPE === $productAttribute->getAttribute()->getStorageType() ?
  83.                         self::DEFAULT_DATE_FORMAT :
  84.                         self::DEFAULT_DATE_TIME_FORMAT
  85.                     ;
  86.                     $attributes[] = $attributeValue->format($attributeFormat ?? $defaultFormat);
  87.                     break;
  88.                 default:
  89.                     $attributes[] = $attributeValue;
  90.             }
  91.         }
  92.         return $attributes;
  93.     }
  94.     private function processAttribute(
  95.         AttributeInterface $attribute,
  96.         ProductAttributeValue $productAttribute,
  97.         Document $document
  98.     ): void {
  99.         $attributeCode $attribute->getCode();
  100.         $attributeConfiguration $attribute->getConfiguration();
  101.         $value $productAttribute->getValue();
  102.         $documentKey $this->attributeNameResolver->resolvePropertyName($attributeCode);
  103.         $code sprintf('%s_%s'$documentKey$productAttribute->getLocaleCode());
  104.         $values $this->resolveProductAttribute(
  105.             $attributeConfiguration,
  106.             $value,
  107.             $productAttribute
  108.         );
  109.         $values in_array($attribute->getStorageType(), [DateAttributeType::TYPEDatetimeAttributeType::TYPE]) ?
  110.             ($values[0] ?? $values) :
  111.             $values
  112.         ;
  113.         $document->set($code$values);
  114.     }
  115. }