This commit is contained in:
team 1
2026-05-04 09:36:20 +02:00
parent 228ca96df1
commit c4bf41cd47
3 changed files with 154 additions and 12 deletions

View File

@@ -1665,21 +1665,70 @@ final class ShopSearchService
$seen = [];
foreach ($products as $product) {
$key = mb_strtolower(trim(implode($this->shopConfig->getDeduplicationSeparator(), [
$product->id,
$product->productNumber ?? '',
$product->name,
$product->url ?? '',
])), 'UTF-8');
$keys = $this->buildProductDeduplicationKeys($product);
if (isset($seen[$key])) {
if ($keys === []) {
$unique[] = $product;
continue;
}
$seen[$key] = true;
$isDuplicate = false;
foreach ($keys as $key) {
if (isset($seen[$key])) {
$isDuplicate = true;
break;
}
}
if ($isDuplicate) {
continue;
}
foreach ($keys as $key) {
$seen[$key] = true;
}
$unique[] = $product;
}
return $unique;
}
}
/**
* @return string[]
*/
private function buildProductDeduplicationKeys(ShopProductResult $product): array
{
$separator = $this->shopConfig->getDeduplicationSeparator();
$productNumber = $this->normalizeDeduplicationValue($product->productNumber ?? '');
if ($productNumber !== '') {
return ['number' . $separator . $productNumber];
}
$id = $this->normalizeDeduplicationValue($product->id);
if ($id !== '') {
return ['id' . $separator . $id];
}
$url = $this->normalizeDeduplicationValue((string) ($product->url ?? ''));
if ($url !== '') {
return ['url' . $separator . $url];
}
$name = $this->normalizeDeduplicationValue($product->name);
if ($name !== '') {
return ['name' . $separator . $name];
}
return [];
}
private function normalizeDeduplicationValue(string $value): string
{
$value = mb_strtolower(trim($value), 'UTF-8');
$value = preg_replace($this->shopConfig->getWhitespaceCollapsePattern(), ' ', $value) ?? $value;
return trim($value);
}
}