Move improved range detection from LCSCProvider to ParameterDTO class

Improve numeric value detection by moving extra info to value_text
This commit is contained in:
Frank Fenor 2024-03-31 11:38:41 +02:00
parent a31f7d2414
commit 283ec725d6
No known key found for this signature in database
GPG key ID: E6B5029FCCE96376
2 changed files with 41 additions and 31 deletions

View file

@ -54,23 +54,54 @@ class ParameterDTO
*/
public static function parseValueField(string $name, string|float $value, ?string $unit = null, ?string $symbol = null, ?string $group = null): self
{
if (is_float($value) || is_numeric($value)) {
return new self($name, value_typ: (float) $value, unit: $unit, symbol: $symbol, group: $group);
if (is_string($value) && preg_match('/^(.+)(@.+)$/', $value, $matches) === 1) {
$value = $matches[1];
$value_text = $matches[2];
} else {
$value_text = null;
}
//Try to parse as range
if (str_contains($value, '...')) {
$parts = explode('...', $value);
if (count($parts) === 2) {
if (is_float($value) || is_numeric($value)) {
return new self($name, value_typ: (float) $value, value_text: $value_text, unit: $unit, symbol: $symbol, group: $group);
}
//Ensure that both parts are numerical
if (is_numeric($parts[0]) && is_numeric($parts[1])) {
return new self($name, value_min: (float) $parts[0], value_max: (float) $parts[1], unit: $unit, symbol: $symbol, group: $group);
//If the attribute contains a tilde we assume it is a range
if (preg_match('/(\.{3}|~)/', $value) === 1) {
$parts = preg_split('/\s*(\.{3}|~)\s*/', $value);
if (count($parts) === 2) {
//Try to extract number and unit from value (allow leading +)
[$number, $unit] = self::splitIntoValueAndUnit(ltrim($parts[0], " +")) ?? [$parts[0], null];
// If the second part has some extra info, we'll save that into value_text
if (!empty($unit) && preg_match('/^(.+' . preg_quote($unit) . ')\s*(.+)$/', $parts[1], $matches) > 0) {
$parts[1] = $matches[1];
$value_text2 = $matches[2];
}
[$number2, $unit2] = self::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)) {
return new self(name: $name, value_min: (float) $number, value_max: (float) $number2, value_text: $value_text2, unit: $unit, group: null);
}
}
//If it's a plus/minus value, we'll also treat it as a range
} elseif (str_starts_with($value, '±')) {
[$number, $unit] = self::splitIntoValueAndUnit(ltrim($value, " ±")) ?? [$value, null];
if (is_numeric($number)) {
return new self(name: $name, value_min: -abs((float) $number), value_max: abs((float) $number), unit: $unit, group: null);
}
}
return new self($name, value_text: $value, unit: $unit, symbol: $symbol, group: $group);
//If no unit was passed to us, try to extract it from the value
if (empty($unit)) {
[$value, $unit] = self::splitIntoValueAndUnit($value) ?? [$value, null];
}
//Were we successful in trying to reduce the value to a number?
if ($value_text !== null && is_numeric($value)) {
return new self($name, value_typ: (float) $value, value_text: $value_text, unit: $unit, symbol: $symbol, group: $group);
}
return new self($name, value_text: $value.$value_text, unit: $unit, symbol: $symbol, group: $group);
}
/**

View file

@ -299,27 +299,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);