mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-13 11:54:32 +02:00
Improve parameter parsing (#583)
* DigiKey: Skip empty values * Move improved range detection from LCSCProvider to ParameterDTO class Improve numeric value detection by moving extra info to value_text * ParameterDTO: Add micro unit prefix * Bring $value_text2 to a defined state * ParameterDTO: Don't overwrite $unit if it's not empty * ParameterDTO: Don't overwrite $unit if it's not empty * Correct some inaccuacies in comments * Added tests and fixed certain edge cases in parsing parameters * Added more tests for parameter parsing --------- Co-authored-by: Jan Böhmer <mail@jan-boehmer.de>
This commit is contained in:
parent
fdf64f9e9a
commit
7a6b045030
4 changed files with 141 additions and 34 deletions
|
@ -323,27 +323,6 @@ class LCSCProvider implements InfoProviderInterface
|
|||
//Skip this attribute if it's empty
|
||||
if (in_array(trim($attribute['paramValueEn']), array('', '-'), true)) {
|
||||
continue;
|
||||
//If the attribute contains a tilde we assume it is a range
|
||||
} elseif (str_contains($attribute['paramValueEn'], '~')) {
|
||||
$parts = explode('~', $attribute['paramValueEn']);
|
||||
if (count($parts) === 2) {
|
||||
//Try to extract number and unit from value (allow leading +)
|
||||
[$number, $unit] = ParameterDTO::splitIntoValueAndUnit(ltrim($parts[0], " +")) ?? [$parts[0], null];
|
||||
[$number2, $unit2] = ParameterDTO::splitIntoValueAndUnit(ltrim($parts[1], " +")) ?? [$parts[1], null];
|
||||
|
||||
//If both parts have the same unit and both values are numerical, we assume it is a range
|
||||
if ($unit === $unit2 && is_numeric($number) && is_numeric($number2)) {
|
||||
$result[] = new ParameterDTO(name: $attribute['paramNameEn'], value_min: (float) $number, value_max: (float) $number2, unit: $unit, group: null);
|
||||
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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue