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\Attribute\Model\AttributeTranslation; |
20
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
21
|
|
|
use Sylius\Component\Locale\Context\LocaleContextInterface; |
22
|
|
|
|
23
|
|
|
final class AttributeBuilder extends AbstractBuilder |
24
|
|
|
{ |
25
|
|
|
/** @var ConcatedNameResolverInterface */ |
26
|
|
|
private $attributeNameResolver; |
27
|
|
|
|
28
|
|
|
/** @var StringFormatterInterface */ |
29
|
|
|
private $stringFormatter; |
30
|
|
|
|
31
|
|
|
/** @var LocaleContextInterface */ |
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
|
|
|
|
57
|
|
|
if (!$attribute) { |
58
|
|
|
continue; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$attributeValueCode = $attributeValue->getLocaleCode(); |
62
|
|
|
$attributeCode = $attribute->getCode(); |
63
|
|
|
|
64
|
|
|
$attributeConfiguration = $attribute->getConfiguration(); |
65
|
|
|
foreach ($attribute->getTranslations() as $attributeTranslation) { |
66
|
|
|
if ($attributeValueCode !== $attributeTranslation->getLocale()) { |
67
|
|
|
continue; |
68
|
|
|
} |
69
|
|
|
$attributeValue = $attributeValue->getValue(); |
70
|
|
|
|
71
|
|
|
$propertyName = $this->attributeNameResolver->resolvePropertyName( |
72
|
|
|
\sprintf('%s_%s', $attributeCode, $attributeTranslation->getLocale() |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
$values = $this->resolveProductAttribute( |
76
|
|
|
$attributeConfiguration, |
77
|
|
|
$attributeValue, |
78
|
|
|
$attributeTranslation |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$document->set($propertyName, $values); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function resolveProductAttribute(array $attributeConfiguration, $attributeValue, AttributeTranslation $attribute): array |
87
|
|
|
{ |
88
|
|
|
if ($attribute->getTranslatable()->getType() === 'select') { |
|
|
|
|
89
|
|
|
$choices = $attributeConfiguration['choices']; |
90
|
|
|
if (is_array($attributeValue)) { |
91
|
|
|
foreach ($attributeValue as $i => $item) { |
92
|
|
|
$attributeValue[$i] = $choices[$item][$attribute->getLocale()] ?? $item; |
93
|
|
|
} |
94
|
|
|
} else { |
95
|
|
|
$attributeValue = $choices[$attributeValue][$attribute->getLocale()] ?? $attributeValue; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$attributes = []; |
100
|
|
|
if (is_array($attributeValue)) { |
101
|
|
|
foreach ($attributeValue as $singleElement) { |
102
|
|
|
$attributes[] = $this->stringFormatter->formatToLowercaseWithoutSpaces((string) $singleElement); |
103
|
|
|
} |
104
|
|
|
} else { |
105
|
|
|
$attributeValue = is_string($attributeValue) ? $this->stringFormatter->formatToLowercaseWithoutSpaces($attributeValue) : $attributeValue; |
106
|
|
|
$attributes[] = $attributeValue; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $attributes; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|