From 412fa3f0bf70384d6d3b22cb78920230f5b8c537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 16 Jul 2023 18:35:44 +0200 Subject: [PATCH] Get datasheets and category from digikey --- .../Providers/DigikeyProvider.php | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/Services/InfoProviderSystem/Providers/DigikeyProvider.php b/src/Services/InfoProviderSystem/Providers/DigikeyProvider.php index b8630972..b1196fc7 100644 --- a/src/Services/InfoProviderSystem/Providers/DigikeyProvider.php +++ b/src/Services/InfoProviderSystem/Providers/DigikeyProvider.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace App\Services\InfoProviderSystem\Providers; use App\Entity\Parts\ManufacturingStatus; +use App\Services\InfoProviderSystem\DTOs\FileDTO; use App\Services\InfoProviderSystem\DTOs\ParameterDTO; use App\Services\InfoProviderSystem\DTOs\PartDetailDTO; use App\Services\InfoProviderSystem\DTOs\PriceDTO; @@ -116,7 +117,8 @@ class DigikeyProvider implements InfoProviderInterface provider_key: $this->getProviderKey(), provider_id: $product['DigiKeyPartNumber'], name: $product['ManufacturerPartNumber'], - description: $product['ProductDescription'], + description: $product['DetailedDescription'] ?? $product['ProductDescription'], + category: $this->getCategoryString($product), manufacturer: $product['Manufacturer']['Value'] ?? null, mpn: $product['ManufacturerPartNumber'], preview_image_url: $product['PrimaryPhoto'] ?? null, @@ -138,18 +140,22 @@ class DigikeyProvider implements InfoProviderInterface $footprint = null; $parameters = $this->parametersToDTOs($product['Parameters'] ?? [], $footprint); + $media = $this->mediaToDTOs($product['MediaLinks']); return new PartDetailDTO( provider_key: $this->getProviderKey(), provider_id: $product['DigiKeyPartNumber'], name: $product['ManufacturerPartNumber'], description: $product['DetailedDescription'] ?? $product['ProductDescription'], + category: $this->getCategoryString($product), manufacturer: $product['Manufacturer']['Value'] ?? null, mpn: $product['ManufacturerPartNumber'], preview_image_url: $product['PrimaryPhoto'] ?? null, manufacturing_status: $this->productStatusToManufacturingStatus($product['ProductStatus']), provider_url: $product['ProductUrl'], footprint: $footprint, + datasheets: $media['datasheets'], + images: $media['images'], parameters: $parameters, vendor_infos: $this->pricingToDTOs($product['StandardPricing'] ?? [], $product['DigiKeyPartNumber'], $product['ProductUrl']), ); @@ -174,6 +180,17 @@ class DigikeyProvider implements InfoProviderInterface }; } + private function getCategoryString(array $product): string + { + $category = $product['Category']['Value']; + $sub_category = $product['Family']['Value']; + + //Replace the ' - ' category separator with ' -> ' + $sub_category = str_replace(' - ', ' -> ', $sub_category); + + return $category . ' -> ' . $sub_category; + } + /** * This function converts the "Parameters" part of the Digikey API response to an array of ParameterDTOs * @param array $parameters @@ -216,4 +233,34 @@ class DigikeyProvider implements InfoProviderInterface new PurchaseInfoDTO(distributor_name: self::VENDOR_NAME, order_number: $order_number, prices: $prices, product_url: $product_url) ]; } + + /** + * @param array $media_links + * @return FileDTO[][] + * @phpstan-return array + */ + private function mediaToDTOs(array $media_links): array + { + $datasheets = []; + $images = []; + + foreach ($media_links as $media_link) { + $file = new FileDTO(url: $media_link['Url'], name: $media_link['Title']); + + switch ($media_link['MediaType']) { + case 'Datasheets': + $datasheets[] = $file; + break; + case 'Product Photos': + $images[] = $file; + break; + } + } + + return [ + 'datasheets' => $datasheets, + 'images' => $images, + ]; + } + } \ No newline at end of file