This commit is contained in:
Jan Böhmer 2024-02-24 22:49:05 +01:00
commit 05ec7ab665
2 changed files with 14 additions and 4 deletions

View file

@ -106,7 +106,7 @@ class ParameterDTO
*/
public static function splitIntoValueAndUnit(string $value): ?array
{
if (preg_match('/^(?<value>[0-9.]+)\s*(?<unit>[°℃a-zA-Z_]+\s?\w{0,4})$/u', $value, $matches)) {
if (preg_match('/^(?<value>-?[0-9\.]+)\s*(?<unit>[%Ω°℃a-z_\/]+\s?\w{0,4})$/iu', $value, $matches)) {
$value = $matches['value'];
$unit = $matches['unit'];
@ -115,4 +115,4 @@ class ParameterDTO
return null;
}
}
}

View file

@ -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,
];
}
}
}