mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-03-06 15:30:13 +01:00
Fix RegEx to handle negative values and Ohms without prefix (#530)
* 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
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user