Properly formatted MouserProvider and fixed some type issues

This commit is contained in:
Jan Böhmer 2023-10-08 00:15:57 +02:00
parent d7bc74fb2b
commit 4f0730b6f9

View file

@ -49,18 +49,15 @@ class MouserProvider implements InfoProviderInterface
private const ENDPOINT_URL = 'https://api.mouser.com/api/v2/search'; private const ENDPOINT_URL = 'https://api.mouser.com/api/v2/search';
private const NUMBER_OF_RESULTS = 50; //MAX 50
public const DISTRIBUTOR_NAME = 'Mouser'; public const DISTRIBUTOR_NAME = 'Mouser';
public function __construct(private readonly HttpClientInterface $mouserClient, public function __construct(
private readonly HttpClientInterface $mouserClient,
private readonly string $api_key, private readonly string $api_key,
private readonly string $language, private readonly string $language,
private readonly string $options, private readonly string $options,
private readonly int $search_limit) private readonly int $search_limit
{ ) {
} }
public function getProviderInfo(): array public function getProviderInfo(): array
@ -214,23 +211,25 @@ class MouserProvider implements InfoProviderInterface
provider_id: $product['MouserPartNumber'], provider_id: $product['MouserPartNumber'],
name: $product['ManufacturerPartNumber'], name: $product['ManufacturerPartNumber'],
description: $product['Description'], description: $product['Description'],
category: $product['Category'],
manufacturer: $product['Manufacturer'], manufacturer: $product['Manufacturer'],
mpn: $product['ManufacturerPartNumber'], mpn: $product['ManufacturerPartNumber'],
preview_image_url: $product['ImagePath'], preview_image_url: $product['ImagePath'],
category: $product['Category'],
provider_url: $product['ProductDetailUrl'],
manufacturing_status: $this->releaseStatusCodeToManufacturingStatus($product['LifecycleStatus'] ?? null), manufacturing_status: $this->releaseStatusCodeToManufacturingStatus($product['LifecycleStatus'] ?? null),
datasheets: $this->parseDataSheets($product['DataSheetUrl'], $product['MouserPartNumber'] ?? null), provider_url: $product['ProductDetailUrl'],
vendor_infos: $this->pricingToDTOs($product['PriceBreaks'] ?? [], $product['MouserPartNumber'], $product['ProductDetailUrl']), datasheets: $this->parseDataSheets($product['DataSheetUrl'] ?? null,
$product['MouserPartNumber'] ?? null),
vendor_infos: $this->pricingToDTOs($product['PriceBreaks'] ?? [], $product['MouserPartNumber'],
$product['ProductDetailUrl']),
); );
} }
return $result; return $result;
} }
private function parseDataSheets(string $sheetUrl, string $sheetName): ?array private function parseDataSheets(?string $sheetUrl, ?string $sheetName): ?array
{ {
if ($sheetUrl === null) { if (empty($sheetUrl)) {
return null; return null;
} }
$result = []; $result = [];
@ -242,7 +241,8 @@ class MouserProvider implements InfoProviderInterface
* Mouser API price is a string in the form "n[.,]nnn[.,] currency" * Mouser API price is a string in the form "n[.,]nnn[.,] currency"
* then this convert it to a number * then this convert it to a number
*/ */
private function floatvalue($val){ private function priceStrToFloat($val): float
{
$val = str_replace(",", ".", $val); $val = str_replace(",", ".", $val);
$val = preg_replace('/\.(?=.*\.)/', '', $val); $val = preg_replace('/\.(?=.*\.)/', '', $val);
return (float)$val; return (float)$val;
@ -260,18 +260,21 @@ class MouserProvider implements InfoProviderInterface
$prices = []; $prices = [];
foreach ($price_breaks as $price_break) { foreach ($price_breaks as $price_break) {
$number = $this->floatvalue($price_break['Price']); $number = $this->priceStrToFloat($price_break['Price']);
$prices[] = new PriceDTO(minimum_discount_amount: $price_break['Quantity'], price: (string)$number, currency_iso_code: $price_break['Currency']); $prices[] = new PriceDTO(
$number = 0; minimum_discount_amount: $price_break['Quantity'],
price: (string)$number,
currency_iso_code: $price_break['Currency']
);
} }
return [ return [
new PurchaseInfoDTO(distributor_name: self::DISTRIBUTOR_NAME, order_number: $order_number, prices: $prices, product_url: $product_url) new PurchaseInfoDTO(distributor_name: self::DISTRIBUTOR_NAME, order_number: $order_number, prices: $prices,
product_url: $product_url)
]; ];
} }
/* Converts the product status from the MOUSER API to the manufacturing status used in Part-DB: /* Converts the product status from the MOUSER API to the manufacturing status used in Part-DB:
Factory Special Order - Ordine speciale in fabbrica Factory Special Order - Ordine speciale in fabbrica
Not Recommended for New Designs - Non raccomandato per nuovi progetti Not Recommended for New Designs - Non raccomandato per nuovi progetti