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\AttributeInterface; |
20
|
|
|
use Sylius\Component\Attribute\Model\AttributeTranslation; |
21
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
22
|
|
|
|
23
|
|
|
final class AttributeBuilder extends AbstractBuilder |
24
|
|
|
{ |
25
|
|
|
/** @var ConcatedNameResolverInterface */ |
26
|
|
|
private $attributeNameResolver; |
27
|
|
|
|
28
|
|
|
/** @var StringFormatterInterface */ |
29
|
|
|
private $stringFormatter; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
ConcatedNameResolverInterface $attributeNameResolver, |
33
|
|
|
StringFormatterInterface $stringFormatter |
34
|
|
|
) { |
35
|
|
|
$this->attributeNameResolver = $attributeNameResolver; |
36
|
|
|
$this->stringFormatter = $stringFormatter; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function consumeEvent(TransformEvent $event): void |
40
|
|
|
{ |
41
|
|
|
$this->buildProperty($event, ProductInterface::class, |
42
|
|
|
function (ProductInterface $product, Document $document): void { |
43
|
|
|
$this->resolveProductAttributes($product, $document); |
44
|
|
|
}); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function resolveProductAttributes(ProductInterface $product, Document $document): void |
48
|
|
|
{ |
49
|
|
|
foreach ($product->getAttributes() as $productAttribute) { |
50
|
|
|
$attribute = $productAttribute->getAttribute(); |
51
|
|
|
|
52
|
|
|
if (!$attribute) { |
53
|
|
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->processAttribute($attribute, $productAttribute, $document); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function resolveProductAttribute(array $attributeConfiguration, $attributeValue, AttributeTranslation $attribute): array |
61
|
|
|
{ |
62
|
|
|
if ($attribute->getTranslatable()->getType() === 'select') { |
|
|
|
|
63
|
|
|
$choices = $attributeConfiguration['choices']; |
64
|
|
|
if (is_array($attributeValue)) { |
65
|
|
|
foreach ($attributeValue as $i => $item) { |
66
|
|
|
$attributeValue[$i] = $choices[$item][$attribute->getLocale()] ?? $item; |
67
|
|
|
} |
68
|
|
|
} else { |
69
|
|
|
$attributeValue = $choices[$attributeValue][$attribute->getLocale()] ?? $attributeValue; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$attributes = []; |
74
|
|
|
if (is_array($attributeValue)) { |
75
|
|
|
foreach ($attributeValue as $singleElement) { |
76
|
|
|
$attributes[] = $this->stringFormatter->formatToLowercaseWithoutSpaces((string) $singleElement); |
77
|
|
|
} |
78
|
|
|
} else { |
79
|
|
|
$attributeValue = is_string($attributeValue) ? $this->stringFormatter->formatToLowercaseWithoutSpaces($attributeValue) : $attributeValue; |
80
|
|
|
$attributes[] = $attributeValue; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $attributes; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
private function processAttribute(AttributeInterface $attribute, $productAttribute, Document $document): void |
88
|
|
|
{ |
89
|
|
|
$attributeCode = $attribute->getCode(); |
90
|
|
|
$attributeConfiguration = $attribute->getConfiguration(); |
91
|
|
|
foreach ($attribute->getTranslations() as $attributeTranslation) { |
92
|
|
|
$value = $productAttribute->getValue(); |
93
|
|
|
$documentKey = $this->attributeNameResolver->resolvePropertyName($attributeCode); |
|
|
|
|
94
|
|
|
$code = \sprintf('%s_%s', $documentKey, $attributeTranslation->getLocale()); |
95
|
|
|
$values = $this->resolveProductAttribute( |
96
|
|
|
$attributeConfiguration, |
97
|
|
|
$value, |
98
|
|
|
$attributeTranslation |
99
|
|
|
); |
100
|
|
|
$document->set($code, $values); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|