mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-01-23 10:39:34 +00:00
ParameterDTO: Don't overwrite $unit if it's not empty
This commit is contained in:
parent
f8db489c0d
commit
c9e391bca6
1 changed files with 11 additions and 4 deletions
|
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue