From e8bc93f67adb35a03be8a5e47d65d6e3f9b7bcbd Mon Sep 17 00:00:00 2001 From: frank-f Date: Sat, 24 Feb 2024 22:48:38 +0100 Subject: [PATCH] Fix RegEx to handle negative values and Ohms without prefix (#530) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix RegEx to include negative values * Update RegEx to handle Ω without prefix * Update RegEx to include % * Handle plus/minus values as range * Fix copy&paste error * Change minimum value to negative * Escape decimal point and add slash to valid unit characters to be able to pick up for example "ppm/°C" * Skip empty values --- .../InfoProviderSystem/DTOs/ParameterDTO.php | 4 ++-- .../InfoProviderSystem/Providers/LCSCProvider.php | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Services/InfoProviderSystem/DTOs/ParameterDTO.php b/src/Services/InfoProviderSystem/DTOs/ParameterDTO.php index b5597eaf..e8ff9fb9 100644 --- a/src/Services/InfoProviderSystem/DTOs/ParameterDTO.php +++ b/src/Services/InfoProviderSystem/DTOs/ParameterDTO.php @@ -106,7 +106,7 @@ class ParameterDTO */ public static function splitIntoValueAndUnit(string $value): ?array { - if (preg_match('/^(?[0-9.]+)\s*(?[°℃a-zA-Z_]+\s?\w{0,4})$/u', $value, $matches)) { + if (preg_match('/^(?-?[0-9\.]+)\s*(?[%Ω°℃a-z_\/]+\s?\w{0,4})$/iu', $value, $matches)) { $value = $matches['value']; $unit = $matches['unit']; @@ -115,4 +115,4 @@ class ParameterDTO return null; } -} \ No newline at end of file +} diff --git a/src/Services/InfoProviderSystem/Providers/LCSCProvider.php b/src/Services/InfoProviderSystem/Providers/LCSCProvider.php index 11a2ae59..eea43f61 100755 --- a/src/Services/InfoProviderSystem/Providers/LCSCProvider.php +++ b/src/Services/InfoProviderSystem/Providers/LCSCProvider.php @@ -270,8 +270,11 @@ class LCSCProvider implements InfoProviderInterface foreach ($attributes as $attribute) { + //Skip this attribute if it's empty + if (in_array(trim($attribute['paramValueEn']), array('', '-'))) { + continue; //If the attribute contains a tilde we assume it is a range - if (str_contains($attribute['paramValueEn'], '~')) { + } elseif (str_contains($attribute['paramValueEn'], '~')) { $parts = explode('~', $attribute['paramValueEn']); if (count($parts) === 2) { //Try to extract number and unit from value (allow leading +) @@ -284,6 +287,13 @@ class LCSCProvider implements InfoProviderInterface continue; } } + //If it's a plus/minus value, we'll also it like a range + } elseif (str_starts_with($attribute['paramValueEn'], '±')) { + [$number, $unit] = ParameterDTO::splitIntoValueAndUnit(ltrim($attribute['paramValueEn'], " ±")) ?? [$attribute['paramValueEn'], null]; + if (is_numeric($number)) { + $result[] = new ParameterDTO(name: $attribute['paramNameEn'], value_min: -abs((float) $number), value_max: abs((float) $number), unit: $unit, group: null); + continue; + } } $result[] = ParameterDTO::parseValueIncludingUnit(name: $attribute['paramNameEn'], value: $attribute['paramValueEn'], group: null); @@ -321,4 +331,4 @@ class LCSCProvider implements InfoProviderInterface ProviderCapabilities::FOOTPRINT, ]; } -} \ No newline at end of file +}