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