Skip to content

Commit 40df886

Browse files
committed
ACP2E-2119: address CR comments
1 parent 942208d commit 40df886

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

app/code/Magento/CatalogGraphQl/DataProvider/Product/SearchCriteriaBuilder.php

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ public function __construct(
109109
*/
110110
public function build(array $args, bool $includeAggregation): SearchCriteriaInterface
111111
{
112-
$matchTypes = [];
112+
$partialMatchFilters = [];
113113
if (isset($args['filter'])) {
114-
$matchTypes = $this->getPartialMatchFilters($args);
114+
$partialMatchFilters = $this->getPartialMatchFilters($args);
115115
$args = $this->removeMatchTypeFromArguments($args);
116116
}
117117
$searchCriteria = $this->builder->build('products', $args);
@@ -130,8 +130,8 @@ public function build(array $args, bool $includeAggregation): SearchCriteriaInte
130130
}
131131
$searchCriteria->setRequestName($requestName);
132132

133-
if (count($matchTypes)) {
134-
$this->updateMatchTypeRequestConfig($requestName, $matchTypes);
133+
if (count($partialMatchFilters)) {
134+
$this->updateMatchTypeRequestConfig($requestName, $partialMatchFilters);
135135
}
136136

137137
if ($isSearch) {
@@ -155,20 +155,18 @@ public function build(array $args, bool $includeAggregation): SearchCriteriaInte
155155
* Update dynamically the search match type based on requested params
156156
*
157157
* @param string $requestName
158-
* @param array $matchTypes
158+
* @param array $partialMatchFilters
159159
* @return void
160160
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
161161
*/
162-
private function updateMatchTypeRequestConfig(string $requestName, array $matchTypes): void
162+
private function updateMatchTypeRequestConfig(string $requestName, array $partialMatchFilters): void
163163
{
164164
$data = $this->searchConfig->get($requestName);
165-
foreach ($data['queries'] as $queryName => $match) {
166-
$attributeName = str_replace('_query', '', $queryName);
167-
if (isset($match['match']) && in_array($attributeName, $matchTypes, true)) {
168-
foreach ($match['match'] as $index => $matchItem) {
169-
$match['match'][$index]['matchCondition'] = 'match_phrase_prefix';
165+
foreach ($data['queries'] as $queryName => $query) {
166+
foreach ($query['match'] ?? [] as $index => $matchItem) {
167+
if (in_array($matchItem['field'] ?? null, $partialMatchFilters, true)) {
168+
$data['queries'][$queryName]['match'][$index]['matchCondition'] = 'match_phrase_prefix';
170169
}
171-
$data['queries'][$queryName] = $match;
172170
}
173171
}
174172
$this->searchConfig->merge([$requestName => $data]);
@@ -182,15 +180,13 @@ private function updateMatchTypeRequestConfig(string $requestName, array $matchT
182180
*/
183181
private function getPartialMatchFilters(array $args): array
184182
{
185-
$matchType = [];
183+
$partialMatchFilters = [];
186184
foreach ($args['filter'] as $fieldName => $conditions) {
187-
foreach ($conditions as $filter => $value) {
188-
if ($filter === 'match_type' && $value === 'PARTIAL') {
189-
$matchType[$fieldName] = $fieldName;
190-
}
185+
if (isset($conditions['match_type']) && $conditions['match_type'] === 'PARTIAL') {
186+
$partialMatchFilters[] = $fieldName;
191187
}
192188
}
193-
return $matchType;
189+
return $partialMatchFilters;
194190
}
195191

196192
/**
@@ -202,11 +198,9 @@ private function getPartialMatchFilters(array $args): array
202198
*/
203199
private function removeMatchTypeFromArguments(array $args): array
204200
{
205-
foreach ($args['filter'] as $fieldName => $conditions) {
206-
foreach ($conditions as $filter => $value) {
207-
if ($filter === 'match_type') {
208-
unset($args['filter'][$fieldName][$filter]);
209-
}
201+
foreach ($args['filter'] as &$conditions) {
202+
if (isset($conditions['match_type'])) {
203+
unset($conditions['match_type']);
210204
}
211205
}
212206

0 commit comments

Comments
 (0)