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\Controller\RequestDataHandler; |
14
|
|
|
|
15
|
|
|
use BitBag\SyliusElasticsearchPlugin\Exception\TaxonNotFoundException; |
16
|
|
|
use BitBag\SyliusElasticsearchPlugin\Finder\ProductAttributesFinderInterface; |
17
|
|
|
use Sylius\Component\Attribute\AttributeType\CheckboxAttributeType; |
18
|
|
|
use Sylius\Component\Attribute\AttributeType\IntegerAttributeType; |
19
|
|
|
use Sylius\Component\Locale\Context\LocaleContextInterface; |
20
|
|
|
use Sylius\Component\Product\Model\ProductAttribute; |
21
|
|
|
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; |
22
|
|
|
|
23
|
|
|
final class ShopProductListDataHandler implements DataHandlerInterface |
24
|
|
|
{ |
25
|
|
|
/** @var TaxonRepositoryInterface */ |
26
|
|
|
private $taxonRepository; |
27
|
|
|
|
28
|
|
|
/** @var LocaleContextInterface */ |
29
|
|
|
private $localeContext; |
30
|
|
|
|
31
|
|
|
/** @var ProductAttributesFinderInterface */ |
32
|
|
|
private $attributesFinder; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
private $namePropertyPrefix; |
36
|
|
|
|
37
|
|
|
/** @var string */ |
38
|
|
|
private $taxonsProperty; |
39
|
|
|
|
40
|
|
|
/** @var string */ |
41
|
|
|
private $optionPropertyPrefix; |
42
|
|
|
|
43
|
|
|
/** @var string */ |
44
|
|
|
private $attributePropertyPrefix; |
45
|
|
|
|
46
|
|
|
public function __construct( |
47
|
|
|
TaxonRepositoryInterface $taxonRepository, |
48
|
|
|
LocaleContextInterface $localeContext, |
49
|
|
|
ProductAttributesFinderInterface $attributesFinder, |
50
|
|
|
string $namePropertyPrefix, |
51
|
|
|
string $taxonsProperty, |
52
|
|
|
string $optionPropertyPrefix, |
53
|
|
|
string $attributePropertyPrefix |
54
|
|
|
) { |
55
|
|
|
$this->taxonRepository = $taxonRepository; |
56
|
|
|
$this->localeContext = $localeContext; |
57
|
|
|
$this->attributesFinder = $attributesFinder; |
58
|
|
|
$this->namePropertyPrefix = $namePropertyPrefix; |
59
|
|
|
$this->taxonsProperty = $taxonsProperty; |
60
|
|
|
$this->optionPropertyPrefix = $optionPropertyPrefix; |
61
|
|
|
$this->attributePropertyPrefix = $attributePropertyPrefix; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function retrieveData(array $requestData): array |
65
|
|
|
{ |
66
|
|
|
$slug = $requestData['slug']; |
67
|
|
|
$taxon = $this->taxonRepository->findOneBySlug($slug, $this->localeContext->getLocaleCode()); |
68
|
|
|
|
69
|
|
|
if (null === $taxon) { |
70
|
|
|
throw new TaxonNotFoundException(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$data[$this->namePropertyPrefix] = (string) $requestData[$this->namePropertyPrefix]; |
|
|
|
|
74
|
|
|
$data[$this->taxonsProperty] = strtolower($taxon->getCode()); |
|
|
|
|
75
|
|
|
$data['taxon'] = $taxon; |
76
|
|
|
$data = array_merge($data, $requestData['price']); |
77
|
|
|
|
78
|
|
|
$attributesDefinitions = $this->attributesFinder->findByTaxon($taxon); |
79
|
|
|
|
80
|
|
|
$this->handleOptionsPrefixedProperty($requestData, $data); |
81
|
|
|
$this->handleAttributesPrefixedProperty($requestData, $data, $attributesDefinitions); |
82
|
|
|
|
83
|
|
|
return $data; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function handleOptionsPrefixedProperty( |
87
|
|
|
array $requestData, |
88
|
|
|
array &$data |
89
|
|
|
): void { |
90
|
|
|
if (!isset($requestData['options'])) { |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
foreach ($requestData['options'] as $key => $value) { |
95
|
|
|
if (is_array($value) && 0 === strpos($key, $this->optionPropertyPrefix)) { |
96
|
|
|
$data[$key] = array_map(function (string $property): string { |
97
|
|
|
return strtolower($property); |
98
|
|
|
}, $value); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function handleAttributesPrefixedProperty( |
104
|
|
|
array $requestData, |
105
|
|
|
array &$data, |
106
|
|
|
?array $attributesDefinitions = [] |
107
|
|
|
): void { |
108
|
|
|
if (!isset($requestData['attributes'])) { |
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$attributeTypes = $this->getAttributeTypes($attributesDefinitions); |
113
|
|
|
|
114
|
|
|
foreach ($requestData['attributes'] as $key => $value) { |
115
|
|
|
if (!is_array($value) || 0 !== strpos($key, $this->attributePropertyPrefix)) { |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
$data[$key] = $this->reformatAttributeArrayValues($value, $key, $attributeTypes); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function getAttributeTypes(array $attributesDefinitions): array |
123
|
|
|
{ |
124
|
|
|
$data = []; |
125
|
|
|
/** @var ProductAttribute $attributesDefinition */ |
126
|
|
|
foreach ($attributesDefinitions as $attributesDefinition) { |
127
|
|
|
$data['attribute_' . $attributesDefinition->getCode()] = $attributesDefinition->getType(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $data; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function reformatAttributeArrayValues(array $attributeValues, string $property, array $attributesDefinitions): array |
134
|
|
|
{ |
135
|
|
|
$reformattedValues = []; |
136
|
|
|
foreach ($attributeValues as $attributeValue) { |
137
|
|
|
switch ($attributesDefinitions[$property]) { |
138
|
|
|
case CheckboxAttributeType::TYPE: |
139
|
|
|
$value = (bool) ($attributeValue); |
140
|
|
|
|
141
|
|
|
break; |
142
|
|
|
case IntegerAttributeType::TYPE: |
143
|
|
|
$value = (float) ($attributeValue); |
144
|
|
|
|
145
|
|
|
break; |
146
|
|
|
default: |
147
|
|
|
$value = strtolower($attributeValue); |
148
|
|
|
} |
149
|
|
|
$reformattedValues[] = $value; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $reformattedValues; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|