p29
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user