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\PropertyNameResolver\ConcatedNameResolverInterface; |
16
|
|
|
use BitBag\SyliusElasticsearchPlugin\PropertyValueResolver\AttributeValueResolverInterface; |
17
|
|
|
use Elastica\Document; |
18
|
|
|
use FOS\ElasticaBundle\Event\TransformEvent; |
19
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
20
|
|
|
|
21
|
|
|
final class AttributeBuilder extends AbstractBuilder |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var ConcatedNameResolverInterface |
25
|
|
|
*/ |
26
|
|
|
private $attributeNameResolver; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var AttributeValueResolverInterface |
30
|
|
|
*/ |
31
|
|
|
private $attributeValueResolver; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ConcatedNameResolverInterface $attributeNameResolver |
35
|
|
|
* @param AttributeValueResolverInterface $attributeValueResolver |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
ConcatedNameResolverInterface $attributeNameResolver, |
39
|
|
|
AttributeValueResolverInterface $attributeValueResolver |
40
|
|
|
) { |
41
|
|
|
$this->attributeNameResolver = $attributeNameResolver; |
42
|
|
|
$this->attributeValueResolver = $attributeValueResolver; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param TransformEvent $event |
47
|
|
|
*/ |
48
|
|
|
public function buildProperty(TransformEvent $event): void |
49
|
|
|
{ |
50
|
|
|
/** @var ProductInterface $product */ |
51
|
|
|
$product = $event->getObject(); |
52
|
|
|
|
53
|
|
|
if (!$product instanceof ProductInterface) { |
|
|
|
|
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$document = $event->getDocument(); |
58
|
|
|
|
59
|
|
|
$this->resolveProductAttributes($product, $document); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param ProductInterface $product |
64
|
|
|
* @param Document $document |
65
|
|
|
*/ |
66
|
|
|
private function resolveProductAttributes(ProductInterface $product, Document $document): void |
67
|
|
|
{ |
68
|
|
|
foreach ($product->getAttributes() as $attributeValue) { |
69
|
|
|
$attributeCode = $attributeValue->getAttribute()->getCode(); |
70
|
|
|
$index = $this->attributeNameResolver->resolvePropertyName($attributeCode); |
71
|
|
|
$value = $this->attributeValueResolver->resolve($attributeValue); |
72
|
|
|
$attributes = []; |
73
|
|
|
|
74
|
|
|
if (is_array($value)) { |
75
|
|
|
foreach ($value as $singleElement) { |
76
|
|
|
$attributes[] = $singleElement; |
77
|
|
|
} |
78
|
|
|
} else { |
79
|
|
|
$attributes[] = $value; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$document->set($index, $attributes); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|