getSearchResult()->getProviderUsed() ?? 'unknown'; foreach ($context->getSearchResult()->getItems() as $item) { if (! $item instanceof SearchItem) { continue; } $product = $item->getProduct(); $primaryProvider = $product->getProviderInfo() !== null ? $product->getProviderInfo()->getName() : $fallbackProvider; $provider = $this->providerLabel($product, $primaryProvider); $confidence = $item->getScore() > 0.0 ? $item->getScore() : $configuration->getDefaultConfidence(); $priority = $configuration->getDefaultSourcePriority(); $fields = []; $this->addField($fields, 'title', $product->getName(), $provider, $confidence, $priority); $this->addField($fields, 'subtitle', $this->attributeValue($product, 'subtitle'), $provider, $confidence, $priority); $this->addField($fields, 'description', $product->getDescription(), $provider, $confidence, $priority); $this->addField($fields, 'images', $this->images($product), $provider, $confidence, $priority); $this->addField($fields, 'categories', $product->getCategories(), $provider, $confidence, $priority); $this->addField($fields, 'attributes', $product->getAttributes(), $provider, $confidence, $priority); if ($product->getPrice() !== null) { $this->addField($fields, 'price', [ 'amount' => $product->getPrice()->getAmount(), 'sale_amount' => $product->getPrice()->getSaleAmount(), 'currency' => $product->getPrice()->getCurrency(), ], $provider, $confidence, $priority); } $identifiers = $this->identifiers($product); $this->addField($fields, 'identifiers', $identifiers, $provider, $confidence, $priority); $profiles[] = new ProductProfile($fields); } return $profiles; } private function addField( array &$fields, string $field, $value, string $provider, float $confidence, int $priority ): void { if ($value === null || $value === '' || $value === []) { return; } $fields[] = new FieldScore($field, $value, $provider, $confidence, $priority); } private function identifiers(ProductDTO $product): array { $identifiers = ['id' => $product->getId()]; if ($product->getSku() !== null) { $identifiers['sku'] = $product->getSku()->getValue(); } if ($product->getBarcode() !== null) { $identifiers['barcode'] = $product->getBarcode()->getValue(); } foreach ($product->getAttributes() as $attribute) { if (! $attribute instanceof AttributeDTO) { continue; } $name = strtolower($attribute->getName()); if (in_array($name, ['isbn', 'isbn_10', 'isbn_13', 'ean', 'upc'], true)) { $identifiers[$name] = $attribute->getValue(); } } return array_filter($identifiers, static fn ($value): bool => $value !== ''); } private function attributeValue(ProductDTO $product, string $name): ?string { foreach ($product->getAttributes() as $attribute) { if ($attribute instanceof AttributeDTO && strtolower(trim($attribute->getName())) === strtolower(trim($name))) { $value = trim($attribute->getValue()); return $value !== '' ? $value : null; } } return null; } private function providerLabel(ProductDTO $product, string $primaryProvider): string { foreach ($product->getAttributes() as $attribute) { if ($attribute instanceof AttributeDTO && strtolower($attribute->getName()) === 'source_providers') { $providers = array_values(array_filter(array_map( 'trim', preg_split('/[,]+/', $attribute->getValue()) ?: [] ))); return $providers !== [] ? implode('+', $providers) : $primaryProvider; } } return $primaryProvider; } private function images(ProductDTO $product): array { $images = []; foreach ($product->getImages() as $image) { if (is_array($image)) { $images[] = $image; continue; } if (! $image instanceof ImageDTO) { continue; } $dimensions = $this->imageDimensions($image->getUrl()); $images[] = [ 'url' => $image->getUrl(), 'width' => $dimensions['width'], 'height' => $dimensions['height'], ]; } return $images; } private function imageDimensions(string $url): array { if (preg_match('/(?:^|[^0-9])(\d{2,4})x(\d{2,4})(?:[^0-9]|$)/i', $url, $matches) === 1) { return ['width' => (int) $matches[1], 'height' => (int) $matches[2]]; } if (preg_match('/-(L|M|S)\.(?:jpg|jpeg|png|webp)/i', $url, $matches) === 1) { return match (strtoupper($matches[1])) { 'L' => ['width' => 1000, 'height' => 1500], 'M' => ['width' => 600, 'height' => 900], default => ['width' => 300, 'height' => 450], }; } return ['width' => 0, 'height' => 0]; } }