ParameterDTO: Don't overwrite $unit if it's not empty

This commit is contained in:
Frank Fenor 2024-03-31 20:42:03 +02:00
parent f8db489c0d
commit c9e391bca6
No known key found for this signature in database
GPG key ID: E6B5029FCCE96376

View file

@ -70,7 +70,12 @@ class ParameterDTO
$parts = preg_split('/\s*(\.{3}|~)\s*/', $value); $parts = preg_split('/\s*(\.{3}|~)\s*/', $value);
if (count($parts) === 2) { if (count($parts) === 2) {
//Try to extract number and unit from value (allow leading +) //Try to extract number and unit from value (allow leading +)
if (empty($unit)) {
[$number, $unit] = self::splitIntoValueAndUnit(ltrim($parts[0], " +")) ?? [$parts[0], null]; [$number, $unit] = self::splitIntoValueAndUnit(ltrim($parts[0], " +")) ?? [$parts[0], null];
$unit2 = null;
} else {
$unit2 = $unit;
}
// If the second part has some extra info, we'll save that into value_text // 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) { if (!empty($unit) && preg_match('/^(.+' . preg_quote($unit) . ')\s*(.+)$/', $parts[1], $matches) > 0) {
$parts[1] = $matches[1]; $parts[1] = $matches[1];
@ -78,18 +83,20 @@ class ParameterDTO
} else { } else {
$value_text2 = null; $value_text2 = null;
} }
if (empty($unit2)) {
[$number2, $unit2] = self::splitIntoValueAndUnit(ltrim($parts[1], " +")) ?? [$parts[1], null]; [$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 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)) { 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); return new self(name: $name, value_min: (float) $number, value_max: (float) $number2, value_text: $value_text2, unit: $unit, symbol: $symbol, group: $group);
} }
} }
//If it's a plus/minus value, we'll also treat it as a range //If it's a plus/minus value, we'll also treat it as a range
} elseif (str_starts_with($value, '±')) { } elseif (str_starts_with($value, '±')) {
[$number, $unit] = self::splitIntoValueAndUnit(ltrim($value, " ±")) ?? [$value, null]; [$number, $unit] = self::splitIntoValueAndUnit(ltrim($value, " ±")) ?? [$value, null];
if (is_numeric($number)) { 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: $name, value_min: -abs((float) $number), value_max: abs((float) $number), unit: $unit, symbol: $symbol, group: $group);
} }
} }