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 Sylius\Component\Locale\Context\LocaleContextInterface; |
17
|
|
|
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
|
20
|
|
|
final class ShopProductListDataHandler implements DataHandlerInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var TaxonRepositoryInterface |
24
|
|
|
*/ |
25
|
|
|
private $taxonRepository; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var LocaleContextInterface |
29
|
|
|
*/ |
30
|
|
|
private $localeContext; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $nameProperty; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var PaginationDataHandler |
39
|
|
|
*/ |
40
|
|
|
private $paginationDataHandler; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $taxonsProperty; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $optionPropertyPrefix; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private $attributePropertyPrefix; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param TaxonRepositoryInterface $taxonRepository |
59
|
|
|
* @param LocaleContextInterface $localeContext |
60
|
|
|
* @param PaginationDataHandler $paginationDataHandler |
61
|
|
|
* @param string $nameProperty |
62
|
|
|
* @param string $taxonsProperty |
63
|
|
|
* @param string $optionPropertyPrefix |
64
|
|
|
* @param string $attributePropertyPrefix |
65
|
|
|
*/ |
66
|
|
|
public function __construct( |
67
|
|
|
TaxonRepositoryInterface $taxonRepository, |
68
|
|
|
LocaleContextInterface $localeContext, |
69
|
|
|
PaginationDataHandler $paginationDataHandler, |
70
|
|
|
string $nameProperty, |
71
|
|
|
string $taxonsProperty, |
72
|
|
|
string $optionPropertyPrefix, |
73
|
|
|
string $attributePropertyPrefix |
74
|
|
|
) |
75
|
|
|
{ |
76
|
|
|
$this->taxonRepository = $taxonRepository; |
77
|
|
|
$this->localeContext = $localeContext; |
78
|
|
|
$this->paginationDataHandler = $paginationDataHandler; |
79
|
|
|
$this->nameProperty = $nameProperty; |
80
|
|
|
$this->taxonsProperty = $taxonsProperty; |
81
|
|
|
$this->optionPropertyPrefix = $optionPropertyPrefix; |
82
|
|
|
$this->attributePropertyPrefix = $attributePropertyPrefix; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function retrieveData(Request $request): array |
89
|
|
|
{ |
90
|
|
|
$slug = $request->get('slug'); |
91
|
|
|
$taxon = $this->taxonRepository->findOneBySlug($slug, $this->localeContext->getLocaleCode()); |
92
|
|
|
|
93
|
|
|
if (null === $taxon) { |
94
|
|
|
throw new TaxonNotFoundException(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$data = $this->paginationDataHandler->retrieveData($request); |
98
|
|
|
$data[$this->nameProperty] = (string) $request->query->get($this->nameProperty); |
99
|
|
|
$data[$this->taxonsProperty] = (string) strtolower($taxon->getCode()); |
100
|
|
|
|
101
|
|
|
$this->handlePrefixedProperty($request, 'options', $this->optionPropertyPrefix, $data); |
102
|
|
|
|
103
|
|
|
return $data; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param Request $request |
108
|
|
|
* @param string $formName |
109
|
|
|
* @param string $propertyPrefix |
110
|
|
|
* @param array $data |
111
|
|
|
*/ |
112
|
|
|
private function handlePrefixedProperty( |
113
|
|
|
Request $request, |
114
|
|
|
string $formName, |
115
|
|
|
string $propertyPrefix, |
116
|
|
|
array &$data |
117
|
|
|
): void |
118
|
|
|
{ |
119
|
|
|
if (!isset($request->query->all()[$formName])) { |
120
|
|
|
return; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
foreach ($request->query->all()[$formName] as $key => $value) { |
124
|
|
|
if (is_array($value) && 0 === strpos($key, $propertyPrefix)) { |
125
|
|
|
$data[$key] = array_map(function (string $property): string { |
126
|
|
|
return strtolower($property); |
127
|
|
|
}, $value); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|