|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file has been created by developers from BitBag. |
|
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
|
6
|
|
|
* another great project. |
|
7
|
|
|
* You can find more information about us on https://bitbag.shop and write us |
|
8
|
|
|
* an email on [email protected]. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder; |
|
14
|
|
|
|
|
15
|
|
|
use BitBag\SyliusElasticsearchPlugin\Formatter\StringFormatterInterface; |
|
16
|
|
|
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface; |
|
17
|
|
|
use Elastica\Document; |
|
18
|
|
|
use FOS\ElasticaBundle\Event\TransformEvent; |
|
19
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
|
20
|
|
|
use Sylius\Component\Locale\Context\LocaleContextInterface; |
|
21
|
|
|
|
|
22
|
|
|
final class AttributeBuilder extends AbstractBuilder |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var ConcatedNameResolverInterface */ |
|
25
|
|
|
private $attributeNameResolver; |
|
26
|
|
|
|
|
27
|
|
|
/** @var StringFormatterInterface */ |
|
28
|
|
|
private $stringFormatter; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var LocaleContextInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $localeContext; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct( |
|
35
|
|
|
ConcatedNameResolverInterface $attributeNameResolver, |
|
36
|
|
|
StringFormatterInterface $stringFormatter, |
|
37
|
|
|
LocaleContextInterface $localeContext |
|
38
|
|
|
) { |
|
39
|
|
|
$this->attributeNameResolver = $attributeNameResolver; |
|
40
|
|
|
$this->stringFormatter = $stringFormatter; |
|
41
|
|
|
$this->localeContext = $localeContext; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function consumeEvent(TransformEvent $event): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->buildProperty($event, ProductInterface::class, |
|
47
|
|
|
function (ProductInterface $product, Document $document): void { |
|
48
|
|
|
$this->resolveProductAttributes($product, $document); |
|
49
|
|
|
}); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function resolveProductAttributes(ProductInterface $product, Document $document): void |
|
53
|
|
|
{ |
|
54
|
|
|
foreach ($product->getAttributes() as $attributeValue) { |
|
55
|
|
|
$attribute = $attributeValue->getAttribute(); |
|
56
|
|
|
if (!$attribute) { |
|
57
|
|
|
continue; |
|
58
|
|
|
} |
|
59
|
|
|
$attributeCode = $attribute->getCode(); |
|
60
|
|
|
$index = $this->attributeNameResolver->resolvePropertyName($attributeCode); |
|
61
|
|
|
$value = $attributeValue->getValue(); |
|
62
|
|
|
if ($attribute->getType() === 'select') { |
|
63
|
|
|
$choices = $attribute->getConfiguration()['choices']; |
|
64
|
|
|
if (is_array($value)) { |
|
65
|
|
|
foreach ($value as $i => $item) { |
|
66
|
|
|
$value[$i] = $choices[$item][$this->localeContext->getLocaleCode()] ?? $item; |
|
67
|
|
|
} |
|
68
|
|
|
} else { |
|
69
|
|
|
$value = $choices[$value][$this->localeContext->getLocaleCode()] ?? $value; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
$attributes = []; |
|
74
|
|
|
|
|
75
|
|
|
if (is_array($value)) { |
|
76
|
|
|
foreach ($value as $singleElement) { |
|
77
|
|
|
$attributes[] = $this->stringFormatter->formatToLowercaseWithoutSpaces((string) $singleElement); |
|
78
|
|
|
} |
|
79
|
|
|
} else { |
|
80
|
|
|
$value = is_string($value) ? $this->stringFormatter->formatToLowercaseWithoutSpaces($value) : $value; |
|
81
|
|
|
$attributes[] = $value; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$document->set($index, $attributes); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|