|
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
|
|
|
|
|
73
|
|
|
if (!$document->has($index)) { |
|
74
|
|
|
$document->set($index, []); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$reference = $document->get($index); |
|
78
|
|
|
|
|
79
|
|
|
if (is_array($value)) { |
|
80
|
|
|
foreach ($value as $singleElement) { |
|
81
|
|
|
$reference[] = $singleElement; |
|
82
|
|
|
} |
|
83
|
|
|
} else { |
|
84
|
|
|
$reference[] = $value; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$document->set($index, $reference); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|