mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 09:35:49 +02:00
Properly formatted MouserProvider and fixed some type issues
This commit is contained in:
parent
d7bc74fb2b
commit
4f0730b6f9
1 changed files with 28 additions and 25 deletions
|
@ -49,18 +49,15 @@ class MouserProvider implements InfoProviderInterface
|
|||
|
||||
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 function __construct(private readonly HttpClientInterface $mouserClient,
|
||||
public function __construct(
|
||||
private readonly HttpClientInterface $mouserClient,
|
||||
private readonly string $api_key,
|
||||
private readonly string $language,
|
||||
private readonly string $options,
|
||||
private readonly int $search_limit)
|
||||
{
|
||||
|
||||
private readonly int $search_limit
|
||||
) {
|
||||
}
|
||||
|
||||
public function getProviderInfo(): array
|
||||
|
@ -214,23 +211,25 @@ class MouserProvider implements InfoProviderInterface
|
|||
provider_id: $product['MouserPartNumber'],
|
||||
name: $product['ManufacturerPartNumber'],
|
||||
description: $product['Description'],
|
||||
category: $product['Category'],
|
||||
manufacturer: $product['Manufacturer'],
|
||||
mpn: $product['ManufacturerPartNumber'],
|
||||
preview_image_url: $product['ImagePath'],
|
||||
category: $product['Category'],
|
||||
provider_url: $product['ProductDetailUrl'],
|
||||
manufacturing_status: $this->releaseStatusCodeToManufacturingStatus($product['LifecycleStatus'] ?? null),
|
||||
datasheets: $this->parseDataSheets($product['DataSheetUrl'], $product['MouserPartNumber'] ?? null),
|
||||
vendor_infos: $this->pricingToDTOs($product['PriceBreaks'] ?? [], $product['MouserPartNumber'], $product['ProductDetailUrl']),
|
||||
provider_url: $product['ProductDetailUrl'],
|
||||
datasheets: $this->parseDataSheets($product['DataSheetUrl'] ?? null,
|
||||
$product['MouserPartNumber'] ?? null),
|
||||
vendor_infos: $this->pricingToDTOs($product['PriceBreaks'] ?? [], $product['MouserPartNumber'],
|
||||
$product['ProductDetailUrl']),
|
||||
);
|
||||
}
|
||||
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;
|
||||
}
|
||||
$result = [];
|
||||
|
@ -242,7 +241,8 @@ class MouserProvider implements InfoProviderInterface
|
|||
* Mouser API price is a string in the form "n[.,]nnn[.,] currency"
|
||||
* then this convert it to a number
|
||||
*/
|
||||
private function floatvalue($val){
|
||||
private function priceStrToFloat($val): float
|
||||
{
|
||||
$val = str_replace(",", ".", $val);
|
||||
$val = preg_replace('/\.(?=.*\.)/', '', $val);
|
||||
return (float)$val;
|
||||
|
@ -260,18 +260,21 @@ class MouserProvider implements InfoProviderInterface
|
|||
$prices = [];
|
||||
|
||||
foreach ($price_breaks as $price_break) {
|
||||
$number = $this->floatvalue($price_break['Price']);
|
||||
$prices[] = new PriceDTO(minimum_discount_amount: $price_break['Quantity'], price: (string)$number, currency_iso_code: $price_break['Currency']);
|
||||
$number = 0;
|
||||
$number = $this->priceStrToFloat($price_break['Price']);
|
||||
$prices[] = new PriceDTO(
|
||||
minimum_discount_amount: $price_break['Quantity'],
|
||||
price: (string)$number,
|
||||
currency_iso_code: $price_break['Currency']
|
||||
);
|
||||
}
|
||||
|
||||
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:
|
||||
Factory Special Order - Ordine speciale in fabbrica
|
||||
Not Recommended for New Designs - Non raccomandato per nuovi progetti
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue